如何在 docker compose 中使用环境变量

How to use environment variables in docker compose

我希望能够在 docker-compose.yml 中使用环境变量,并在 docker-compose up 时传入值。这是例子。

我今天使用基本的 docker 运行 命令执行此操作,该命令包含在我自己的脚本中。 有没有一种方法可以通过撰写来实现它,而无需任何此类 bash 包装器?

proxy:
  hostname: $hostname
  volumes:
    - /mnt/data/logs/$hostname:/logs
    - /mnt/data/$hostname:/data

您还不能……。但这是另一种选择,像 docker-composer.yml 生成器一样思考:

https://gist.github.com/Vad1mo/9ab63f28239515d4dafd

基本上是一个 shell 脚本,它将替换您的变量。您还可以使用 Grunt 任务在 CI 流程结束时构建 docker 撰写文件。

我为此创建了一个简单的 bash 脚本,它只是意味着 运行 在使用前将其放在您的文件中: https://github.com/antonosmond/subber

基本上只需使用双花括号创建您的撰写文件来表示环境变量,例如:

app:
    build: "{{APP_PATH}}"
ports:
    - "{{APP_PORT_MAP}}"

双花括号中的任何内容都将替换为同名的环境变量,因此如果我设置了以下环境变量:

APP_PATH=~/my_app/build
APP_PORT_MAP=5000:5000

在 运行 subber docker-compose.yml 上生成的文件如下所示:

app:
    build: "~/my_app/build"
ports:
    - "5000:5000"

