准备jdk-8u211-linux-x64.tar.gz和apache-tomcat-9.0.50.tar.gz
[root@localhost java_tomcat]# ls
apache-tomcat-9.0.50.tar.gz c.txt Dockerfile jdk-8u211-linux-x64.tar.gz
编写Dockerfile文件
MAINTAINER zzyy
COPY c.txt /usr/local/cincontainer.txt
ADD jdk-8u211-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.50.tar.gz /usr/local
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_211
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.50
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.50
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:CATALINA_HOME/bin
EXPOSE 8080
#ENTRYPOINT ["/usr/local/apache-tomcat-9.0.50/bin/startup.sh"]
#CMD ["/usr/local/apache-tomcat-9.0.50/bin/catalina.sh","run"]
CMD /usr/local/apache-tomcat-9.0.50/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.50/bin/logs/catalina.out
开始构建镜像
[root@localhost java_tomcat]# docker build -t java_tomcat .
Sending build context to Docker daemon 206.5MB
Step 1/14 : FROM centos
---> 300e315adb2f
Step 2/14 : MAINTAINER zzyy
---> Using cache
---> 06ecc398817f
Step 3/14 : COPY c.txt /usr/local/cincontainer.txt
---> Using cache
---> eced77fa60f8
Step 4/14 : ADD jdk-8u211-linux-x64.tar.gz /usr/local
---> Using cache
---> e838b08a9b1c
Step 5/14 : ADD apache-tomcat-9.0.50.tar.gz /usr/local
---> Using cache
---> 0ca0be303253
Step 6/14 : ENV MYPATH /usr/local
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in cdb453274a24
Removing intermediate container cdb453274a24
---> a0122f42cd2a
Step 7/14 : WORKDIR $MYPATH
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 93b9c0e1be4e
Removing intermediate container 93b9c0e1be4e
---> b4ef2c38cbaa
Step 8/14 : ENV JAVA_HOME /usr/local/jdk1.8.0_211
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 8873a8a0723e
Removing intermediate container 8873a8a0723e
---> ef31b64b5203
Step 9/14 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 4a1fbe3259d5
Removing intermediate container 4a1fbe3259d5
---> be62bf5028ee
Step 10/14 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.50
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 7311f5038222
Removing intermediate container 7311f5038222
---> 2aa580626636
Step 11/14 : ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.50
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 7df0023ae793
Removing intermediate container 7df0023ae793
---> dd9d5ad026a4
Step 12/14 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:CATALINA_HOME/bin
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in fd762ef29052
Removing intermediate container fd762ef29052
---> 4605f8795086
Step 13/14 : EXPOSE 8080
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in 0c7019906fe9
Removing intermediate container 0c7019906fe9
---> f8e1f6a4106b
Step 14/14 : CMD /usr/local/apache-tomcat-9.0.50/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.50/bin/lalina.out
---> [Warning] IPv4 forwarding is disabled. Networking will not work.
---> Running in e419dd4b7a93
Removing intermediate container e419dd4b7a93
---> fe3d4763bebe
Successfully built fe3d4763bebe
Successfully tagged java_tomcat:latest
查看镜像
[root@localhost java_tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
java_tomcat latest fe3d4763bebe 15 seconds ago 632MB
使用上面构建的镜像运行容器
[root@localhost java_tomcat]# mkdir -p /tomcat9/{logs,test}
[root@localhost java_tomcat]# docker run -d -p 9080:8080 --name mytom -v /data/java_tomcat/tomcat9/test:/usr/local/apache-tomcat-9.0.50/webapps/test -v /data/java_tomcat/tomcat9/logs:/usr/local/apache-tomcat-9.0.50/webapps/logs --privileged=true java_tomcat
WARNING: IPv4 forwarding is disabled. Networking will not work.
c633e6a9e2126fadfe4ce8d3adfd995ef4c26e05de527158cb0ea0dffbe1c0d3
访问浏览器http://localhost:9080
看到这只猫表示这次构建镜像圆满成功
继续测试构建的镜像,添加测试的项目文件a.jsp和WEB-INF/web.xml
[root@localhost test]# ls
a.jsp WEB-INF
a.jsp内容如下:
<% page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
----------welcome--------
Hello World!<br/>
<%="i am in docker tomcat test"%>
<br>
<br>
<% System.out.println("===========Hello World!"); %>
</body>
</html>
web.xml内容如下:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>test</display-name>
</web-app>
重启容器
[root@localhost WEB-INF]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c633e6a9e212 java_tomcat "/bin/sh -c '/usr/lo…" 28 minutes ago Up 28 minutes 0.0.0.0:9080->8080/tcp, :::9080->8080/tcp mytom
[root@localhost WEB-INF]# docker restart c6
c6
访问http://localhost:9080/test/a.jsp
看到这个页面表示测试通过。
下载源码