Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixing named volumes and bind mounting in Docker?

How does mixing named volumes and bind mounts work? Using the following setup will the paths that are being bind mounted still be available inside the bind mount as they exist in the bind mount? /var/www/html/wp-content/uploads

Using a separate container which I attach to the named volumes, seems to show that it is not the case as those paths are completely empty from the view of the separate container. Is there a way for this to work in a sense?

    volumes:
      - "wordpress:/var/www/html"
      - "./wordpress/uploads:/var/www/html/wp-content/uploads"
      - "./wordpress/plugins:/var/www/html/wp-content/plugins"
      - "./wordpress/themes:/var/www/html/wp-content/themes"
like image 839
PeaceBringer Avatar asked Jan 15 '18 10:01

PeaceBringer


People also ask

What are two differences between a Docker volume and a bind mount?

The most notable difference between the two options is that --mount is more verbose and explicit, whereas -v is more of a shorthand for --mount . It combines all the options you pass to --mount into one field. On the surface, both commands create a PostgreSQL container and set a volume to persist data.

When should I use BIND volume vs mount?

The main difference a bind mount has from a volume is that since it can exist anywhere on the host filesystem, processes outside of Docker can also modify it. Volumes: Volumes are the preferred way to store persistent data Docker containers create or use. The host filesystem also stores volumes, similar to bind mounts.

What is Docker volume bind mount?

Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its absolute path on the host machine.

Can two Docker containers mount same volume?

Multiple containers can run with the same volume when they need access to shared data. Docker creates a local volume by default.


1 Answers

Host volumes: For a host volume, defined with a path in your docker compose file like:

volumes:
  - "./wordpress/uploads:/var/www/html/wp-content/uploads"

you will not receive any initialization of the host directory from the image contents. This is by design.


Named volumes: You can define a named volume that maps back to a local directory:

version: "2"

services:
  your-service:
    volumes:
      - uploads:/var/www/html/wp-content/uploads

volumes:
  uploads:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /path/on/host/to/wordpress/uploads

This will provide the initialization properties of a named volume. When your host directory is empty empty, on container creation docker will copy the contents of the image at /var/www/html/wp-content/uploads to /path/on/host/to/wordpress/uploads.


Nested mounts with Docker: If you have multiple nested volume mounts, docker will still copy from the image directory contents, not from a parent volume.

Here's an example of that initialization. Starting with the filesystem:

testvol/
  data-image/
    sub-dir/
      from-image
  data-submount/
  Dockerfile
  docker-compose.yml

The Dockerfile contains:

FROM busybox
COPY data-image/ /data

The docker-compose.yml contains:

version: "2"

services:
  test:
    build: .
    image: test-vol
    command: find /data
    volumes:
      - data:/data
      - subdir:/data/sub-dir

volumes:
  data:
  subdir:
    driver: local
    driver_opts:
      type: none
      o: bind
      device: /path/on/host/test-vol/data-submount

And the named volume has been initialized:

$ docker run -it --rm -v testvol_data:/data busybox find /data
/data
/data/sub-dir
/data/sub-dir/from-named-vol

Running the test shows the copy comes from-image rather than from-named-vol:

$ docker-compose -f docker-compose.bind.yml up
...
Attaching to testvol_test_1
test_1  | /data
test_1  | /data/sub-dir
test_1  | /data/sub-dir/from-image
testvol_test_1 exited with code 0

And docker has copied this to the host filesystem:

$ ls -l data-submount/
total 0
-rw-r--r-- 1 root root 0 Jan 15 08:08 from-image

Nested mounts in Linux: From your question, there appears to be some confusion on how a mount itself works in Linux. Each volume mount runs in the container's mount namespace. This namespace gives the container its own view of a filesystem tree. When you mount a volume into that tree, you do not modify the contents from the parent filesystem, it simply covers up the contents of the parent at that location. All changes happen directly in that newly mounted directory, and if you were to unmount it, the parent directories will then be visible in their original state.

Therefore, if you mount two nested directories in one container, e.g. /data and /data/a, and then mount /data in a second container, you will not see /data/a from your first container in the second container, only the contents of /data will be there, including any folders that were mounted over top of.

like image 155
BMitch Avatar answered Oct 22 '22 05:10

BMitch