Q: How do you share data between the host and a container?
To share data between the host and a container, mount it: use a bind mount (-v /host/path:/container/path) to expose a specific host directory, or a named volume that Docker manages for persistent data.
Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: To <strong>share data between the host and a container</strong>, mount it: use a bind mount (<code>-v /host/path:/container/path</code>) to expose a specific host directory, or a named volume that Docker manages for persistent data. Interviewers ask this to see whether you know the three mount types, understand why container writes disappear on removal, and can debug the classic permission and path problems. A good answer explains when each type fits, shows the modern <code>--mount</code> syntax, and mentions read-only mounts, UID mapping and SELinux labels.
Mental model: three ways to mount data
A container's writable layer is deleted with the container, so anything worth keeping must live in a mount.
- Bind mount: maps an existing host path into the container. Great for source code in development and for config files. The host path must exist for the container to see real data.
- Named volume: storage created and managed by Docker under its data root (typically
/var/lib/docker/volumes). Best for databases and anything that should be portable and independent of host layout. - tmpfs mount: memory-backed, never touches disk, gone when the container stops. Use it for secrets or scratch space on Linux hosts.
- Anonymous volume: created by a
VOLUMEinstruction when you give no name. Hard to track and a common source of orphaned data. - Choosing quickly: if you want to edit files on the host and see them in the container, use a bind mount. If Docker should own the data and you only care that it persists, use a named volume.
Hands-on: share data between the host and a container
Prefer --mount in docs and scripts because it is explicit. -v remains common and fine.
# Bind mount, host dir is shared both ways
docker run -d --name web \
--mount type=bind,source="$(pwd)/site",target=/usr/share/nginx/html,readonly \
nginx:1.27
# Named volume for persistent database files
docker run -d --name db \
--mount type=volume,source=pgdata,target=/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=change-me postgres:16
# Short form of the same bind mount
docker run -d -v "$(pwd)/site:/usr/share/nginx/html:ro" nginx:1.27
- Bind mount a directory: keeps host and container views of the same files in sync in both directions.
- Named volume:
docker volume createis optional because Docker creates it on first use. - Read-only: append
,readonly(or:rowith-v) so the container cannot modify host files. - Behavioural difference: with
-va missing host path is silently created as a root-owned directory, while--mount type=bindfails with an error, which catches typos. - Backups of volumes: run a short-lived helper container that mounts the volume read-only and writes a tarball to a bind-mounted host directory. This works without knowing the host path of the volume.
Verify what is actually mounted
Confirm the mount, the direction of data flow and the permissions.
- List mounts:
docker inspect --format '{{json .Mounts}}' webshows type, source, destination and read/write mode. - Test both directions: create a file on the host and
docker exec web ls /usr/share/nginx/html, then write inside and check the host. - Volume details:
docker volume inspect pgdatagives the Mountpoint on the host. Treat that path as read-only for debugging, not as an edit target. - Find orphans:
docker volume ls -f dangling=truelists volumes no container references. - Compose behaviour: named volumes declared at the top level survive
docker compose down. Onlydocker compose down -vremoves them, which is a classic way to lose a development database by accident.
Failure modes: permissions, SELinux and Docker Desktop
Most bind mount problems are not Docker bugs but identity and path problems.
- UID mismatch: files are owned by numeric IDs. If the container runs as UID 1000 and the host directory belongs to root, writes fail with
Permission denied. Fix with--user $(id -u):$(id -g)orchownthe directory. - SELinux hosts (RHEL, Fedora): add
:z(shared) or:Z(private) to relabel the content. Never relabel system directories such as/homeor/usr. - Docker Desktop: the host path must be in a shared location, and bind mounts cross a VM boundary, so heavy file I/O such as
node_modulesis slower than a named volume. - Mount hides content: mounting over a non-empty image directory makes the original files invisible, a frequent cause of missing configs.
- Sharing between containers: the same named volume can be mounted into several containers, for example a web server and a log shipper, but concurrent writers still need application-level coordination to avoid corruption.
Production practice: choose by lifecycle
Match the mount type to who owns the data.
services:
api:
image: registry.example.com/my-api:2.0
volumes:
- ./config:/app/config:ro # bind, read-only
- api-data:/var/lib/api # named volume
volumes:
api-data:
- Prod data: named volumes or a volume driver for network storage, with backups taken via a helper container using
--volumes-fromortar. - Dev code: bind mounts, or Compose
develop.watchsync, to avoid rebuilding on each edit. - Hardening: run the container as a non-root user, mount config as read-only, and avoid mounting
/var/run/docker.sockunless truly required because it grants root-equivalent control of the host. - Remote storage: for multi-host setups, use a volume driver or network filesystem. Bind mounts to NFS paths work but add latency and locking caveats that surface under load.
- Containers lose their writable layer on removal, so I share or persist data through mounts: bind mounts, named volumes or tmpfs.
- I verify with <code>docker inspect</code> on .Mounts, test writes in both directions, and check numeric UID and SELinux labels when permissions fail.
- In production I use named volumes with backups, mount config read-only, and never expose the Docker socket casually.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Using -v with a typo in the host path Docker silently creates an empty root-owned directory, so the app starts with no data. Use --mount to get a hard error.
- Bind mounting over a populated image directory The mount hides the image's files, which breaks apps that expect default configs or assets there.
- Ignoring numeric UID/GID ownership Names do not map between host and container, so write failures look random until you compare IDs.
- Relying on anonymous volumes for databases They are unnamed, easy to orphan and hard to back up. Name volumes explicitly.