将不同的Docker容器路径绑定到单个命名卷

时间:2019-08-07 10:29:45

标签: linux docker docker-compose

我正在运行一个dockerized应用程序,该应用程序有两个我想访问的有趣目录。我正在使用命名卷。

branchB

哪个给:

version: "3.7"
services
  myapp:
    volumes:
      - myvol1:/foo/data
      - myvol2:/bar/data
volumes
  myvol1:
  myvol2:

这是一个维护麻烦,因此我宁愿将两者同时使用:

/var/lib/docker/volumes/
  myvol1/_data/
    ...stuff from container's /foo/data
  myvol2/_data/
    ...stuff from container's /bar/data

有可能吗?

1 个答案:

答案 0 :(得分:1)

您可以使用driver_opts将指定的卷挂载到其他位置。下面是一个最小的可行示例供您参考:

第1步:准备文件夹:

shubuntu1@shubuntu1:~$ cd /tmp
shubuntu1@shubuntu1:/tmp$ mkdir bar foo

步骤2:指定driver_opts使用/tmp/foo/tmp/bar作为目标安装位置:

docker-compose.yaml:

version: "3.7"
services:
  myapp:
    image: alpine
    volumes:
      - myvol1:/etc
      - myvol2:/bin
volumes:
  myvol1:
    driver: local
    driver_opts:
      type: 'none'
      o: 'bind'
      device: '/tmp/foo'
  myvol2:
    driver: local
    driver_opts:
      type: 'none'
      o: 'bind'
      device: '/tmp/bar'

执行它:

shubuntu1@shubuntu1:~/test_dir$ docker-compose up -d
Creating network "test_dir_default" with the default driver
Creating volume "test_dir_myvol1" with local driver
Creating volume "test_dir_myvol2" with local driver
Creating test_dir_myapp_1 ... done

第3步:测试一切是否正常:

shubuntu1@shubuntu1:~/test_dir$ ls /tmp/foo
alpine-release  crontabs  hostname  inittab  modprobe.d  motd opt periodic   protocolsservices  ssl  udhcpd.conf
apk fstab hosts issuemodules mtab os-release  profileresolv.conf  shadowsysctl.conf
conf.d  group init.dlogrotate.d  modules-load.d  network  passwd  profile.d  securettyshellssysctl.d
shubuntu1@shubuntu1:~/test_dir$ ls /tmp/bar
arch  chgrp   dd ed   fsync ionicelinux32  makemime  mountpoint  ping   reformime  setprivsu  uname
ash   chmod   df egrepgetoptiostatlinux64  mkdir mpstat  ping6  revsetserial  syncusleep
base64chown   dmesg  falsegrep  ipcalcln   mknod mv  pipe_progress  rm sh tar watch
bbconfig  conspy  dnsdomainname  fatattr  gunzipkbd_mode  loginmktempnetstat printenv   rmdir  sleep  touch   zcat
busybox   cp  dumpkmap   fdflush  gzip  kill  ls   more  niceps run-parts  stat   true
cat   dateecho   fgrephostname  link  lzop mount pidof   pwdsedstty   umount

您可以看到/etc/bin在高山地区已经可以在/tmp/foo/tmp/bar中看到。

相关问题