Dockerfile
FROM centos:centos7
# 把 java 与 tomcat 添加到容器中
ADD jdk-8u231-linux-x64.tar.gz /usr/local
ADD war.war /data/project/chenshun-tag-test/
ADD tomcat.tar.gz /data/project/chenshun-tag-test/
ADD load.sh /root/
RUN yum -y install vim wget curl telnet unzip file openssh-server net-tools \
&& /bin/echo "chenshun" | passwd --stdin root \
&& ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key \
&& ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key \
&& ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key \
&& /bin/sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g' /etc/pam.d/sshd \
&& /bin/sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config \
&& /bin/sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
ENV MYPATH /usr/local
WORKDIR $MYPATH
# 配置 java 与 tomcat 环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_231
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /data/project/chenshun-tag-test/tomcat
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 22
# 启动时运行tomcat
ENTRYPOINT ["sh","/root/load.sh"]
load.sh
#!/bin/bash
/usr/sbin/sshd -D
unzip /data/project/chenshun-tag-test/war.war -d /data/project/chenshun-tag-test/code
rm -f /data/project/chenshun-tag-test/war.war
sh /data/project/chenshun-tag-test/tomcat/bin/startup.sh
touch /root/xx.log
echo ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDajuleUTvfKeOkSmlWGP7kGtrsAhIRnVxTxK6Q5LmyymhvK3M732odwnd+/SP+Cj4n6L73wuFiRnhA0/T7QjKu6AP0ZVTKCuDE1QrcNDhd06/tGd2ewkN7lbM8b+5n427bxpROvazOk2ai3qxPMlOLFNqGrLHHRqseXrB2Apgras9Bq/R4md0WIDoG2LMM+vknaQEIZa2AFQi9zBcLK3n3O3K+yKdetVKYoCQd4rMJhkICBs0T+c++DHzwNfXN1cPkwhMx9+efxSCyiag2OPLQnmta0UEce6XRCG3g9/uo8TOzmZVVAzI+hZgdOkSXM2EPAlV9jR+PIMrFX2u/MWK/ root@iZbp11om21c05wzu8e4tx0Z > /root/.ssh/authorized_keys
tail -fn 2 /root/xx.log
build
- build
- commit
- push
然后这个镜像就可以使用了[root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker build -t chenshun00/java8:1.0.2 .
Sending build context to Docker daemon 221.6 MB
Step 1/14 : FROM centos:centos7
---> 5e35e350aded
Step 2/14 : ADD jdk-8u231-linux-x64.tar.gz /usr/local
---> Using cache
---> 1b639eb2af81
....
Removing intermediate container 44b18de8d569
Successfully built 332e5a810ee2
root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker run -i --name java8.1.0.2 -d chenshun00/java8:1.0.2
e3cd8ebd6406774d7eab2917d82778669fa6eacdd1ec017f7ae2c0fe50d99424
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3cd8ebd6406 chenshun00/java8:1.0.2 "sh /root/load.sh" 2 seconds ago Up 2 seconds 22/tcp java8.1.0.2
[root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker commit -a chenshun00 -m test e3cd8ebd6406 chenshun00/java8:1.0.2
sha256:819b4f00fa721f893901450723ecaf16ffb0860660c635a2a2cfbd8ad514a557
[root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker push chenshun00/java8:1.0.2
The push refers to a repository [docker.io/chenshun00/java8]
689ae2934f88: Pushed
3140aa6365da: Pushing [==================================> ] 105.5 MB/154.7 MB
06616a855bc3: Pushed
28b42818535c: Pushed
23312e433ad2: Pushed
24103fe2258d: Layer already exists
77b174a6a187: Layer already exists
迭代
新增一个shutdown.sh
#!/bin/bash
PID=`ps -ef |grep java |grep -v grep|awk '{print $2}'`
if [[ -n ${PID} ]]; then
kill ${PID}
fi
DockerFile2
FROM chenshun00/java8:1.0.2
ADD shutdown.sh /root/
ENTRYPOINT ["sh","/root/load.sh"]
build
[root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker build -f Dockerfile2 -t chenshun00/java8:1.0.3 .
Sending build context to Docker daemon 221.6 MB
Step 1/3 : FROM chenshun00/java8:1.0.2
---> 819b4f00fa72
Step 2/3 : ADD shutdown.sh /root/
---> f4b97550a8a3
Removing intermediate container 6432d5fa1bc2
Step 3/3 : ENTRYPOINT sh /root/load.sh
---> Running in 6fb61565229f
---> db24092a381b
Removing intermediate container 6fb61565229f
Successfully built db24092a381b
commit && push
[root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker run -i --name java8.1.0.3 -d chenshun00/java8:1.0.3
945316b5bb18916feed9b6e7427aab599bdfdc821376803d4c4301a8eb34a766
[root@iZbp11om21c05wzu8e4tx0Z xxxx]#
[root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
945316b5bb18 chenshun00/java8:1.0.3 "sh /root/load.sh" 2 seconds ago Up 1 second 22/tcp java8.1.0.3
[root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker commit -a chenshun00 -m test 945316b5bb18 chenshun00/java8:1.0.3
sha256:5c10788fb275570d6c61b3b80beeb78a7262736d732ada12e0fb0042c206d308
[root@iZbp11om21c05wzu8e4tx0Z xxxx]#
[root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker push chenshun00/java8:1.0.3
The push refers to a repository [docker.io/chenshun00/java8]
5312b4b9db27: Pushed
b7a9f3dfe35c: Pushed
689ae2934f88: Layer already exists
3140aa6365da: Layer already exists
06616a855bc3: Layer already exists
28b42818535c: Layer already exists
23312e433ad2: Layer already exists
24103fe2258d: Layer already exists
77b174a6a187: Layer already exists
1.0.3: digest: sha256:e8e20a1c6a9e10111b4ad82787ba1183235602a2c30da6ca3675d2b9f8b1c063 size: 2205