Docker Bind Mounts

dockerd(8) uses bind mounts to mount a host file or directory into a container.

Compare to Volumes.


Mounts

The syntax for mounting a file or directory into a container is:

docker run --detach --name=my-nginx \
  --mount type=bind,src=/absolute/path/to/web/root,dst=/usr/share/nginx/html,readonly \
  --mount type=bind,src=/absolute/path/to/app/binary,dst=/app,readonly \
  nginx:latest

Note that the host file or directory must exist; an error will be raised otherwise.

When a bind mount targets an existing and non-empty directory in a container, the contents of that directory are obscured.


Volumes

An alternative syntax for mounting a file or directory is:

docker run --detach --name=my-nginx \
  --volume /absolute/path/to/web/root:/usr/share/nginx/html:ro \
  --volume /absolute/path/to/app/binary:/app:ro \
  nginx:latest

This syntax is much shorter but...

Nonetheless the --volume option is frequently used in documentation.


Docker Compose

To use a bind mount with Docker Compose, try:

services:
  web:
    image: nginx
    volumes:
      - type: bind
        source: /absolute/path/to/web/root
        target: /usr/share/nginx/html
        read_only: true


CategoryRicottone