docker-compose
volume mount
container management
devops
virtualization

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

yaml
1services:
2  app:
3    image: python:3.12-slim
4    working_dir: /app
5    command: python main.py
6    volumes:
7      - ./src:/app

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 /app looks empty.

Docker did not delete anything. The mount obscures the image content at that path.

Named volumes versus bind mounts

Bind mount:

yaml
volumes:
  - ./data:/var/lib/app

Named volume:

yaml
1services:
2  db:
3    image: postgres:16
4    volumes:
5      - db_data:/var/lib/postgresql/data
6
7volumes:
8  db_data:

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:

yaml
1services:
2  app:
3    image: alpine:3.20
4    command: sh -c "ls -la /config && cat /config/app.conf"
5    volumes:
6      - ./config:/config

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:

yaml
1services:
2  web:
3    image: node:20
4    working_dir: /usr/src/app
5    command: npm run dev
6    volumes:
7      - ./:/usr/src/app

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:

bash
docker compose up -d
docker compose exec app ls -la /app
docker inspect $(docker compose ps -q app)

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.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design