Skip to main content

Volume Mount

A volume mount connects storage from outside a container into its filesystem at a chosen path. Docker supports three mount types: named volumes, bind mounts, and tmpfs.

Three mount types

  • Named volume: Docker-managed storage that persists after the container is removed. Best for production data.
  • Bind mount: a specific host file or directory mapped into the container. Best for development and injecting config.
  • tmpfs mount: in-memory storage that disappears when the container stops. Best for sensitive or scratch data.
Bash
# Named volume
docker run -d --name postgres -v postgres-data:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=devpassword postgres:16
# Bind mount for development, source code live-reloaded
docker run -d --name dev-api -p 3000:3000 -v "$(pwd)/src:/app/src" node:20-alpine \
node /app/src/server.js
# Read-only bind mount for config
docker run -d --name nginx -v "$(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro" nginx:1.27
# tmpfs
docker run -d --tmpfs /tmp payment-api:v3.1.0

The clearer --mount syntax

Bash
docker run -d \
--mount type=volume,source=postgres-data,target=/var/lib/postgresql/data \
postgres:16

Each option is named, so source and target cannot be confused. A bind mount with --mount also fails if the host path is missing, while -v creates a root-owned directory silently.

In Compose

YAML
services:
api:
image: payment-api:v3.1.0
volumes:
- payment-data:/app/data
- ./config/prod.json:/app/config/prod.json:ro
- type: tmpfs
target: /tmp
volumes:
payment-data:

Inspect mounts

Bash
docker inspect postgres \
--format '{{range .Mounts}}{{.Type}} {{.Source}} -> {{.Destination}}{{println}}{{end}}'

Choosing a type

Use case Mount type
PostgreSQL or Redis data Named volume
Live code reload in development Bind mount
Config injection Read-only bind mount
Temporary sensitive files tmpfs
Common Mistake

Shipping a development Compose file with bind-mounted source into production. It hides the code baked into the image and defeats immutable images.

Security

Never bind-mount sensitive host paths such as /etc, /root, or SSH key folders into production containers. Use :ro whenever the container only reads.

Frequently Asked Questions

How do I choose between a named volume and a bind mount?

Use a named volume for application data that must persist and be portable across hosts, such as databases. Use a bind mount when you want a specific host path to appear in the container, mainly for development and config files.

What is a common production mistake with mounts?

Reusing a development file with bind-mounted code in production, which can silently replace the image's code with host files. Another is permission mismatch, where a non-root container user cannot write to a bind-mounted host directory.

What does the :ro suffix do?

It mounts the path read-only inside the container, so the process cannot modify the host files or volume data, which is a good default for configuration.

Found a mistake, an outdated step or a broken link?