在docker鏡像中加入環境變量

原文連接

前言

reference:https://vsupalov.com/docker-build-time-env-values/docker

不少時候,咱們須要在docker鏡像中加入環境變量,本人瞭解的有2種方法能夠作到ubuntu

第一種

使用docker run --env VARIABLE=VALUE image:tag直接添加變量,適用於直接用docker啓動的項目ui

root@ubuntu:/home/vickey/test_build# docker run --rm -it --env TEST=2 ubuntu:latest
root@2bbe75e5d8c7:/# env |grep "TEST"
TEST=2

第二種

使用dockerfile的ARGENV添加變量,適用於不能用docker run命令啓動的項目,如k8s3d

ARG只在構建docker鏡像時有效(dockerfile的RUN指令等),在鏡像建立了並用該鏡像啓動容器後則無效(後面有例子驗證)。但能夠配合ENV指令使用使其在建立後的容器也能夠生效。code

ARG buildtime_variable=default_value        # if not set default_value buildtime_variable would be set ''
ENV env_var_name=$buildtime_variable

在構建映像時,能夠使用--build-arg buildtime_variable=other_value覆蓋dockerfile裏的變量值default_valuetoken

$ docker build --build-arg buildtime_variable=other_value --tag image:tag

多階段構建

可是有時咱們只是臨時須要環境變量或文件,最後的鏡像是不須要的這些變量的,設置ARG和ENV值就會在Docker鏡像中留下痕跡,好比保密信息等。多階段構建能夠用來去掉包含保密信息的鏡像。get

  • dockerfile
FROM ubuntu as intermediate     # 爲第一階段構建設置別名,在第二階段引用
ARG TEST=deault_value       # 設置環境變量
ENV ENV_TEST=$TEST      # 設置環境變量
RUN echo test > /home/test.txt
RUN cat /home/test.txt      # 查看文件是否正常
RUN env
RUN env |grep TEST      # 查看環境變量是否已設置

FROM ubuntu
COPY --from=intermediate /home/test.txt /home/another_test.txt      # 將第一階段生成的文件拷貝到第二階段鏡像中
RUN cat /home/another_test.txt      # 查看拷貝的文件是否正常
RUN env
RUN env |grep TEST      # 查看環境變量是否已設置
  • 多階段構建
root@ubuntu:/home/vickey/test_build# docker build --build-arg TEST=2 -t ubuntu:test-multi-build --no-cache -f ./dockerfile .
Sending build context to Docker daemon   2.56kB
Step 1/12 : FROM ubuntu as intermediate
 ---> 94e814e2efa8
Step 2/12 : ARG TEST=deault_value
 ---> Running in 7da9180a6311
Removing intermediate container 7da9180a6311
 ---> 7e8420f3ecf2
Step 3/12 : ENV ENV_TEST=$TEST
 ---> Running in 256788d179ce
Removing intermediate container 256788d179ce
 ---> 11cf4e0581d9
Step 4/12 : RUN echo test > /home/test.txt
 ---> Running in c84799ba3831
Removing intermediate container c84799ba3831
 ---> f578ca5fe373
Step 5/12 : RUN cat /home/test.txt
 ---> Running in dbf8272fd10c
test
Removing intermediate container dbf8272fd10c
 ---> 9f8720732878
Step 6/12 : RUN env
 ---> Running in 9050cd9e36c9
HOSTNAME=9050cd9e36c9
HOME=/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TEST=2
PWD=/
ENV_TEST=2
Removing intermediate container 9050cd9e36c9
 ---> f1f4daf42cc0
Step 7/12 : RUN env |grep TEST
 ---> Running in 1cc7968144f5
TEST=2
ENV_TEST=2
Removing intermediate container 1cc7968144f5
 ---> c6d390887082
Step 8/12 : FROM ubuntu
 ---> 94e814e2efa8
Step 9/12 : COPY --from=intermediate /home/test.txt /home/another_test.txt
 ---> 27480a945fab
Step 10/12 : RUN cat /home/another_test.txt
 ---> Running in de1f5a999fe1
test
Removing intermediate container de1f5a999fe1
 ---> 16c630eb6b1b
Step 11/12 : RUN env
 ---> Running in d13becd5ae77
HOSTNAME=d13becd5ae77
HOME=/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
Removing intermediate container d13becd5ae77
 ---> ea52a6e9a7b2
