在Docker中使用卷名称的环境变量组成
问题描述:
我使用的是docker compose版本3.3,并且想要使用环境变量来定义卷名称。我看了一下相关的question,但那似乎很古老。在3.2中支持长语法,有没有办法实现这一点? 以下是我在我的搬运工撰写文件的尝试:在Docker中使用卷名称的环境变量组成
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}:
显然,作为卷名未在按键取代这句法不工作,并引发以下错误:
volumes value Additional properties are not allowed ('${VOLUME_NAME}' was unexpected)
任何帮助将非常感谢。
答
这是预期的行为 - 撰写只能在值中进行变量插值,而不是键值。见here。
在项目中,我使用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}
,但首先我需要手动创建外部卷,E。摹:
export VER=1.1 && docker volume create --name code-$VER
你可以看到创建的卷:
docker volume ls
DRIVER VOLUME NAME
local code-1.0
local code-1.1
而在此之后,使用部署服务:
env $(cat .env | grep ^[A-Z] | xargs) docker stack deploy --with-registry-auth --compose-file docker-compose.yml MY_STACK