Bind Mounts

A bind mount maps a folder on your host directly into a container, ideal for live-editing code during development.

Syntaxdocker run -v /host/path:/container/path IMAGE

A bind mount links an exact folder or file on your host machine into the container. Unlike volumes, you control the host location.

Volumes vs bind mounts

  • Volume — managed by Docker, best for persistent app data like databases.
  • Bind mount — points to a host path you choose, best for development and config files.

Live development

Bind mounting your source code means edits on your host appear instantly inside the container, so you can see changes without rebuilding the image.

Example

Example · bash
# Mount the current project folder into the container for live editing
docker run -d \
  -p 3000:3000 \
  -v $(pwd):/app \
  --name dev node:20-alpine \
  sh -c "cd /app && npm run dev"

# Mount a single read-only config file
docker run -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro nginx

When to use it

  • A developer mounts their local source directory into a container so code changes are reflected immediately without rebuilding the image.
  • An ops engineer mounts /etc/ssl/certs into a container to inject host TLS certificates without baking them into the image.
  • A data scientist mounts a local dataset directory into a Jupyter container so notebooks can access large files stored on the host.

More examples

Mount source code for live editing

Mounts the local src/ directory into the container so the running process sees changes immediately.

Example · bash
docker run -d \
  --name dev-server \
  -v $(pwd)/src:/app/src \
  -p 3000:3000 \
  myapp:dev

Mount a config file read-only

Mounts a single host config file as read-only (:ro) so the container cannot accidentally overwrite it.

Example · bash
docker run -d \
  --name nginx \
  -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro \
  -p 80:80 \
  nginx:alpine

Compare bind mount vs volume syntax

Shows both syntaxes for bind mounts; --mount is more verbose but less ambiguous about what is being mounted.

Example · bash
# Old -v syntax (bind mount)
docker run -v /host/path:/container/path myapp

# Modern --mount syntax (more explicit)
docker run --mount type=bind,source=/host/path,target=/container/path myapp

Discussion

  • Be the first to comment on this lesson.