【问题标题】:Bind different docker container paths to single named volume将不同的 docker 容器路径绑定到单个命名卷
【发布时间】:2019-08-07 10:29:45
【问题描述】:

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

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

维护起来很麻烦,所以我宁愿两个都用一个卷:

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

这可能吗?

【问题讨论】:

    标签: linux docker docker-compose


    【解决方案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
    

    您可以在/tmp/foo/tmp/bar 中看到/etc/bin

    【讨论】:

    • 谢谢!然而,这仍然使用两卷。我很想知道如何用一卷来做到这一点。
    • 我猜docker要区分myvol:/foo/datamyvol:/bar/data并不容易。作为一种常用工具,似乎没有机会了解/foo//bar/ 上的差异。不管怎样,你可以等着看其他人是否能给出好的建议,祝你好运。
    • 如果不可能,我会将此标记为答案...再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 2019-12-21
    • 2016-07-27
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    相关资源
    最近更新 更多