Using environment variable for volume name in docker compose

I am using docker compose version 3.3 and want to use an environment variable to determine the volume name. I looked at a related question , but that seems pretty old. With the long syntax supported in 3.2, is there a way to achieve this? Here's what I tried in my dockerfile:

version: '3.3'
services:
  target:
    image: "my-registry/my-image:${IMAGE_TAG}"
    volumes:
        - type: volume
          source: ${VOLUME_NAME}
          target: /data
    ports:
     - "${TOMCAT_PORT}:8080"

volumes:
  ${VOLUME_NAME}:

      

Obviously, this syntax does not work as the volume name is not replaced in the keys and generates the following error:

Volume value Additional properties are not allowed ('$ {VOLUME_NAME}' was unexpected)

Any help would be much appreciated.

+3


source to share


1 answer


This is expected behavior. Make up only variable interpolation in values, not keys. See here .

In my project I am using an external structure :

version: '3.1'
services:
### Code from branch develop ###
  applications:
    image: registry.gitlab.lc:5000/develop/ed/develop.sources:latest
    volumes:
      - developcode:/var/www/develop
    deploy:
      replicas: 1
      update_config:
        parallelism: 1
        delay: 5s
      restart_policy:
        condition: on-failure
      placement:
        constraints: [node.role == manager]
### PHP-FPM ###
  php-fpm:
    image: registry.gitlab.lc:5000/develop/ed/php-fpm-ed-sq:latest
    volumes:
      - developcode:/var/www/develop
    expose:
      - "9000"
    deploy:
      replicas: 2
      update_config:
        parallelism: 1
        delay: 5s
      restart_policy:
        condition: on-failure
      placement:
        constraints: [node.role == manager]
    logging:
      driver: gelf
      options:
        gelf-address: "udp://${GRAYLOG_ADDR}:12201"
        tag: "php-fpm"
### Nginx ###
  nginx:
    image: registry.gitlab.lc:5000/develop/ed/nginx-ed-sq:staging
    volumes:
      - developcode:/var/www/develop
    ports:
      - "80:80"
      - "443:443"
    deploy:
      replicas: 2
      update_config:
        parallelism: 1
        delay: 5s
      restart_policy:
        condition: on-failure
      placement:
        constraints: [node.role == manager]
### Volumes Setup ###
volumes:
  developcode:
    external:
      name: code-${VER}

      

but first of all I need to create the outer volume manually, eg. g :.

export VER=1.1 && docker volume create --name code-$VER

      



You can see the volume created:

docker volume ls
DRIVER              VOLUME NAME
local               code-1.0
local               code-1.1

      

And after that, deploy the services using:

env $(cat .env | grep ^[A-Z] | xargs) docker stack deploy --with-registry-auth --compose-file docker-compose.yml MY_STACK

      

0


source







All Articles