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.
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
:roto 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.
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.
See Bind mounts.
Test yourself
Why mount configuration files with :ro?
It's fasterSo the container can read but not modify themDocker requires itTo 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.