ora*_*rad 6 versioning environment-variables docker docker-compose
我尝试跟随Dockerfile和docker 组合引用以将环境变量传递给Docker镜像的所有内容都不起作用.
我想在使用docker-compose时在docker build期间使这个env变量可用.
在Docker主机上我有:
export BUILD_VERSION=1.0
Run Code Online (Sandbox Code Playgroud)
app.js
console.log('BUILD_VERSION: ' + process.env.BUILD_VERSION);
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
泊坞窗,compose.yml:
version: '3'
services:
app:
build:
context: .
args:
- BUILD_VERSION
Run Code Online (Sandbox Code Playgroud)
如果我直接构建图像,env var就可以正常传递:
docker build -t test --no-cache --build-arg BUILD_VERSION .
Run Code Online (Sandbox Code Playgroud)
并且在运行时也可用:
$ docker run --rm test
Run Time: 1.0
$ docker run --rm test node /app
BUILD_VERSION: 1.0
Run Code Online (Sandbox Code Playgroud)
但不是与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:
Run Code Online (Sandbox Code Playgroud)
它仅在运行时可用:
$ docker run --rm test
Run Time: 1.0
$ docker run --rm test node /app
BUILD_VERSION: 1.0
Run Code Online (Sandbox Code Playgroud)
我也试过environment在下面的docker-compose.yml中使用它,它只能在运行时使用它而不是构建时:
version: '3'
services:
app:
build:
context: .
environment:
- BUILD_VERSION
Run Code Online (Sandbox Code Playgroud)
请告知,我怎样才能让它以最复杂的方式运作?
你的例子对我有用。
您是否尝试过删除图像并重新构建?如果图像在缓存中,尽管环境变量已更改,Docker 也不会重新构建您的图像。
您可以使用以下方法删除它们:
docker-compose down --rmi all
Run Code Online (Sandbox Code Playgroud)
编辑,我在这里展示了它在构建时如何为我工作:
$ 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
Run Code Online (Sandbox Code Playgroud)
建造:
$ 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
Run Code Online (Sandbox Code Playgroud)
正如另一个答案所提到的,您可以使用docker-compose build --no-cache,并且如果您有多个服务,则可以避免提及“应用程序”,因此 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
Run Code Online (Sandbox Code Playgroud)
出口:
$ export APP1_BUILD_VERSION=1.1.1
$ export APP2_BUILD_VERSION=2.2.2
Run Code Online (Sandbox Code Playgroud)
建造:
$ 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3582 次 |
| 最近记录: |