简体   繁体   中英

Docker-compose shared volume between containers but that has a path in host

I found that you can use named volumes so two containers can exchange data between them. However, I need to store this names volume in my host computer (the computer which is running the docker images).

So how do I create a voluma that is stored in /media/my_volume that is also shared between containers? I tried to simply binding /media/my_volume to both containers but it ended up in everything being erased when I started the compose again

UPDATE:

version: '3'

services:
  transmission:
    build: ./rpi-transmission
    image: rpi-transmission
    ports:
     - "9091:9091"
     - "51413:51413"
     - "51413:51413/udp"
    volumes:
     - "/home/pi/transmission:/etc/transmission"
     - "/media/external:/home/downloads"
     - "/home/transmission-watch:/home/transmission-watch"
  samba:
    build: ./rpi-samba
    image: rpi-samba
    stdin_open: true
    volumes:
     - "/media/external:/data/share:ro"
  kodi:
    build: ./kodi-rpi
    image: kodi-rpi
    ports:
     - "127.0.0.1:8080:8080"
     - "127.0.0.1:9777:9777/udp"
    devices:
     - "/dev/tty0:/dev/tty0"
     - "/dev/tty2:/dev/tty2"
     - "/dev/fb0:/dev/fb0"
     - "/dev/input:/dev/input"
     - "/dev/snd:/dev/snd"
     - "/dev/vchiq:/dev/vchiq"
    volumes:
     - "/var/run/dbus:/var/run/dbus"
     - "/etc/localtime:/etc/localtime:ro"
     - "/etc/timezone:/etc/timezone:ro"
     - "/home/pi/kodi-rpi/config:/config/kodi"
     - "/home/pi/kodi-rpi/data:/data"

I need to use /media/external on both containers. If I give it a name, I can't mount it to /media/external. If I simply do as it it now, I think samba erases the content of transmission

The content isn't erased from the container though, it is "masked", because the mounted directory is mounted on top of the existing files. The files are still in the container, only not reachable.

However, un-mounting the volume reveals the content that is still in the container (only not accessible, because the volume is mounted over it)

It already has a path on the host inside /var/lib/docker (or whatever directory you has configured as a graph path).

$ docker volume create test
test
$ docker volume inspect -f '{{.Mountpoint}}' test
/var/lib/docker/volumes/test/_data

If you want it to appear on /media/my_volume you can do a bind mount:

mount --bind /var/lib/docker/volumes/test/_data /media/my_volume

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