简体   繁体   中英

mounting volume into docker container

How would i mount a path from host into the container without overriding the already existing contains at the same mounted path.

Confused? let me give an example:

Built a docker image with WORKDIR as /app and baked it with file /app/config1 . Now, i will be adding few config files in runtime at hosts ~/ and want these in the container at same /app path

When i do docker run -v ~/:/app example It seems to be deleting the baked config1 file

Any pointers on how to solve this use-case? Ofcourse, other then mounting at different path.

TIA!

Docker is implementing the Linux mount syscall to perform the bind mounts, and that always hides the underlying filesystem, like most other mount commands in Linux, there's no flag or option to change the behavior of Linux. An overlay filesystem would merge several directories together, but you can't point to an overlay filesystem as the source for an overlay filesystem so that's out. That leaves two options I can think of.

Named volumes in docker will get initialized when empty with the contents of the image filesystem, so if you're not worried about rebuilding the image with new config files using configs from an older volume, you can use those instead. And if you need that volume to mount back to a host path, you can pass flags to the local volume driver to mount a different host path than the default named volume path docker uses. Here's three different ways to do that depending on how you run your containers:

  # create the volume in advance
  $ docker volume create --driver local \
      --opt type=none \
      --opt device=/home/user/test \
      --opt o=bind \
      test_vol

  # create on the fly with --mount
  $ docker run -it --rm \
    --mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=/home/user/test \
    foo

  # inside a docker-compose file
  ...
  volumes:
    bind-test:
      driver: local
      driver_opts:
        type: none
        o: bind
        device: /home/user/test
  ...

Otherwise you can modify your image an entrypoint to save off the config files to a cached location during the image build and then copy them back to the target directory when the container starts in the entrypoint. I have an example of that in my base image scripts . This has the advantage over the named volume to a bind mount in that it works regardless of how the volume is mounted and can update existing volumes that already have contents.

Bind mounting directory from host will override content of that folder. If you only need to mount few config files you could have a separate bind mount for each file, eg -v ~/config2:/app/config2 .

Another approach would be to mount the directory to different location and add logic to the container for collecting the config files from multiple locations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM