我希望能够在docker-compose中使用env变量。Yml,在docker-compose up时传入的值。这就是例子。

今天我使用的是基本的docker run命令,该命令包含在我自己的脚本中。 有没有一种方法来实现它与撰写,没有任何这样的bash包装?

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

当前回答

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"

我从这个链接得到的 https://github.com/docker/cli/issues/939

其他回答

我最终在我的deploy.sh脚本中使用“sed”来完成这一点,尽管我的要求略有不同,因为Docker - Compose是由Terrafom调用的:通过Azure应用程序服务的Terraform脚本将变量传递给Docker Compose

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 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"

我从这个链接得到的 https://github.com/docker/cli/issues/939

这是为Docker v20编写的,使用Docker compose v2命令。

I was having a similar roadblock and found that the --env-file parameter ONLY works for docker compose config command. On top of that using the docker compose env_file variable, still forced me to repeat values for the variables, when wanting to reuse them in other places than the Dockerfile such as environment for docker-compose.yml. I just wanted one source of truth, my .env, with the ability to swap them per deployment stage. So here is how I got it to work, basically use docker compose config to generate a base docker-compose.yml file that will pass ARG into Dockerfile's.

。local。这是你的。env,我把我的。env分成了不同的部署。

DEVELOPMENT=1
PLATFORM=arm64

docker-compose.config。yml -这是我的核心docker合成文件。

services:
  server:
    build:
      context: .
      dockerfile: docker/apache2/Dockerfile
      args:
        - PLATFORM=${PLATFORM}
        - DEVELOPMENT=${DEVELOPMENT}
    environment:
      - PLATFORM=${PLATFORM}
      - DEVELOPMENT=${DEVELOPMENT}

现在不幸的是,我需要传递两次变量,一次用于Dockerfile,另一次用于environment。然而,它们仍然来自单一来源。local。env这样至少我不需要重复值。

然后,我使用docker compose config生成一个半成品docker-compose.yml。这让我传入我的同伴override docker-compose.local。Yml表示发生最终部署的地方。

docker compose --env-file=.local.env -f docker-compose.config.yml config > docker-compose.yml

这将允许我的Dockerfile访问.env变量。

FROM php:5.6-apache

# Make sure to declare after FROM
ARG PLATFORM
ARG DEVELOPMENT

# Access args in strings with $PLATFORM, and can wrap i.e ${PLATFORM}
RUN echo "SetEnv PLATFORM $PLATFORM" > /etc/apache2/conf-enabled/environment.conf
RUN echo "SetEnv DEVELOPMENT $DEVELOPMENT" > /etc/apache2/conf-enabled/environment.conf

然后从docker-compose中传递.env变量。yml到Dockerfile,然后传递到我的Apache HTTP服务器,它传递到我的最终目的地,PHP代码。

我的下一步是从部署阶段传入我的docker合成重写。

docker-compose.local。yml -这是我的docker重写。

services:
  server:
    volumes:
      - ./localhost+2.pem:/etc/ssl/certs/localhost+2.pem
      - ./localhost+2-key.pem:/etc/ssl/private/localhost+2-key.pem

最后,运行docker compose命令。

docker compose -f docker-compose.yml -f docker-compose.local.yml up --build

请注意,如果你在你的.env文件中改变了任何东西,你将需要重新运行docker compose config并添加——build for docker compose up。因为构建是缓存的,所以影响很小。

因此,对于我的最后一个命令,我通常运行:

docker compose --env-file=.local.env -f docker-compose.config.yml config > docker-compose.yml; docker compose --env-file=.local.env -f docker-compose.yml -f docker-compose.local.yml up --build 

从1.25.4开始,docker-compose支持——env-file选项,允许你指定一个包含变量的文件。

你的简历应该是这样的:

hostname=my-host-name

和命令:

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

使用.env文件在docker-compse.yml中定义动态值。不管是port还是其他值。

示例docker-compose:

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