使用docker compose在构建时将环境变量传递给docker映像

使用docker compose在构建时将环境变量传递给docker映像,docker,docker-compose,environment-variables,versioning,Docker,Docker Compose,Environment Variables,Versioning,我尝试跟随和引用将环境变量传递给Docker映像的所有操作都不起作用 我想在使用docker compose时,在docker构建期间使此环境变量可用 在Docker主机上,我有: export BUILD_VERSION=1.0 app.js console.log('BUILD_VERSION: ' + process.env.BUILD_VERSION); Dockerfile: FROM node ADD app.js / ARG BUILD_VERSION ENV BUILD_VE

我尝试跟随和引用将环境变量传递给Docker映像的所有操作都不起作用

我想在使用docker compose时,在docker构建期间使此环境变量可用

在Docker主机上,我有:

export BUILD_VERSION=1.0
app.js

console.log('BUILD_VERSION: ' + process.env.BUILD_VERSION);
Dockerfile:

FROM node
ADD app.js /
ARG BUILD_VERSION
ENV BUILD_VERSION=$BUILD_VERSION
RUN echo Build Time: $BUILD_VERSION
RUN node /app.js
CMD echo Run Time: $BUILD_VERSION
version: '3'
services:
  app:
    build:
      context: .
      args:
        - BUILD_VERSION
docker compose.yml:

FROM node
ADD app.js /
ARG BUILD_VERSION
ENV BUILD_VERSION=$BUILD_VERSION
RUN echo Build Time: $BUILD_VERSION
RUN node /app.js
CMD echo Run Time: $BUILD_VERSION
version: '3'
services:
  app:
    build:
      context: .
      args:
        - BUILD_VERSION
如果我直接构建映像,则会很好地传递env变量:

docker build -t test --no-cache --build-arg BUILD_VERSION .
并且在运行时也可用:

$ docker run --rm test
Run Time: 1.0
$ docker run --rm test node /app
BUILD_VERSION: 1.0
$ docker run --rm test
Run Time: 1.0
$ docker run --rm test node /app
BUILD_VERSION: 1.0
但与docker compose不同

docker-compose up --build

...
Step 5/7 : RUN echo Build Time: $BUILD_VERSION
 ---> Running in 6115161f33bf
Build Time:
 ---> c691c619018a
Removing intermediate container 6115161f33bf
Step 6/7 : RUN node /app.js
 ---> Running in f51831cc5e1e
BUILD_VERSION:
它仅在运行时可用:

$ docker run --rm test
Run Time: 1.0
$ docker run --rm test node /app
BUILD_VERSION: 1.0
$ docker run --rm test
Run Time: 1.0
$ docker run --rm test node /app
BUILD_VERSION: 1.0
我还尝试在docker-compose.yml中使用
environment
,如下所示,它同样只在运行时可用,而在构建时不可用:

version: '3'
services:
  app:
    build:
      context: .
    environment:
      - BUILD_VERSION

请告知,如何使其以最简单的方式工作?

您可以将参数传递到构建,从docker compose文件传递到docker构建。令人惊讶的是,env变量没有用于运行和构建


你的例子对我有用

您是否尝试过删除图像并重新构建?如果映像在缓存中,Docker将不会重新构建映像,尽管环境变量已更改

您可以通过以下方式删除它们:

 docker-compose down --rmi all

编辑,我在这里展示它在构建时是如何为我工作的:

$ cat Dockerfile
FROM alpine
ARG BUILD_VERSION
ENV BUILD_VERSION=$BUILD_VERSION
RUN echo Build Time: $BUILD_VERSION

$ cat docker-compose.yml
version: '3'
services:
  app:
    build:
      context: .
      args:
        - BUILD_VERSION
建造:

$ export BUILD_VERSION=122221
$ docker-compose up --build
Creating network "a_default" with the default driver
Building app
Step 1/4 : FROM alpine
latest: Pulling from library/alpine
8e3ba11ec2a2: Pull complete
Digest: sha256:7043076348bf5040220df6ad703798fd8593a0918d06d3ce30c6c93be117e430
Status: Downloaded newer image for alpine:latest
---> 11cd0b38bc3c
Step 2/4 : ARG BUILD_VERSION
---> Running in b0a1a79967a0
Removing intermediate container b0a1a79967a0
---> 9fa331d63f6d
Step 3/4 : ENV BUILD_VERSION=$BUILD_VERSION
---> Running in a602c27689a5
Removing intermediate container a602c27689a5
---> bf2181423c93
Step 4/4 : RUN echo Build Time: $BUILD_VERSION                <<<<<< (*)
---> Running in 9d828cefcfab
Build Time: 122221
Removing intermediate container 9d828cefcfab
---> 2b3afa3d348c
Successfully built 2b3afa3d348c
Successfully tagged a_app:latest
Creating a_app_1 ... done
Attaching to a_app_1
a_app_1 exited with code 0
$ docker-compose build
Building app1
Step 1/4 : FROM alpine
latest: Pulling from library/alpine
8e3ba11ec2a2: Pull complete
Digest: sha256:7043076348bf5040220df6ad703798fd8593a0918d06d3ce30c6c93be117e430
Status: Downloaded newer image for alpine:latest
---> 11cd0b38bc3c
Step 2/4 : ARG BUILD_VERSION
---> Running in 0b66093bc2ef
Removing intermediate container 0b66093bc2ef
---> 906130ee5da8
Step 3/4 : ENV BUILD_VERSION=$BUILD_VERSION
---> Running in 9d89b48c875d
Removing intermediate container 9d89b48c875d
---> ca2480695149
Step 4/4 : RUN echo Build Time: $BUILD_VERSION
---> Running in 52dec27874ec
Build Time: 1.1.1
Removing intermediate container 52dec27874ec
---> 1b3654924297
Successfully built 1b3654924297
Successfully tagged a_app1:latest
Building app2
Step 1/4 : FROM alpine
---> 11cd0b38bc3c
Step 2/4 : ARG BUILD_VERSION
---> Using cache
---> 906130ee5da8
Step 3/4 : ENV BUILD_VERSION=$BUILD_VERSION
---> Running in d29442339459
Removing intermediate container d29442339459
---> 8b26def5ef3a
Step 4/4 : RUN echo Build Time: $BUILD_VERSION
---> Running in 4b3de2d223e5
Build Time: 2.2.2
Removing intermediate container 4b3de2d223e5
---> 89033b10b61e
Successfully built 89033b10b61e
Successfully tagged a_app2:latest
出口:

