在Docker卷中使用{{.Task.Slot}} [英] Using {{.Task.Slot}} in Docker volumes

查看:149
本文介绍了在Docker卷中使用{{.Task.Slot}}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to mount individual volumes to each replica of my Docker service using the {{.Task.Slot}} syntax:

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

volumes:
  foo1:
    ...
  foo2:
    ...
  foo3:
    ...

但是,Docker失败了:

However, Docker fails with:

service foo: undefined volume "foo{{.Task.Slot}}"

Go语法似乎没有在 source 属性中解释,但在 target 属性中却解释得很顺利:

It seems that the Go syntax is not interpreted in the source property but in the target property, it works smoothly:

services:
  foo:
    ...
    volumes:
      - type: volume
        source: foo1
        target: /mnt{{.Task.Slot}}

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

But that's obviously not what I need.

推荐答案

这是正确的方法:

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

volumes:
  foo:
    name: 'foo-{{.Task.Slot}}'
    ...

扩展服务后,将根据需要创建卷.

Scaling the service will then create the volume(s) as needed.

所有学分归@larsks.

All credits go to @larsks.

这篇关于在Docker卷中使用{{.Task.Slot}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