下载centos镜像
# docker pull daocloud.io/centos:7
[root@localhost ~]# docker pull daocloud.io/centos:
: Pulling from centos
a02a4930cb5d: Pull complete
Digest: sha256:365fc7f33107869dfcf2b3ba220ce0aa42e16d3f8e8b3c21d72af1ee622f0cf0
Status: Downloaded newer image for daocloud.io/centos:
[root@localhost ~]#
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
daocloud.io/centos 1e1148e4cc2c weeks ago 202MB
创建一个镜像
# docker tag daocloud.io/centos:7 centos
[root@localhost ~]# docker tag daocloud.io/centos: centos
[root@localhost ~]#
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
daocloud.io/centos 1e1148e4cc2c weeks ago 202MB
centos latest 1e1148e4cc2c weeks ago 202MB
删除源镜像
# docker rmi daocloud.io/centos:7
[root@localhost ~]# docker rmi daocloud.io/centos:
Untagged: daocloud.io/centos:
Untagged: daocloud.io/centos@sha256:365fc7f33107869dfcf2b3ba220ce0aa42e16d3f8e8b3c21d72af1ee622f0cf0
[root@localhost ~]#
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 1e1148e4cc2c 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..tar.gz kjxy.war
[root@localhost ~]#
[root@localhost ~]# cp jdk-7u67-linux-x64.tar.gz apache-tomcat-7.0..tar.gz software/
[root@localhost ~]#
[root@localhost ~]# cd software/
[root@localhost software]#
[root@localhost software]# ls
apache-tomcat-7.0..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..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..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..0_67
export PATH=$JAVA_HOME/bin:$PATH
sh /home/tomcat/bin/catalina.sh run
为运行脚本添加执行权限
# chmod 777 run.sh
[root@d8222810d886 ~]# chmod run.sh
[root@d8222810d886 ~]#
[root@d8222810d886 ~]# ll
total
-rw-------. root root Dec : anaconda-ks.cfg
-rwxrwxrwx. root root Jan : run.sh
给tomcat运行脚本添加权限
[root@d8222810d886 ~]# chmod +x /home/tomcat/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" minutes ago Exited () 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 seconds ago 508MB
centos latest 1e1148e4cc2c weeks ago 202MB
启动tomcat容器
首先,新建/root/webapps/目录,把war包拷贝到该目录
# mkdir webapps
[root@localhost ~]# ls
anaconda-ks.cfg apache-tomcat-7.0..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 : -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" seconds ago Up seconds 0.0.0.0:->/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