③. RUN 构建时期运行的指令
- ①. RUN指令有两种形式,一种是shell,另外一个是exec形式
②. 在shell形式中,您可以使用(反斜杠)将一条RUN指令继续到下一行
FROM alpine LABEL maintainer="tangzhi" ARG prams=helloword RUN echo $prams &&\ echo tangzhi RUN echo "123456" RUN echo 123456789 # 这种方式取不到环境变量 RUN ["echo","$prams"] # 下面这种写法和RUN echo 123456789等价 RUN ["/bin/sh","-c","echo $prams"]
[root@i-id8g0yu9 ~]# docker build -t mydockerfile4 -f dockerfile57 . Sending build context to Docker daemon 3.119MB Step 1/8 : FROM alpine ---> 6dbb9cc54074 Step 2/8 : LABEL maintainer="tangzhi" ---> Using cache ---> 3b6b777a2b23 Step 3/8 : ARG prams=helloword ---> Using cache ---> 80bcc56f3aee Step 4/8 : RUN echo $prams &&echo tangzhi ---> Running in 9e1c545b68c9 helloword tangzhi Removing intermediate container 9e1c545b68c9 ---> 93f7704f978b Step 5/8 : RUN echo "123456" ---> Running in ae14d2044897 123456 Removing intermediate container ae14d2044897 ---> 32ed2861f3e5 Step 6/8 : RUN echo 123456789 ---> Running in 84db90a766c4 123456789 Removing intermediate container 84db90a766c4 ---> d3fd8aeaec1f Step 7/8 : RUN ["echo","$prams"] ---> Running in 99dddaaba442 $prams Removing intermediate container 99dddaaba442 ---> 31ed024c8569 Step 8/8 : RUN ["/bin/sh","-c","echo $prams"] ---> Running in 4fdfca35b59f helloword Removing intermediate container 4fdfca35b59f ---> a7457f8b7933 Successfully built a7457f8b7933 Successfully tagged mydockerfile4:latest [root@i-id8g0yu9 ~]#
④. CMD(运行时期)、ENTRYPOINT 指定启动容器、镜像的默认入口
- ①. ENTRYPOINT或者CMD作为唯一入口,只能写一个,最后一个生效
#最终都是以ping tangzhi.com为准 CMD ping baidu.com CMD ping tangzhi.com
②. [“echo”,"${param}"] 不是bash -c的方式,取不出环境变量性[]
echo $param 等价于 ["/bin/sh","-c",“多长的命令都写在这里 echo ${param}”]
# CMD ["ping","baidu.com"] # CMD ["useradd","-u","1000","-g","2000"] # CMD ["ping","${url}"] 取不出变量 # CMD ping ${url}
③. 官方都是建议使用[ ]方式(CMD ["/bin/sh","-c",“ping ${url}”]),变化的写CMD,固定不变的写ENTRYPO INT(未来他是容器启动的唯一入口)
# 一旦传递了cmd1,CMD指定的所有参数都会被覆盖 # 在控制台输入 docker run imageName 6 tangzhi.com # ping 5 -c baidu.com CMD [ "5","baidu.com" ] ENTRYPOINT [ "ping","-c" ]
④. 我们使用ENTRYPOINT ping baidu.com的形式输出,那么组合CMD怎么写都没用,容器启动都是以ENT RYPOINT的完整命令为准
# 最终都是以ping baidu.com为准 ENTRYPOINT ping baidu.com CMD ping tangzhi.com