Docker Compose volume mounts happen before the container runs
Codemia Knowledge Hub ·
Docker Compose mounts volumes when it creates the container, before the container's command starts. So your entrypoint already sees the mounted files, and a mount replaces whatever the image had at that path. Knowing that order explains many confusing development and deployment bugs.
Order of steps when a container starts
When Compose creates a container, Docker:
- creates the container from the image,
- attaches networks,
- attaches volume mounts and bind mounts,
- starts the container command.
Your entrypoint or application starts only after the mount is in place.
A basic bind mount
When the container starts, /app shows the host directory ./src, not whatever the image originally had at /app.
Why image files seem to disappear
A mount hides the image's files at the same path. For example:
- the image contains
/app/package.json, - Compose bind-mounts an empty host directory to
/app, - the container starts and
/applooks empty.
Docker did not delete anything. The mount obscures the image content at that path.
Named volumes versus bind mounts
Bind mount:
Named volume:
A bind mount exposes a host path directly. A named volume is managed by Docker and is usually the better choice for persistent container data.
Named volumes copy image content on first use
If the target path in the image already has files and the named volume is empty on first use, Docker may copy the image's contents into the new volume. Bind mounts do not work this way: the host path becomes the visible content immediately. This matters when images ship default configuration or seed data.
Entrypoints can rely on mounted files
Because mounts are attached before the command runs, startup scripts can count on mounted files being there:
The command reads the mounted /config directory, not image-layer files that used to be there. That is why mounted configuration is common for local development and environment-specific startup.
Live code editing with a bind mount
Bind mounts let you edit code without rebuilding the image:
Host edits show up in the container immediately, which also means host filesystem state controls what the process sees.
Debugging a mount that shows the wrong files
Inspect the mount first:
Check:
- whether the host path exists,
- whether the mounted path is correct,
- whether a bind mount is hiding files you expected from the image.
Most volume bugs are path mistakes or image content being masked. In production, named volumes or image-based config are usually safer than bind mounts.
Related reading
- docker-compose volumes_from equivalent with version 3
- Docker-compose with django could not translate host name db to address Name or service not known
- Docker-Desktop for MAC Kubernetes not creating secrets or token for serviceaccounts
- docker-exec failed cd executable file not found in PATH
- docker-machine behind corporate proxy
- Docker - Can't access Django server
- Docker-ized Consul, Zookeeper and Kafka in Amazon-ECS
- Docker-Swarm, Kubernetes, Mesos Core-OS Fleet
System design interview problems on this topic
- Design a Container Orchestration System: solution and walkthrough
- Design a Code Deployment System: solution and walkthrough
- Design GitHub Actions: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.