Dockerfile

  1. FROM centos:centos7
  2. # 把 java 与 tomcat 添加到容器中
  3. ADD jdk-8u231-linux-x64.tar.gz /usr/local
  4. ADD war.war /data/project/chenshun-tag-test/
  5. ADD tomcat.tar.gz /data/project/chenshun-tag-test/
  6. ADD load.sh /root/
  7. RUN yum -y install vim wget curl telnet unzip file openssh-server net-tools \
  8. && /bin/echo "chenshun" | passwd --stdin root \
  9. && ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key \
  10. && ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key \
  11. && ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key \
  12. && /bin/sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g' /etc/pam.d/sshd \
  13. && /bin/sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config \
  14. && /bin/sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
  15. ENV MYPATH /usr/local
  16. WORKDIR $MYPATH
  17. # 配置 java 与 tomcat 环境变量
  18. ENV JAVA_HOME /usr/local/jdk1.8.0_231
  19. ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
  20. ENV CATALINA_HOME /data/project/chenshun-tag-test/tomcat
  21. ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
  22. EXPOSE 22
  23. # 启动时运行tomcat
  24. ENTRYPOINT ["sh","/root/load.sh"]

load.sh

  1. #!/bin/bash
  2. /usr/sbin/sshd -D
  3. unzip /data/project/chenshun-tag-test/war.war -d /data/project/chenshun-tag-test/code
  4. rm -f /data/project/chenshun-tag-test/war.war
  5. sh /data/project/chenshun-tag-test/tomcat/bin/startup.sh
  6. touch /root/xx.log
  7. 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
  8. tail -fn 2 /root/xx.log

build

  • build
  • commit
  • push
    1. [root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker build -t chenshun00/java8:1.0.2 .
    2. Sending build context to Docker daemon 221.6 MB
    3. Step 1/14 : FROM centos:centos7
    4. ---> 5e35e350aded
    5. Step 2/14 : ADD jdk-8u231-linux-x64.tar.gz /usr/local
    6. ---> Using cache
    7. ---> 1b639eb2af81
    8. ....
    9. Removing intermediate container 44b18de8d569
    10. Successfully built 332e5a810ee2
    11. root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker run -i --name java8.1.0.2 -d chenshun00/java8:1.0.2
    12. e3cd8ebd6406774d7eab2917d82778669fa6eacdd1ec017f7ae2c0fe50d99424
    13. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    14. e3cd8ebd6406 chenshun00/java8:1.0.2 "sh /root/load.sh" 2 seconds ago Up 2 seconds 22/tcp java8.1.0.2
    15. [root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker commit -a chenshun00 -m test e3cd8ebd6406 chenshun00/java8:1.0.2
    16. sha256:819b4f00fa721f893901450723ecaf16ffb0860660c635a2a2cfbd8ad514a557
    17. [root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker push chenshun00/java8:1.0.2
    18. The push refers to a repository [docker.io/chenshun00/java8]
    19. 689ae2934f88: Pushed
    20. 3140aa6365da: Pushing [==================================> ] 105.5 MB/154.7 MB
    21. 06616a855bc3: Pushed
    22. 28b42818535c: Pushed
    23. 23312e433ad2: Pushed
    24. 24103fe2258d: Layer already exists
    25. 77b174a6a187: Layer already exists
    然后这个镜像就可以使用了

迭代

  • 新增一个shutdown.sh

    1. #!/bin/bash
    2. PID=`ps -ef |grep java |grep -v grep|awk '{print $2}'`
    3. if [[ -n ${PID} ]]; then
    4. kill ${PID}
    5. fi
  • DockerFile2

    1. FROM chenshun00/java8:1.0.2
    2. ADD shutdown.sh /root/
    3. ENTRYPOINT ["sh","/root/load.sh"]
  • build

    1. [root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker build -f Dockerfile2 -t chenshun00/java8:1.0.3 .
    2. Sending build context to Docker daemon 221.6 MB
    3. Step 1/3 : FROM chenshun00/java8:1.0.2
    4. ---> 819b4f00fa72
    5. Step 2/3 : ADD shutdown.sh /root/
    6. ---> f4b97550a8a3
    7. Removing intermediate container 6432d5fa1bc2
    8. Step 3/3 : ENTRYPOINT sh /root/load.sh
    9. ---> Running in 6fb61565229f
    10. ---> db24092a381b
    11. Removing intermediate container 6fb61565229f
    12. Successfully built db24092a381b
  • commit && push

    1. [root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker run -i --name java8.1.0.3 -d chenshun00/java8:1.0.3
    2. 945316b5bb18916feed9b6e7427aab599bdfdc821376803d4c4301a8eb34a766
    3. [root@iZbp11om21c05wzu8e4tx0Z xxxx]#
    4. [root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker ps
    5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6. 945316b5bb18 chenshun00/java8:1.0.3 "sh /root/load.sh" 2 seconds ago Up 1 second 22/tcp java8.1.0.3
    7. [root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker commit -a chenshun00 -m test 945316b5bb18 chenshun00/java8:1.0.3
    8. sha256:5c10788fb275570d6c61b3b80beeb78a7262736d732ada12e0fb0042c206d308
    9. [root@iZbp11om21c05wzu8e4tx0Z xxxx]#
    10. [root@iZbp11om21c05wzu8e4tx0Z xxxx]# docker push chenshun00/java8:1.0.3
    11. The push refers to a repository [docker.io/chenshun00/java8]
    12. 5312b4b9db27: Pushed
    13. b7a9f3dfe35c: Pushed
    14. 689ae2934f88: Layer already exists
    15. 3140aa6365da: Layer already exists
    16. 06616a855bc3: Layer already exists
    17. 28b42818535c: Layer already exists
    18. 23312e433ad2: Layer already exists
    19. 24103fe2258d: Layer already exists
    20. 77b174a6a187: Layer already exists
    21. 1.0.3: digest: sha256:e8e20a1c6a9e10111b4ad82787ba1183235602a2c30da6ca3675d2b9f8b1c063 size: 2205