Containers have a writable layer, but anything written there is lost when the container is removed. Docker offers three mount types for data that must persist or be shared: volumes, bind mounts and tmpfs mounts. Volumes - Storage created and managed by Docker, stored in Docker's own area on the host (for example /var/lib/docker/volumes on Linux). docker volume create pgdata docker run -d --name db -v pgdata:/var/lib/postgresql/data postgres:16 (or --mount type=volume,source=pgdata,target=/var/lib/postgresql/data) - Survive container removal; can be attached to new containers or shared between containers. - Managed with Docker commands (docker volume ls, inspect, rm, prune) and supported by volume drivers for remote or cloud storage. - Portable across hosts and operating systems; independent of the host's directory structure. - Good performance, especially on Docker Desktop for Windows and macOS compared with bind mounts. - Recommended for persistent application data: databases, uploaded files, message broker data. Bind mounts - Mount an existing file or directory from the host into the container: docker run -d -v "$(pwd)/src:/app/src" -p 3000:3000 my-dev-image (or --mount type=bind,source=/home/me/src,target=/app/src) - Depend on the host's filesystem paths and structure, so they are less portable. - Changes are visible both ways instantly: editing a file on the host updates it in the container. - The container can modify host files, which carries security risks (mount read-only with :ro when possible). - Performance can be slower on Docker Desktop due to file sharing between the host and the Linux VM. - Ideal for local development (live reload of source code), sharing configuration files or certificates from the host. tmpfs mounts - Stored in the host's memory only and never written to disk; removed when the container stops. - Useful for sensitive temporary data or high-speed scratch space. docker run --tmpfs /app/cache:size=64m my-image Key differences between volumes and bind mounts - Managed by: Docker vs the user and host filesystem. - Location: Docker's storage area vs any path you choose. - Portability: high vs depends on host paths. - Typical use: production persistent data vs development and host file sharing. - Pre-population: when you mount an empty volume, Docker copies the existing content of the container directory into it; a bind mount hides the container directory's existing content. In Docker Compose services: db: image: postgres:16 volumes: - pgdata:/var/lib/postgresql/data # named volume api: build: . volumes: - ./src:/app/src:ro # bind mount (read-only) volumes: pgdata: In Kubernetes The equivalent concepts are PersistentVolumes and PersistentVolumeClaims backed by cloud disks or file shares, while hostPath resembles bind mounts (generally avoided in production). Best practices - Do not store important data in the container's writable layer. - Back up volumes (or use managed databases instead of containerised databases in production). - Use read-only bind mounts where possible.
Ravindra Wadile · · 0 likes