【问题标题】:Making a global bind mapping in docker-compose?在 docker-compose 中进行全局绑定映射?
【发布时间】:2021-05-02 07:42:28
【问题描述】:

我正在 Windows Containers 中对一堆 Windows 应用进行 dockerizing。

我的所有应用都需要相同的映射,这是我的配置的简短 sn-p:

version: '3.9'
services:


  shell0:
    build:
      target: myimage
      context: .
    image: 'salimfadhley/myimage:latest'
    entrypoint: c:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe
    working_dir: "c:\\"
    volumes:
      - type: "bind"
        source: "x:"
        target: "x:"


volumes: # THIS BIT DOESN'T WORK!
  xdrive:
    source: "x:"

“xdrive”是我所有应用程序都使用的网络驱动器共享。每个进程都需要访问“xdrve”,这就是为什么我将它绑定到每个服务中。

我通过在这个 Docker Compose 文件中重复每个服务的配置来做到这一点。会有很多。这将使我的 docker-compose 文件非常重复。

有没有办法只定义一次“xdrive”,例如在全局“volumes”部分?我希望能够为每个服务做这样的事情:

...service
  volumes:
   - xdrive: "x:"

可以吗?全局定义绑定挂载的语法是什么?

【问题讨论】:

    标签: windows docker-compose


    【解决方案1】:

    你可以用 YAML 语法解决这个问题:

    version: "3.5"
    services:
      one:
        image: busybox
        command: ls /foo
        volumes:
        - &volume-foo
          type: bind
          source: .
          target: /foo
    
      two:
        image: busybox
        command: ls /foo
        volumes:
        - *volume-foo
    

    &volume-foo 是锚点,*volume-foo 是别名。别名重复在相应锚点之后声明的内容,在本例中是数组的单个对象。解析后是这样的:

    version: "3.5"
    services: 
      one: 
        image: busybox
        command: "ls /foo"
        volumes: 
          - 
            source: "."
            target: /foo
            type: bind
    
      two: 
        image: busybox
        command: "ls /foo"
        volumes: 
          - 
            source: "."
            target: /foo
            type: bind
    

    【讨论】:

    • 这肯定会消除大部分重复!有没有办法全局定义绑定映射?
    • @SalimFadhley volumes 部分用于创建卷。 AFAIK 当您绑定安装某些内容时,您不会创建卷。我发布的解决方案是您所需要的,但您可能需要更多示例。使用 YAML 语法,您不仅可以引用一个对象,还可以合并多个对象并混合它们的属性(例如覆盖 target。也可以在顶级键 X-something 下声明公共键,再次使用锚点和别名.
    • @SalimFadhley 请参阅此答案中的示例 1 stackoverflow.com/questions/36283908/…
    • 感谢@anemyte 的帮助。是的,我想我误解了全局卷选项的含义。我们没有创建新卷,我们只是引用系统上已经存在的内容,因此这种方法可以正常工作。
    猜你喜欢
    • 2017-05-11
    • 2019-08-23
    • 2022-10-17
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    相关资源
    最近更新 更多