据我所知,这是一项正在进行的工作。他们想做,但还没有发布。请参阅@Andy 提到的 1377 (the "new" 495

我最终实施了@Thomas 提出的 "generate .yml as part of CI" 方法。

DOCKER解决方案:

看起来 docker-compose 1.5+ 启用了变量替换:https://github.com/docker/compose/releases

最新的 Docker Compose 允许您从您的 compose 文件访问环境变量。所以你可以获取你的环境变量,然后 运行 像这样组成:

set -a
source .my-env
docker-compose up -d

然后你可以使用${VARIABLE}引用docker-compose.yml中的变量,像这样:

db:
  image: "postgres:${POSTGRES_VERSION}"

这里是文档中的更多信息,取自此处:https://docs.docker.com/compose/compose-file/#variable-substitution

When you run docker-compose up with this configuration, Compose looks for the POSTGRES_VERSION environment variable in the shell and substitutes its value in. For this example, Compose resolves the image to postgres:9.3 before running the configuration.

If an environment variable is not set, Compose substitutes with an empty string. In the example above, if POSTGRES_VERSION is not set, the value for the image option is postgres:.

Both $VARIABLE and ${VARIABLE} syntax are supported. Extended shell-style features, such as ${VARIABLE-default} and ${VARIABLE/foo/bar}, are not supported.

If you need to put a literal dollar sign in a configuration value, use a double dollar sign ($$).

而且我相信此拉取请求中添加了此功能:https://github.com/docker/compose/pull/1765

BASH解决方案:

我注意到人们对 Docker 的环境变量支持有疑问。与其在 Docker 中处理环境变量,不如让我们回到基础,比如 bash!这是使用 bash 脚本和 .env 文件的更灵活的方法。

.env 文件示例:

EXAMPLE_URL=http://example.com
# Note that the variable below is commented out and will not be used:
# EXAMPLE_URL=http://example2.com 
SECRET_KEY=ABDFWEDFSADFWWEFSFSDFM

# You can even define the compose file in an env variable like so:
COMPOSE_CONFIG=my-compose-file.yml
# You can define other compose files, and just comment them out
# when not needed:
# COMPOSE_CONFIG=another-compose-file.yml

然后 运行 这个 bash 脚本在同一目录中,应该正确部署所有内容:

#!/bin/bash

docker rm -f `docker ps -aq -f name=myproject_*`
set -a
source .env
cat ${COMPOSE_CONFIG} | envsubst | docker-compose -f - -p "myproject" up -d

只需使用通常的 bash 语法在您的 compose 文件中引用您的 env 变量(即 ${SECRET_KEY}.env 文件中插入 SECRET_KEY)。

请注意 COMPOSE_CONFIG 在我的 .env 文件中定义并在我的 bash 脚本中使用,但您可以轻松地将 {$COMPOSE_CONFIG} 替换为 my-compose-file.yml 在 bash 脚本中。

另请注意,我通过使用 "myproject" 前缀命名所有容器来标记此部署。您可以使用任何您想要的名称,但它有助于识别您的容器,以便您以后可以轻松引用它们。假设您的容器是无状态的,因为它们应该是无状态的,此脚本将根据您的 .env 文件参数和您撰写的 YAML 文件快速删除和重新部署您的容器。

更新 由于这个答案似乎很受欢迎,我写了一篇博客 post 更深入地描述了我的 Docker 部署工作流程:http://lukeswart.net/2016/03/lets-deploy-part-1/ 当您向部署配置添加更多复杂性时,这可能会有所帮助,例如nginx 配置、LetsEncrypt 证书和链接的容器。

  1. 创建一个 template.yml,这是你的 docker-compose.yml 和环境变量。
  2. 假设您的环境变量在一个文件中'env.sh'
  3. 将下面的代码放在一个 sh 文件中 运行 它。

source env.sh; rm -rf docker-compose.yml; envsubst < "template.yml" > "docker-compose.yml";

将使用正确的环境变量值生成一个新文件docker-compose.yml

示例 template.yml 文件:

oracledb:
        image: ${ORACLE_DB_IMAGE}
        privileged: true
        cpuset: "0"
        ports:
                - "${ORACLE_DB_PORT}:${ORACLE_DB_PORT}"
        command: /bin/sh -c "chmod 777 /tmp/start; /tmp/start"
        container_name: ${ORACLE_DB_CONTAINER_NAME}

示例 env.sh 文件:

#!/bin/bash 
export ORACLE_DB_IMAGE=<image-name> 
export ORACLE_DB_PORT=<port to be exposed> 
export ORACLE_DB_CONTAINER_NAME=ORACLE_DB_SERVER

将 env 添加到 .env 文件

比如

VERSION=1.0.0

然后保存到deploy.sh

INPUTFILE=docker-compose.yml
RESULT_NAME=docker-compose.product.yml
NAME=test

prepare() {
        local inFile=$(pwd)/$INPUTFILE
        local outFile=$(pwd)/$RESULT_NAME
        cp $inFile $outFile
        while read -r line; do
            OLD_IFS="$IFS"
            IFS="="
            pair=($line)
            IFS="$OLD_IFS"
               sed -i -e "s/${${pair[0]}}/${pair[1]}/g" $outFile
            done <.env
     }
       
deploy() {
        docker stack deploy -c $outFile $NAME
}

        
prepare
deploy
    

最好的方法是在docker-compose.yml文件外指定环境变量。您可以使用 env_file 设置,并在同一行中定义您的环境文件。然后再次 docker-compose up 应该使用新的环境变量重新创建容器。

这是我的 docker-compose.yml 的样子:

services:
  web:
    env_file: variables.env

Note: docker-compose expects each line in an env file to be in VAR=VAL format. Avoid using export inside the .env file. Also, the .env file should be placed in the folder where the docker-compose command is executed.

使用卷环境变量时,您需要:

  1. 在包含 docker-compose.yaml 文件的同一文件夹中创建 .env 文件

  2. .env 文件中声明变量:

    HOSTNAME=your_hostname
    
  3. docker-compose.yaml 文件中将 $hostname 更改为 ${HOSTNAME}

    proxy:
      hostname: ${HOSTNAME}
      volumes:
        - /mnt/data/logs/${HOSTNAME}:/logs
        - /mnt/data/${HOSTNAME}:/data
    

当然,您可以在每次构建时动态执行此操作,例如:

echo "HOSTNAME=your_hostname" > .env && sudo docker-compose up

似乎 docker-compose 现在对 default environment variables in file 有原生支持。

您需要做的就是在名为 .env 的文件中声明您的变量,它们将在 docker-compose.yml.

中可用

例如,.env 文件的内容为:

MY_SECRET_KEY=SOME_SECRET
IMAGE_NAME=docker_image

您可以在 docker-compose.yml 中访问您的变量或将它们转发到容器中:

my-service:
  image: ${IMAGE_NAME}
  environment:
    MY_SECRET_KEY: ${MY_SECRET_KEY}

使用 .env 文件定义 docker-compse.yml 中的动态值。无论是端口还是任何其他值。

示例 docker-撰写:

testcore.web:
       image: xxxxxxxxxxxxxxx.dkr.ecr.ap-northeast-2.amazonaws.com/testcore:latest
       volumes: 
            - c:/logs:c:/logs
       ports:
            - ${TEST_CORE_PORT}:80
       environment:
            - CONSUL_URL=http://${CONSUL_IP}:8500 
            - HOST=${HOST_ADDRESS}:${TEST_CORE_PORT}

在 .env 文件中,您可以定义这些变量的值:

CONSUL_IP=172.31.28.151
HOST_ADDRESS=172.31.16.221
TEST_CORE_PORT=10002

以下适用于docker-compose 3.x 在容器内设置环境变量

method - 1 Straight method

web:
  environment:
    - DEBUG=1
      POSTGRES_PASSWORD: 'postgres'
      POSTGRES_USER: 'postgres'

method - 2 The “.env” file

在与 docker-compose.yml

相同的位置创建一个 .env 文件
$ cat .env
TAG=v1.5
POSTGRES_PASSWORD: 'postgres'

你的撰写文件会像

$ cat docker-compose.yml
version: '3'
services:
  web:
    image: "webapp:${TAG}"
    postgres_password: "${POSTGRES_PASSWORD}"

source

env SOME_VAR="I am some var" OTHER_VAR="I am other var" docker stack deploy -c docker-compose.yml

使用3.6版本:

version: "3.6"
services:
  one:
    image: "nginx:alpine"
    environment:
      foo: "bar"
      SOME_VAR:
      baz: "${OTHER_VAR}"
    labels:
      some-label: "$SOME_VAR"
  two:
    image: "nginx:alpine"
    environment:
      hello: "world"
      world: "${SOME_VAR}"
    labels:
      some-label: "$OTHER_VAR"

我从这个 link 得到它 https://github.com/docker/cli/issues/939

从 1.25.4 开始,docker-compose 支持选项 --env-file,使您能够指定包含变量的文件。

你的应该是这样的:

hostname=my-host-name

和命令:

docker-compose --env-file /path/to/my-env-file config

我最终在 deploy.sh 脚本中使用“sed”来完成此操作,因为我的要求略有不同,因为 docker-compose 被 Terraform 调用:Passing Variables to Docker Compose via a Terraform script for an Azure App Service

eval "sed -i 's/MY_VERSION/$VERSION/' ../docker-compose.yaml"

cat ../docker-compose.yaml

terraform init 
terraform apply -auto-approve \
    -var "app_version=$VERSION" \
    -var "client_id=$ARM_CLIENT_ID" \
    -var "client_secret=$ARM_CLIENT_SECRET" \
    -var "tenant_id=$ARM_TENANT_ID" \
    -var "subscription_id=$ARM_SUBSCRIPTION_ID"

eval "sed -i 's/$VERSION/MY_VERSION/' ../docker-compose.yaml"

要添加环境变量,您可以将 env_file(我们称之为 var.env)定义为:

ENV_A=A
ENV_B=B

并将其添加到 docker 撰写清单服务。此外,您可以直接使用 environment.

定义环境变量

例如 docker-compose.yaml:

version: '3.8'
services:
  myservice:
    build:
      context: .
      dockerfile: ./docker/Dockerfile.myservice
    image: myself/myservice
    env_file:
     - ./var.env
    environment:
     - VAR_C=C
     - VAR_D=D
    volumes:
     - $HOME/myfolder:/myfolder
    ports:
     - "5000:5000"

请在此处查看 more/updated 信息:https://docs.docker.com/compose/environment-variables/

仅关注环境变量的默认值和强制值问题,并作为对 的更新:

现在支持在 docker-compose.yml 文件中使用默认值并强制执行强制值 (from the docs):

Both $VARIABLE and ${VARIABLE} syntax are supported. Additionally when using the 2.1 file format, it is possible to provide inline default values using typical shell syntax:

${VARIABLE:-default} evaluates to default if VARIABLE is unset or empty in the environment. ${VARIABLE-default} evaluates to default only if VARIABLE is unset in the environment.

Similarly, the following syntax allows you to specify mandatory variables:

${VARIABLE:?err} exits with an error message containing err if VARIABLE is unset or empty in the environment. ${VARIABLE?err} exits with an error message containing err if VARIABLE is unset in the environment.

Other extended shell-style features, such as ${VARIABLE/foo/bar}, are not supported.

就这么简单:

使用命令行 as mentioned in the doc:

docker-compose --env-file ./config/.env.dev config 

或者使用 .env 文件,我认为这是最简单的方法:

 web:
  env_file:
    - web-variables.env

Doc with sample