docker构建tomcat镜像
Posted 葡萄架
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker构建tomcat镜像相关的知识,希望对你有一定的参考价值。
下载centos镜像
# docker pull daocloud.io/centos:7
[root@localhost ~]# docker pull daocloud.io/centos:7 7: Pulling from centos a02a4930cb5d: Pull complete Digest: sha256:365fc7f33107869dfcf2b3ba220ce0aa42e16d3f8e8b3c21d72af1ee622f0cf0 Status: Downloaded newer image for daocloud.io/centos:7 [root@localhost ~]# [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE daocloud.io/centos 7 1e1148e4cc2c 7 weeks ago 202MB
创建一个镜像
# docker tag daocloud.io/centos:7 centos
[root@localhost ~]# docker tag daocloud.io/centos:7 centos [root@localhost ~]# [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE daocloud.io/centos 7 1e1148e4cc2c 7 weeks ago 202MB centos latest 1e1148e4cc2c 7 weeks ago 202MB
删除源镜像
# docker rmi daocloud.io/centos:7
[root@localhost ~]# docker rmi daocloud.io/centos:7 Untagged: daocloud.io/centos:7 Untagged: daocloud.io/centos@sha256:365fc7f33107869dfcf2b3ba220ce0aa42e16d3f8e8b3c21d72af1ee622f0cf0 [root@localhost ~]# [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 1e1148e4cc2c 7 weeks ago 202MB [root@localhost ~]#
创建一个/root/software/目录,并把安装包放在该目录下
[root@localhost ~]# mkdir software [root@localhost ~]# ls anaconda-ks.cfg jdk-7u67-linux-x64.tar.gz software apache-tomcat-7.0.55.tar.gz kjxy.war [root@localhost ~]# [root@localhost ~]# cp jdk-7u67-linux-x64.tar.gz apache-tomcat-7.0.55.tar.gz software/ [root@localhost ~]# [root@localhost ~]# cd software/ [root@localhost software]# [root@localhost software]# ls apache-tomcat-7.0.55.tar.gz jdk-7u67-linux-x64.tar.gz
启动容器
# docker run -it -v /root/software/:/mnt/software/ centos /bin/bash
[root@localhost ~]# docker run -it -v /root/software/:/mnt/software/ centos /bin/bash
[root@d8222810d886 /]#
[root@d8222810d886 /]# cd /mnt/software/
[root@d8222810d886 software]#
[root@d8222810d886 software]# ls
apache-tomcat-7.0.55.tar.gz jdk-7u67-linux-x64.tar.gz
为了搭建 Java Web 运行环境,需要安装 JDK 与 Tomcat,下面的过程均在容器内部进行。
安装JDK
创建jdk存储目录
# mkdir /usr/jdk64/
解压jdk到存储目录
# tar -zxf /mnt/software/jdk-7u67-linux-x64.tar.gz -C /usr/jdk64/
操作过程如下:
[root@d8222810d886 ~]# mkdir /usr/jdk64/ [root@d8222810d886 ~]# [root@d8222810d886 ~]# tar -zxf /mnt/software/jdk-7u67-linux-x64.tar.gz -C /usr/jdk64/ [root@d8222810d886 ~]# [root@d8222810d886 ~]# ls /usr/jdk64/ jdk1.7.0_67
安装tomcat
解压tomcat到存储目录
# tar -zxf /mnt/software/apache-tomcat-7.0.55.tar.gz -C /home/
[root@d8222810d886 ~]# tar -zxf /mnt/software/apache-tomcat-7.0.55.tar.gz -C /home/ [root@d8222810d886 ~]# [root@d8222810d886 ~]# ls /home/ apache-tomcat-7.0.55
[root@d8222810d886~]# mv /home/apache-tomcat-7.0.55/ /home/tomcat7
[root@d8222810d886~]# ls /home/
tomcat7
编写运行脚本
编写一个运行脚本,当启动容器时,运行该脚本,启动 Tomcat。
# vi /root/run.sh
[root@d8222810d886 ~]# vi run.sh [root@d8222810d886 ~]# [root@d8222810d886 ~]# cat run.sh
#!/bin/bash export JAVA_HOME=/usr/jdk64/jdk1.7.0_67 export PATH=$JAVA_HOME/bin:$PATH sh /home/tomcat7/bin/catalina.sh run
为运行脚本添加执行权限
# chmod 777 run.sh
[root@d8222810d886 ~]# chmod 777 run.sh [root@d8222810d886 ~]# [root@d8222810d886 ~]# ll total 8 -rw-------. 1 root root 3415 Dec 5 01:37 anaconda-ks.cfg -rwxrwxrwx. 1 root root 124 Jan 29 14:17 run.sh
给tomcat运行脚本添加权限
[root@d8222810d886 ~]# chmod +x /home/tomcat7/bin/*.sh [root@d8222810d886 ~]# [root@d8222810d886 ~]# ll /home/apache-tomcat-7.0.55/bin/*.sh -rwxr-xr-x. 1 root root 20779 Jul 18 2014 /home/apache-tomcat-7.0.55/bin/catalina.sh -rwxr-xr-x. 1 root root 1922 Jul 18 2014 /home/apache-tomcat-7.0.55/bin/configtest.sh -rwxr-xr-x. 1 root root 7888 Jul 18 2014 /home/apache-tomcat-7.0.55/bin/daemon.sh -rwxr-xr-x. 1 root root 1965 Jul 18 2014 /home/apache-tomcat-7.0.55/bin/digest.sh -rwxr-xr-x. 1 root root 3459 Jul 18 2014 /home/apache-tomcat-7.0.55/bin/setclasspath.sh -rwxr-xr-x. 1 root root 1902 Jul 18 2014 /home/apache-tomcat-7.0.55/bin/shutdown.sh -rwxr-xr-x. 1 root root 1904 Jul 18 2014 /home/apache-tomcat-7.0.55/bin/startup.sh -rwxr-xr-x. 1 root root 5024 Jul 18 2014 /home/apache-tomcat-7.0.55/bin/tool-wrapper.sh -rwxr-xr-x. 1 root root 1908 Jul 18 2014 /home/apache-tomcat-7.0.55/bin/version.sh
退出容器
[root@d8222810d886 ~]# exit exit [root@localhost ~]# [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d8222810d886 centos "/bin/bash" 16 minutes ago Exited (0) 5 seconds ago zealous_galileo
创建tomcat镜像
根据某个“容器 ID”来创建一个新的“镜像”
# docker commit d8222810d886 djltomcat:latest
[root@localhost ~]# docker commit d8222810d886 djltomcat:latest sha256:559211e0b347204543772eddb94c5b73810a8d2746fb30d9a24c99e33b89b3e9 [root@localhost ~]# [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE djltomcat latest 559211e0b347 22 seconds ago 508MB centos latest 1e1148e4cc2c 7 weeks ago 202MB
启动tomcat容器
首先,新建/root/webapps/目录,把war包拷贝到该目录
# mkdir webapps
[root@localhost ~]# ls anaconda-ks.cfg apache-tomcat-7.0.55.tar.gz jdk-7u67-linux-x64.tar.gz kjxy.war software [root@localhost ~]# [root@localhost ~]# mkdir webapps [root@localhost ~]# [root@localhost ~]# cp kjxy.war webapps/ [root@localhost ~]# [root@localhost ~]# ls webapps/ kjxy.war
编写一个运行脚本,当启动容器时,运行该脚本,启动 Tomcat
# docker run -d -p 58080:8080 -v /root/webapps/:/home/tomcat7/webapps/ --name test djltomcat:latest /root/run.sh
[root@localhost ~]# docker run -d -p 58080:8080 -v /root/webapps/:/home/tomcat7/webapps/ --name test djltomcat:latest /root/run.sh 87f68c345d3bd42bd4a87de96430e7dfa7f047688b66f0ea52d1e1778a4a842b [root@localhost ~]# [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 87f68c345d3b djltomcat:latest "/root/run.sh" 20 seconds ago Up 18 seconds 0.0.0.0:58080->8080/tcp test [root@localhost ~]# [root@localhost ~]# cd webapps [root@localhost webapps]# [root@localhost webapps]# ls kjxy kjxy.war
在浏览器中,输入宿主IP和端口号,即可访问 Tomcat:
参考博客:
使用Docker搭建Tomcat运行环境
https://www.cnblogs.com/areyouready/p/8949991.html
end
以上是关于docker构建tomcat镜像的主要内容,如果未能解决你的问题,请参考以下文章
Docker——Dockerfile(构建自己的CentOS镜像和Tomcat镜像)并打包镜像发布上线
Docker:在 Tomcat 镜像构建期间访问 MySQL 容器