在 Docker 卷中使用 {{.Task.Slot}}

kwi*_*zzn 4 docker docker-compose docker-volume

我想使用以下{{.Task.Slot}}语法将各个卷安装到 Docker 服务的每个副本:

services:
  foo:
    ...
    volumes:
      - type: volume
        source: foo{{.Task.Slot}}
        target: /mnt
    deploy:
      mode: replicated
      replicas: 3

volumes:
  foo1:
    ...
  foo2:
    ...
  foo3:
    ...
Run Code Online (Sandbox Code Playgroud)

但是,Docker 失败并显示:

service foo: undefined volume "foo{{.Task.Slot}}"
Run Code Online (Sandbox Code Playgroud)

看来Go语法不是在source属性中解释的,而是在target属性中,它运行顺利:

services:
  foo:
    ...
    volumes:
      - type: volume
        source: foo1
        target: /mnt{{.Task.Slot}}
Run Code Online (Sandbox Code Playgroud)

但这显然不是我所需要的。

kwi*_*zzn 5

这是正确的方法:

services:
  foo:
    ...
    volumes:
      - foo:/mnt
    deploy:
      mode: replicated
      replicas: 3

volumes:
  foo:
    name: 'foo-{{.Task.Slot}}'
    ...
Run Code Online (Sandbox Code Playgroud)

然后扩展服务将根据需要创建卷。

所有积分均归@larsks 所有。