Docker Tutorial · Data and Networking · Lesson 16 of 26

Bind Mounts for Development

Mount source code or configuration from the host into a container, read-only where possible.

Create a free account to save this page, take notes and track your progress. Sign in

Bind mounts

-v "$PWD/src:/app/src" (or --mount type=bind,source=...,target=...) makes a host directory appear inside the container. Edits on the host are visible immediately, which is ideal for development with auto-reload.

  • Add :ro to mount read-only when the container should not change the files (configuration).
  • File ownership is shared with the host: a container running as root creates root-owned files in your project; run as your user ID in development (--user "$(id -u):$(id -g)").
  • Prefer volumes for data in production; bind mounts tie the container to one host's paths.
A read-only configuration mount
mkdir -p config && echo "feature_x=on" > config/app.conf
docker run --rm -v "$PWD/config:/etc/app:ro" alpine:3.24 sh -c 'cat /etc/app/app.conf; echo changed >> /etc/app/app.conf 2>&1 || echo "write refused: mounted read-only"'
echo "feature_x=off" > config/app.conf
docker run --rm -v "$PWD/config:/etc/app:ro" alpine:3.24 cat /etc/app/app.conf

Output

feature_x=on
write refused: mounted read-only
feature_x=off

Run this on your computer — see “Get Started” for setup.

Test yourself

Why mount configuration files with :ro?

  1. It's faster
  2. So the container can read but not modify them
  3. Docker requires it
  4. To hide them from the host
Show answer

Answer: So the container can read but not modify them

Read-only mounts limit what a compromised or buggy container can change.

Was this page helpful?