Step 12/12 : RUN env |grep TEST
 ---> Running in 7ef585772e9a
The command '/bin/sh -c env |grep TEST' returned a non-zero code: 1

從dockerfile的註釋和構建時對應步驟的輸出能夠看出,第一階段的環境變量和文件,在第二階段複製了文件後,環境變了沒有複製過來(最後一步報錯了,就是由於環境變量不存在了),正好達到咱們想要的結果---將環境變量保密信息等刪除而保留了咱們想要的文件。dockerfile

驗證第二種方法實例(可忽略)

  • 同一目錄下建立個dockerfile和至少一個文件
root@ubuntu:/home/vickey/test_build# tree -L 2
.
├── dockerfile
└── whatever
0 directories, 2 files
root@ubuntu:/home/vickey/test_build# cat dockerfile 
FROM ubuntu

dockfileit

FROM ubuntu
  • docker構建鏡像
root@ubuntu:/home/vickey/test_build# docker build --build-arg TEST=1 -t ubuntu:test-build -f ./dockerfile .
Sending build context to Docker daemon   2.56kB
Step 1/1 : FROM ubuntu
 ---> 94e814e2efa8
[Warning] One or more build-args [TEST] were not consumed
Successfully built 94e814e2efa8
Successfully tagged ubuntu:test-build
root@ubuntu:/home/vickey/test_build# docker images |grep test-build
ubuntu                                        test-build          94e814e2efa8        3 months ago        88.9MB
  • 用鏡像啓動個容器
root@ubuntu:/home/vickey/test_build# docker run --rm -it ubuntu:test-build
root@383c30a1d6f5:/# env
HOSTNAME=383c30a1d6f5
PWD=/
HOME=/root
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
root@383c30a1d6f5:/# env|grep "TEST"
root@383c30a1d6f5:/#

發現並無構建鏡像時--build-arg TEST=1傳入的變量,由於構建時有個[Warning] One or more build-args [TEST] were not consumed, 須要在dockfile裏面引用TEST才行。test

  • 在dockerfile加入變量
FROM ubuntu
ARG TEST
  • 從新構建並建立容器
root@ubuntu:/home/vickey/test_build# docker build --build-arg TEST=1 -t ubuntu:test-build -f ./dockerfile .
Sending build context to Docker daemon   2.56kB
Step 1/2 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/2 : ARG TEST
 ---> Running in f9ccda7b3a4b
Removing intermediate container f9ccda7b3a4b
 ---> dc95b444ffc5
Successfully built dc95b444ffc5
Successfully tagged ubuntu:test-build
root@ubuntu:/home/vickey/test_build# docker run --rm -it ubuntu:test-build
root@370dd8b3d2ca:/# env
... ignore...
root@370dd8b3d2ca:/# env|grep "TEST"
root@370dd8b3d2ca:/#

發現沒有warning了,但仍是沒有變量TEST,由於ARG只在構建docker鏡像時有效,在鏡像建立了並用該鏡像啓動容器後則無效。但能夠配合ENV指令使用使其在建立後的容器也能夠生效。下面加入ENV看看

  • 在dockerfile加入ENV
FROM ubuntu
ARG TEST
ENV ENV_TEST=$TEST
  • 再次構建並啓動容器
root@ubuntu:/home/vickey/test_build# docker build --build-arg TEST=1 -t ubuntu:test-build -f ./dockerfile .
Sending build context to Docker daemon   2.56kB
Step 1/3 : FROM ubuntu
 ---> 94e814e2efa8
Step 2/3 : ARG TEST
 ---> Using cache
 ---> dc95b444ffc5
Step 3/3 : ENV ENV_TEST=$TEST
 ---> Running in d8cd0014b36b
Removing intermediate container d8cd0014b36b
 ---> ebd198fcb586
Successfully built ebd198fcb586
Successfully tagged ubuntu:test-build
root@ubuntu:/home/vickey/test_build# docker run --rm -it ubuntu:test-build
root@f9dd6cf0bb47:/# env|grep "TEST"
ENV_TEST=1

很好,這時dockerfile的ARG變量TEST已經傳給ENV變量ENV_TEST了。咱們已經能夠使用docker構建時傳入的變量了。

相關文章
相關標籤/搜索