加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 03:29:49 所属栏目:安全 来源:网络整理
导读:我尝试在Dockerfile和docker compose之后尝试将环境变量传递给Docker图像的所有内容都不起作用. 我想在使用docker-compose时在docker build期间使这个env变量可用. 在Docker主机上我有: export BUILD_VERSION=1.0 app.js console.log('BUILD_VERSION: ' + p

我尝试在Dockerfile和docker compose之后尝试将环境变量传递给Docker图像的所有内容都不起作用.

我想在使用docker-compose时在docker build期间使这个env变量可用.

在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

泊坞窗,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撰写.

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中使用环境,如下所示,它只能在运行时使用它,但不能在构建时使用它:

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

请告知,我怎样才能让它以最复杂的方式运作?

最佳答案
你的榜样对我有用.

您是否尝试删除图像并重新构建?如果图像位于缓存中,则尽管环境变量已更改,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构建中处理不同构建版本的方法是使用不同的env变量,例如:

$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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读