dockerfile传递参数
目录:
为了解决每次都需要单独编写dockerfile的问题,计划对所有jar包使用相同的Docker模板
Dockerfile部分
原Dockerfile
cat /opt/dockerfile/www-why-api-Dockerfile
FROM reg.whysdomain.com/java:termclean
ADD why-api.jar /home/why-api.jar
CMD mkdir -p /home/logs/why-api/ && nohup java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8184 -Dspring.profiles.active=www /home/why-api.jar
使用Dockerfile的ARG
FROM reg.whysdomain.com/java:termclean
# 定义变量
ARG JAR_NAME
ARG START_ARG
ADD $JAR_NAME /home/$JAR_NAME
RUN echo $START_ARG > /home/start.sh
CMD sh /home/start.sh
ARG参考官方文档
这里Dockerfile中ARG可以设置默认值
这里需要将docker build传入的启动命令写入到脚本中,直接在启动的时候CMD $START_ARG是不生效的,因为参数只在docker build过程生效,传递到docker镜像的时候,可以看到每一层仍然是以变量的形式,在镜像启动的时候无法获取变量
如果使用CMD $START_ARG
构建的镜像
$ docker history reg.whysdomain.com/www-why-api-template
IMAGE CREATED CREATED BY SIZE COMMENT
84ec9c75e92d 35 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "$STAR 0 B
<missing> 35 minutes ago /bin/sh -c #(nop) ADD file:276c5d4310f513b058 71.46 MB
<missing> 35 minutes ago /bin/sh -c #(nop) ARG START_ARG 0 B
<missing> 35 minutes ago /bin/sh -c #(nop) ARG JAR_NAME 0 B
<missing> 8 months ago /bin/sh -c #(nop) ENTRYPOINT ["/usr/bin/init 0 B
<missing> 8 months ago /bin/sh -c #(nop) ADD file:d68bac0c44a587a28b 19.01 kB
<missing> 19 months ago /bin/sh -c #(nop) COPY file:e28334f3b5e954558 12.14 MB
<missing> 20 months ago /bin/sh -c #(nop) ENTRYPOINT &{["/usr/bin/ini 0 B
<missing> 20 months ago /bin/sh -c #(nop) ADD file:e589098bbf5fbfb57b 46.4 kB
<missing> 20 months ago /bin/sh -c #(nop) MAINTAINER zhuxiaolei@huala 0 B
<missing> 23 months ago /bin/sh -c #(nop) ADD file:1297a9e04b6828a937 388 B
<missing> 2 years ago /bin/sh -c apk upgrade --update && apk ad 117.5 MB
<missing> 2 years ago /bin/sh -c #(nop) ENV JAVA_VERSION_MAJOR=8 JA 0 B
<missing> 2 years ago /bin/sh -c #(nop) MAINTAINER Anastas Dancha < 0 B
<missing> 2 years ago /bin/sh -c #(nop) ADD file:852e9d0cb9d906535a 4.799 MB
$ docker inspect 84ec9c75e92d | grep -A 10 Cmd
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/sh\" \"-c\" \"$START_ARG\"]"
],
"ArgsEscaped": true,
"Image": "sha256:63d62ffb7fb8de9e359d27ef930353dcbf604c389b53518e68c89f74a7187389",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
--
"Cmd": [
"/bin/sh",
"-c",
"$START_ARG"
],
"ArgsEscaped": true,
"Image": "sha256:63d62ffb7fb8de9e359d27ef930353dcbf604c389b53518e68c89f74a7187389",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": [
"/usr/bin/init",
可以看到直接使用的仍然是$START_ARG
构建镜像参数
这里主要是使用了--build-arg,这里支持多个参数传入,和run的-e参数类似,每传入一个参数使用一次
docker build --build-arg JAR_NAME=why-api.jar --build-arg START_ARG=' mkdir -p /home/logs/why-api/ && nohup java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8184 -Dspring.profiles.active=www /home/why-api.jar' --pull --no-cache -t $IMAGES_NAME .
kubernetes中启动
$ kubectl exec -it why-api-3704865154-4k31t -c why-api /bin/bash
bash-4.3# ps -ef
PID USER TIME COMMAND
1 root 0:00 /usr/bin/init -- /bin/sh -c sh /home/start.sh
7 root 0:00 /bin/sh -c sh /home/start.sh
8 root 0:00 sh /home/start.sh
10 root 1:31 java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8184 -Dspring.profiles.active=www /home/why-api.jar
127 root 0:00 /bin/bash
134 root 0:00 ps -ef
bash-4.3# cat /home/start.sh
mkdir -p /home/logs/why-api/ && nohup java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8184 -Dspring.profiles.active=www /home/why-api.jar
可以看到/home/start.sh
写入了传递进来的启动参数,也正常启动