码迷,mamicode.com
首页 > 其他好文 > 详细

docker构建tomcat镜像

时间:2019-01-29 18:16:15      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:lin   nload   man   apache   rap   运行环境   containe   art   classpath   

下载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

 创建一个/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

 

编写运行脚本

编写一个运行脚本,当启动容器时,运行该脚本,启动 Tomcat。

# vi /root/run.sh

[root@d8222810d886 ~]# vi run.sh 
[root@d8222810d886 ~]# 
[root@d8222810d886 ~]# 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

[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/apache-tomcat-7.0.55/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
daocloud.io/centos   7                   1e1148e4cc2c        7 weeks ago         202MB
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/:/usr/tomcat/apache-tomcat-7.0.92/webapps/ --name mytomcat_1 mytomcat:1.0 /root/run.sh

[root@localhost ~]# 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
[root@localhost ~]# 

 

 在浏览器中,输入宿主IP和端口号,即可访问 Tomcat:

技术分享图片

 

 

 

参考博客:

使用Docker搭建Tomcat运行环境

https://www.cnblogs.com/areyouready/p/8949991.html

end

docker构建tomcat镜像

标签:lin   nload   man   apache   rap   运行环境   containe   art   classpath   

原文地址:https://www.cnblogs.com/djlsunshine/p/10329192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!