Differences between revisions 2 and 3
Revision 2 as of 2023-04-05 14:41:26
Size: 1633
Comment:
Revision 3 as of 2023-04-05 14:42:17
Size: 1658
Comment:
Deletions are marked like this. Additions are marked like this.
Line 66: Line 66:
        read_only: true

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...

  • the option is poorly named, as it can easily be confused with Volumes, which are a separate concept

  • if the host file or directory does not exist, dockerd(8) creates it as a directory on the host

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

Docker/BindMounts (last edited 2023-04-06 14:30:27 by DominicRicottone)