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

查看:136
本文介绍了在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:


体积值不允许使用其他属性('$ {VOLUME_NAME}'
是意外的)

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

任何帮助将不胜感激。

推荐答案

这是预期的行为-撰写仅对值(而不是键)进行变量插值。参见此处

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}

但首先我需要crea手动设置外部音量,e。 g。:

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

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

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