Search code examples
dockerdocker-composedocker-volume

Using {{.Task.Slot}} in Docker volumes


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:
    ...

However, Docker fails with:

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

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.


Solution

  • This is the correct way to do it:

    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.

    All credits go to @larsks.