I am using docker compose version 3.3 and want to use environment variable to define the volume name. I looked at a related question, but that seems to be quite old. With long syntax supported in 3.2, is there a way to achieve that? Here is what I tried in my docker compose file:
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 volume name is not substituted in the keys and throws the following error:
volumes value Additional properties are not allowed ('${VOLUME_NAME}' was unexpected)
Any help will be much appreciated.
This is expected behavior - Compose only does variable interpolation in values, not keys. See here.
In my project I use 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 create external volume manually, e. g.:
export VER=1.1 && docker volume create --name code-$VER
You can see created volume:
docker volume ls
DRIVER VOLUME NAME
local code-1.0
local code-1.1
And after that, deploy services using:
env $(cat .env | grep ^[A-Z] | xargs) docker stack deploy --with-registry-auth --compose-file docker-compose.yml MY_STACK