下载centos镜像
# docker pull daocloud.io/centos:7
[[email protected] ~]# 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 [[email protected] ~]# [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE daocloud.io/centos 7 1e1148e4cc2c 7 weeks ago 202MB
创建一个镜像
# docker tag daocloud.io/centos:7 centos
[[email protected] ~]# docker tag daocloud.io/centos:7 centos [[email protected] ~]# [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE daocloud.io/centos 7 1e1148e4cc2c 7 weeks ago 202MB centos latest 1e1148e4cc2c 7 weeks ago 202MB
创建一个/root/software/目录,并把安装包放在该目录下
[[email protected] ~]# mkdir software [[email protected] ~]# ls anaconda-ks.cfg jdk-7u67-linux-x64.tar.gz software apache-tomcat-7.0.55.tar.gz kjxy.war [[email protected] ~]# [[email protected] ~]# cp jdk-7u67-linux-x64.tar.gz apache-tomcat-7.0.55.tar.gz software/ [[email protected] ~]# [[email protected] ~]# cd software/ [[email protected] software]# [[email protected] 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
[[email protected] ~]# docker run -it -v /root/software/:/mnt/software/ centos /bin/bash
[[email protected] /]#
[[email protected] /]# cd /mnt/software/
[[email protected] software]#
[[email protected] 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/
操作过程如下:
[[email protected] ~]# mkdir /usr/jdk64/ [[email protected] ~]# [[email protected] ~]# tar -zxf /mnt/software/jdk-7u67-linux-x64.tar.gz -C /usr/jdk64/ [[email protected] ~]# [[email protected] ~]# ls /usr/jdk64/ jdk1.7.0_67
安装tomcat
解压tomcat到存储目录
# tar -zxf /mnt/software/apache-tomcat-7.0.55.tar.gz -C /home/
[[email protected] ~]# tar -zxf /mnt/software/apache-tomcat-7.0.55.tar.gz -C /home/ [[email protected] ~]# [[email protected] ~]# ls /home/ apache-tomcat-7.0.55
编写运行脚本
编写一个运行脚本,当启动容器时,运行该脚本,启动 Tomcat。
# vi /root/run.sh
[[email protected] ~]# vi run.sh [[email protected] ~]# [[email protected] ~]# cat run.sh export JAVE_HOME=/usr/jdk64/jdk1.7.0_67 export PATH=$JAVE_HOME/bin:$PATH sh /home/apache-tomcat-7.0.55/bin/catalina.sh run
为运行脚本添加执行权限
# chmod 777 run.sh
[[email protected] ~]# chmod 777 run.sh [[email protected] ~]# [[email protected] ~]# 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运行脚本添加权限
[[email protected] ~]# chmod +x /home/apache-tomcat-7.0.55/bin/*.sh [[email protected] ~]# [[email protected] ~]# 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
退出容器
[[email protected] ~]# exit exit [[email protected] ~]# [[email protected] ~]# 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
[[email protected] ~]# docker commit d8222810d886 djltomcat:latest sha256:559211e0b347204543772eddb94c5b73810a8d2746fb30d9a24c99e33b89b3e9 [[email protected] ~]# [[email protected] ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE djltomcat latest 559211e0b347 22 seconds ago 508MB daocloud.io/centos 7 1e1148e4cc2c 7 weeks ago 202MB centos latest 1e1148e4cc2c 7 weeks ago 202MB
启动tomcat容器
首先,新建/root/webapps/目录,把war包拷贝到该目录
# mkdir webapps
[[email protected] ~]# ls anaconda-ks.cfg apache-tomcat-7.0.55.tar.gz jdk-7u67-linux-x64.tar.gz kjxy.war software [[email protected] ~]# [[email protected] ~]# mkdir webapps [[email protected] ~]# [[email protected] ~]# cp kjxy.war webapps/ [[email protected] ~]# [[email protected] ~]# ls webapps/ kjxy.war
编写一个运行脚本,当启动容器时,运行该脚本,启动 Tomcat
# docker run -d -p 58080:8080 -v /root/webapps/:/usr/tomcat/apache-tomcat-7.0.92/webapps/ --name mytomcat_1 mytomcat:1.0 /root/run.sh
[[email protected] ~]# docker run -d -p 58080:8080 -v /root/webapps/:/home/apache-tomcat-7.0.55/webapps/ --name test djltomcat:1.0 /root/run.sh c4a5471da148763e009453575648d869e0327222605456767ff034887d57d608 [[email protected] ~]#
在浏览器中,输入宿主IP和端口号,即可访问 Tomcat:
参考博客:
使用Docker搭建Tomcat运行环境
https://www.cnblogs.com/areyouready/p/8949991.html
end
原文地址:https://www.cnblogs.com/djlsunshine/p/10329192.html