$ export APP1_BUILD_VERSION=1.1.1
$ export APP2_BUILD_VERSION=2.2.2
建造:

$ export BUILD_VERSION=122221
$ docker-compose up --build
Creating network "a_default" with the default driver
Building app
Step 1/4 : FROM alpine
latest: Pulling from library/alpine
8e3ba11ec2a2: Pull complete
Digest: sha256:7043076348bf5040220df6ad703798fd8593a0918d06d3ce30c6c93be117e430
Status: Downloaded newer image for alpine:latest
---> 11cd0b38bc3c
Step 2/4 : ARG BUILD_VERSION
---> Running in b0a1a79967a0
Removing intermediate container b0a1a79967a0
---> 9fa331d63f6d
Step 3/4 : ENV BUILD_VERSION=$BUILD_VERSION
---> Running in a602c27689a5
Removing intermediate container a602c27689a5
---> bf2181423c93
Step 4/4 : RUN echo Build Time: $BUILD_VERSION                <<<<<< (*)
---> Running in 9d828cefcfab
Build Time: 122221
Removing intermediate container 9d828cefcfab
---> 2b3afa3d348c
Successfully built 2b3afa3d348c
Successfully tagged a_app:latest
Creating a_app_1 ... done
Attaching to a_app_1
a_app_1 exited with code 0
$ docker-compose build
Building app1
Step 1/4 : FROM alpine
latest: Pulling from library/alpine
8e3ba11ec2a2: Pull complete
Digest: sha256:7043076348bf5040220df6ad703798fd8593a0918d06d3ce30c6c93be117e430
Status: Downloaded newer image for alpine:latest
---> 11cd0b38bc3c
Step 2/4 : ARG BUILD_VERSION
---> Running in 0b66093bc2ef
Removing intermediate container 0b66093bc2ef
---> 906130ee5da8
Step 3/4 : ENV BUILD_VERSION=$BUILD_VERSION
---> Running in 9d89b48c875d
Removing intermediate container 9d89b48c875d
---> ca2480695149
Step 4/4 : RUN echo Build Time: $BUILD_VERSION
---> Running in 52dec27874ec
Build Time: 1.1.1
Removing intermediate container 52dec27874ec
---> 1b3654924297
Successfully built 1b3654924297
Successfully tagged a_app1:latest
Building app2
Step 1/4 : FROM alpine
---> 11cd0b38bc3c
Step 2/4 : ARG BUILD_VERSION
---> Using cache
---> 906130ee5da8
Step 3/4 : ENV BUILD_VERSION=$BUILD_VERSION
---> Running in d29442339459
Removing intermediate container d29442339459
---> 8b26def5ef3a
Step 4/4 : RUN echo Build Time: $BUILD_VERSION
---> Running in 4b3de2d223e5
Build Time: 2.2.2
Removing intermediate container 4b3de2d223e5
---> 89033b10b61e
Successfully built 89033b10b61e
Successfully tagged a_app2:latest

您需要在docker-compose.yml中设置参数,如图所示,该参数将被传递的env变量覆盖-

version: '3'
services:
  app:
    build:
      context: .
      args:
        - BUILD_VERSION
需要传递的下一个导出环境变量

$ export BUILD_VERSION=1.0
现在使用命令构建图像

$ docker-compose build --no-cache --build-arg BUILD_VERSION=$BUILD_VERSION app

只要Dockerfile具有ENV AA=$BB,并且之前您已经完成了导出BB=CC,那么yaml就没有必要提及参数或任何命令行构建-arg@ScottStensland我也试过了;没用(我不使用docker compose来构建映像,只用于启动或关闭其容器…来构建我发布…docker build--tag foo--no cache…后面是…docker push foo…docker compose yaml文件的内容是运行时设置,它会合并并复杂化,让它同时控制构建,特别是在您有许多容器的重建标准与启动标准不同。我同意最好将所有构建任务保留在Dockerfile中,只要它在通过docker compose调用时可以工作。但我还没有找到一个有效的解决方案。在运行时,env var可用。我需要在构建时使用它。它甚至可以在usin中没有虚拟值的情况下工作g
docker build
命令,如我所述。但我需要它来与
docker compose
一起工作。我已经更新了答案,如果它有效,你能试试吗!它有效!但我希望我可以用
docker compose up
一步完成。当我在
docker compose中有多个服务时,我必须用它来构建图像分开。