⑤. ARG 构建参数
①. 定义以后的剩下环节生效(不包括运行环节),取值$param不能使用在CMD或者ENTRYPO INT中
②. ARG指令定义了一个变量,用户可以在构建时使用–build-arg = 传递,docker build命令会将其传递给构建器。- -build-arg 指定参数会覆盖Dockerfile 中指定的同名参数
docker build --build-arg param=“xiaozhi” --build-arg message=“hellodockerbuild” --no-cache -t mydockerfile:v1 -f dockerfile3 .
③. 使用ENV指令定义的环境变量始终会覆盖同名的ARG指令
④. ARG不像ENV,不能并排写
⑤. dockerfile文件如下:
# 可以在任意位置使用,并在以后使用 ARG version=3.13.4 # 这是我的第一个dockerfile镜像 FROM alpine:$version # 给镜像加注释信息 LABEL maintainer="TANGZHI " \ age=24 # 运行的指令、安装了软件、修改了文件,默认使用id=0的用户,也就是root,这个基础系统的root # 代表镜像构建过程中的命令 RUN echo hellodockerfile RUN echo $param # 定义以后的剩下环节生效(不包括运行环节),取值$param 不能使用在CMD或者ENTRYPOINT中 # 可以在构建期间进行变化,比如我们使用如下的命令 # docker build --build-arg param="xiaozhi" --build-arg message="hellodockerbuild" --no-cache -t mydockerfile:v1 -f dockerfile3 . # ARG不像ENV 不能并排写 ARG param=helloparam ARG message=hellodocker # 构建时期会运行的指令(根据dockerfile创建一个镜像的整个过程时期) RUN echo 1111 RUN echo $param RUN echo $message # 运行时期,我们会运行的指令(根据之前创建的镜像启动一个容器,容器启动默认运行的命令) # docker run | docker start # CMD和ENTRYPOINT 都是指定运行时的指令 # 当我们使用docker run -it 去执行的时候,发现只有1111输出 CMD ["/bin/sh","-c","echo 1111;echo $param"]
[root@i-id8g0yu9 ~]# docker build --build-arg param="xiaozhi" --build-arg message="hellodockerbuild" --no-cache -t mydockerfile3:v1 -f dockerfile3 . Sending build context to Docker daemon 17.92kB Step 1/11 : ARG version=3.13.4 Step 2/11 : FROM alpine:$version ---> 49f356fa4513 Step 3/11 : LABEL maintainer="TANGZHI " age=24 ---> Running in 382d67fbac27 Removing intermediate container 382d67fbac27 ---> bfa9c16b967a Step 4/11 : RUN echo hellodockerfile ---> Running in 5cc88771ed37 hellodockerfile Removing intermediate container 5cc88771ed37 ---> 337c6e0404bd Step 5/11 : RUN echo $param ---> Running in b88d4af5b1fa Removing intermediate container b88d4af5b1fa ---> e129628435dd Step 6/11 : ARG param=helloparam ---> Running in 5f10ed5dfa16 Removing intermediate container 5f10ed5dfa16 ---> 9904bb5f44df Step 7/11 : ARG message=hellodocker ---> Running in 50f117576dfa Removing intermediate container 50f117576dfa ---> e6c1c8a36097 Step 8/11 : RUN echo 1111 ---> Running in 1ae844ed29a6 1111 Removing intermediate container 1ae844ed29a6 ---> 5947099a85ec Step 9/11 : RUN echo $param ---> Running in c543d2f3ea4c xiaozhi Removing intermediate container c543d2f3ea4c ---> 8353cd041e74 Step 10/11 : RUN echo $message ---> Running in d58ac7513eac hellodockerbuild Removing intermediate container d58ac7513eac ---> 07f41021c095 Step 11/11 : CMD ["/bin/sh","-c","echo 1111;echo $param"] ---> Running in 5cacc787c157 Removing intermediate container 5cacc787c157 ---> e9ed761f20bf Successfully built e9ed761f20bf Successfully tagged mydockerfile3:v1 [root@i-id8g0yu9 ~]# docker run -it mydockerfile3:v1 #注意这里没有输入echo $param 1111 [root@i-id8g0yu9 ~]#
⑥. ENV 构建、运行都生效
- ①. 构建期不能修改ENV的值(docker build)
- ②. 构建期+运行期都可以生效,但是只能在运行期进行修改
- ③. 运行期docker run -it -e message=runENV
# 这是我的第一个dockerfile镜像 FROM alpine # 给镜像加注释信息 LABEL maintainer="TANGZHI " \ age=24 ARG message=helloARG # 构建期+运行期都可以生效,但是只能在运行期进行修改 # 构建期不能修改ENV的值(docker build) # 运行期docker run -it -e message=runENV ENV message=helloENV # 构建时期会运行的指令(根据dockerfile创建一个镜像的整个过程时期) RUN echo $message # 运行时期,我们会运行的指令(根据之前创建的镜像启动一个容器,容器启动默认运行的命令) CMD ["/bin/sh","-c","echo 1111;echo app_$message"]
[root@i-id8g0yu9 ~]# docker build --no-cache -t dockerfilearg:v1 -f dockerfileArg . Sending build context to Docker daemon 20.99kB Step 1/6 : FROM alpine latest: Pulling from library/alpine 540db60ca938: Pull complete Digest: sha256:69e70a79f2d41ab5d637de98c1e0b055206ba40a8145e7bddb55ccc04e13cf8f Status: Downloaded newer image for alpine:latest ---> 6dbb9cc54074 Step 2/6 : LABEL maintainer="TANGZHI " age=24 ---> Running in f4c6286d7edc Removing intermediate container f4c6286d7edc ---> 214a711d0c39 Step 3/6 : ARG message=helloARG ---> Running in a4e068cdd1a2 Removing intermediate container a4e068cdd1a2 ---> d502358f547c Step 4/6 : ENV message=helloENV ---> Running in 20fde633a7c2 Removing intermediate container 20fde633a7c2 ---> 26bcd7c0386d Step 5/6 : RUN echo $message ---> Running in 74d50778b66b helloENV # 如果有ENV和ARY同时定义一个同名变量,我们使用的是ENV Removing intermediate container 74d50778b66b ---> 6867c5ca1d40 Step 6/6 : CMD ["/bin/sh","-c","echo 1111;echo app_$message"] ---> Running in 1980007e51e0 Removing intermediate container 1980007e51e0 ---> a5015b515b34 Successfully built a5015b515b34 Successfully tagged dockerfilearg:v1 [root@i-id8g0yu9 ~]# docker run -it -e message=runENV dockerfilearg:v1 1111 app_runENV [root@i-id8g0yu9 ~]#
④. ENV在image阶段就会被解析并持久化(docker inspect image查看)参照下面示例
msg1=msg2没问题,如果我运行期间修改了msg1=66666的值,请问msg1、msg2输出什么?
结果输出 66666 hello
这是因为env坏境的信息会固化,直接在镜像配置里面就已经写死,msg1=hello
msg2=hello,而-e只能修改当前env本身
# 这是我的第一个dockerfile镜像 FROM alpine # 给镜像加注释信息 LABEL maintainer="TANGZHI " \ age=24 # msg1=msg2没问题,如果我运行期间修改了msg1=66666的值,请问msg1、msg2输出什么? # 结果输出 66666 hello # 这是因为env坏境的信息会固化,直接在镜像配置里面就已经写死,msg1=hello msg2=hello,而-e只能修改当前env本身 ENV msg1=hello ENV msg2=$msg1 # 构建时期会运行的指令(根据dockerfile创建一个镜像的整个过程时期) RUN echo $msg1 RUN echo $msg2 # 运行时期,我们会运行的指令(根据之前创建的镜像启动一个容器,容器启动默认运行的命令) CMD ["/bin/sh","-c","echo $msg1;echo $msg2"]
[root@i-id8g0yu9 ~]# docker build --no-cache -t dockerfilearg2:v1 -f dockerfileArg2 . Sending build context to Docker daemon 22.53kB Step 1/7 : FROM alpine ---> 6dbb9cc54074 Step 2/7 : LABEL maintainer="TANGZHI " age=24 ---> Running in 6f6be6d001e0 Removing intermediate container 6f6be6d001e0 ---> d3cef1d085a5 Step 3/7 : ENV msg1=hello ---> Running in dd2be1e7c37c Removing intermediate container dd2be1e7c37c ---> 034e061f70cc Step 4/7 : ENV msg2=$msg1 ---> Running in 77908fc091f6 Removing intermediate container 77908fc091f6 ---> 5c5499ac0a19 Step 5/7 : RUN echo $msg1 ---> Running in 212c8ea2dcb2 hello Removing intermediate container 212c8ea2dcb2 ---> 5db13711e330 Step 6/7 : RUN echo $msg2 ---> Running in 5e21ac6410e1 hello Removing intermediate container 5e21ac6410e1 ---> 4765e9d7d753 Step 7/7 : CMD ["/bin/sh","-c","echo $msg1;echo $msg2"] ---> Running in 67cb73e39262 Removing intermediate container 67cb73e39262 ---> bf71cc3088ea Successfully built bf71cc3088ea Successfully tagged dockerfilearg2:v1 [root@i-id8g0yu9 ~]# docker run -it -e msg1=66666 dockerfilearg2:v1 66666 hello [root@i-id8g0yu9 ~]#