在 docker compose 中使用环境变量作为卷名 [英] Using environment variable for volume name in docker compose

查看:24
本文介绍了在 docker compose 中使用环境变量作为卷名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 docker compose 3.3 版并想使用环境变量来定义卷名称.我查看了一个相关的问题,但这似乎是很老.3.2 支持长语法,有没有办法实现?这是我在 docker compose 文件中尝试的内容:

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 不允许附加属性 ('${VOLUME_NAME}'没想到)

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

任何帮助将不胜感激.

推荐答案

这是预期的行为 - Compose 仅​​对值进行变量插值,而不对键进行变量插值.请参阅此处.

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

您可以看到创建的卷:

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

这篇关于在 docker compose 中使用环境变量作为卷名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