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

时间:2018-07-10 21:18:14

标签: docker docker-compose environment-variables versioning

我尝试遵循Dockerfiledocker compose引用将环境变量传递给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

docker-compose.yml:

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

如果我直接构建映像,则可以很好地传递env var:

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 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-compose.yml中使用environment,如下所示,它再次仅使其在运行时可用,而在构建时不可用:

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

请告知,如何使它以最少的复杂度工作?

3 个答案:

答案 0 :(得分:4)

您的示例对我有用。

您是否尝试过删除图像并重新构建?即使映像在缓存中,环境变量也会发生变化,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 --no-cache,并且如果您有多个服务,则可以避免提及“ app”,因此docker-compose将构建所有服务。您可以在同一docker-compose build中处理不同的构建版本的方法是使用不同的环境变量,例如:

$ cat docker-compose
version: '3'
services:
  app1:
    build:
      context: .
      args:
        - BUILD_VERSION=$APP1_BUILD_VERSION
  app2:
    build:
      context: .
      args:
        - BUILD_VERSION=$APP2_BUILD_VERSION

导出:

$ export APP1_BUILD_VERSION=1.1.1
$ export APP2_BUILD_VERSION=2.2.2

内部版本:

$ 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

答案 1 :(得分:2)

您需要如图所示在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

答案 2 :(得分:1)

您可以传入args进行构建,从docker-compose文件到docker构建。令人惊讶的是,没有将env var用于运行构建。

// docker-compose.yml

version: '3'
services:
app:
  build:
    context: .
environment:
  - BUILD_VERSION
args:
  - BUILD_VERSION=${BUILD_VERSION}
volumes:
       ...
// 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