在Dockerbuild文件中使用docker

在Dockerbuild文件中使用docker

本文介绍了在Dockerbuild文件中使用docker-compose env变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下docker-compose文件:

Having the following docker-compose file:

db:
    build: .
    environment:
        - MYSQL_ROOT_PASSWORD=password
        - ENV=test
    env_file: .env

有什么方法可以将在docker-compose.yml中声明的env变量(作为环境或在env_file中声明)用作Dockerfile的一部分,而无需在Dockerfile中声明它们?像这样的东西:

Is there any way to use the env variables declared in docker-compose.yml (either as environment or declared in the env_file) as part of Dockerfile without declaring them in the Dockerfile? Something like this:

FROM java:7
ADD ${ENV}/data.xml /data/
CMD ["run.sh"]


推荐答案

尽管这个问题是很久以前提出的,这里有一个类似问题的答案:

Although this question was asked long ago, there is an answer to a similar question here: Pass environment variables from docker-compose to container at build stage

基本上,要在容器的构建时间必须在 docker-compose.yml 中定义变量:

Basically, to use variables at the container's build time one has to define the variable in docker-compose.yml:

build:
  context: .
  args:
    MYSQL_ROOT_PASSWORD: password
    ENV: test

然后使用 ARG Dockerfile 中引用它:

ARG MYSQL_ROOT_PASSWORD
ARG ENV
ADD ${ENV}/data.xml /data/

关于 *。env 文件中定义的环境变量,我相信它们不能在构建时传递给容器时间。

Concerning environment variables defined in an *.env file, I believe that they can't be passed to the container at build time.

这篇关于在Dockerbuild文件中使用docker-compose env变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 22:36