简体   繁体   中英

Docker volume mounts with template strings in Docker Compose

Is there a way to use Docker templates in a docker-compose.yml file? I created an image to setup an automatic rabbitmq cluster. The service create command looks like this:

docker service create \
    --name="rabbitmq" \
    --network="rabbitmq" \
    --publish 15672:15672 \
    --update-delay 2m \
    --hostname="{{.Service.Name}}-{{.Task.Slot}}" \
    --secret source=key.rabbitmq.pem,target=key.pem \
    --secret source=cert.rabbitmq.pem,target=cert.pem \
    --mount type=volume,volume-driver=cloudstor:azure,source={{.Service.Name}}-{{.Task.Slot}},destination=/var/lib/rabbitmq/data \
    -e SERVICE_NAME="{{.Service.Name}}" \
    -e RABBITMQ_ERLANG_COOKIE=xxx \
    -e RABBITMQ_MNESIA_DIR=/var/lib/rabbitmq/data \
    --with-registry-auth \
    registry.tld/rabbitmq

The main point is the mount , which I want to configure inside a docker-compose.yml file:

rabbitmq:
        image: "registry.tld/rabbitmq"
        ports: ["15672:15672"]
        hostname: "{{.Service.Name}}-{{.Task.Slot}}"
        secrets:
            - source: key.rabbitmq.pem
              target: key.pem
            - source: cert.rabbitmq.pem
              target: cert.pem
        environment:
            - SERVICE_NAME: "{{.Service.Name}}"
            - RABBITMQ_ERLANG_COOKIE: "cluster"
            - RABBITMQ_MNESIA_DIR: "/var/lib/rabbitmq/data"
        volumes: 
            - type: volume
              source: "{{.Service.Name}}-{{.Task.Slot}}"
              target: "/var/lib/rabbitmq/data"

I'm not even sure if the normal template strings work, but is there a way to define the volumes even?

Because this obviously doesn't work:

volumes:
    "{{.Service.Name}}-{{.Task.Slot}}": { driver: "cloudstor:azure" }

Thanks in advice!

Try defining in your top level volume something like this:

volumes:
  volume_data:
    name: '{{.Service.Name}}_DataVol-{{.Task.Slot}}'

Then in your service just mount the volume:

volumes:
      - "data_volume:/my_app/data"

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM