原料:

tomcat、jdk安装包,dockerfile文件

步骤

1、准备压缩包

  1. jdk-8u301-linux-x64.tar.gz
  2. jdk1.8.0_301
  3. apache-tomcat-7.0.107.tar.gz
  4. apache-tomcat-7.0.107

image.png

2、编写dockerfile文件

官方指定命名Dockerfile,这样构建的时候不用加-f指定文件

  1. FROM centos
  2. MAINTAINER xiao<find_onepiece@163.com>
  3. COPY readme.txt /usr/local/readme.txt
  4. ADD jdk-8u301-linux-x64.tar.gz /usr/local
  5. ADD apache-tomcat-7.0.107.tar.gz /usr/local
  6. RUN yum -y install vim
  7. ENV MYPATH /usr/local
  8. WORKDIR $MYPATH
  9. ENV JAVA_HOME /usr/local/jdk1.8.0_301
  10. ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
  11. ENV CATALINA_HOME /usr/local/apache-tomcat-7.0.107
  12. ENV CATALINE_BASH /usr/local/apache-tomcat-7.0.107
  13. ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINE_HOME/bin
  14. EXPOSE 8080
  15. CMD /usr/local/apache-tomcat-7.0.107/bin/startup.sh && tail -f /usr/local/apache-tomcat-7.0.107/bin/logs/catalina.out

3、执行构建

  1. docker build -t diytomcat . #自动寻找Dockerfile文件

构建成功
Successfully built e68ad8caa2a4
Successfully tagged diytomcat:latest
image.png

4、启动容器

  1. /webapps/test 部署的项目路径,浏览器访问ip:port/test
  2. docker run -it -p 9090:8080 --name xiaoTomcat -v /home/xiao/build/tomcat/test:/usr/local/apache-tomcat-7.0.107/webapps/test -v /home/xiao/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-7.0.107/logs diytomcat

5、访问查看

数据卷挂载成功
image.png

6、发布项目

已经挂载了卷,所以编写宿主机的项目文件就可以了
在挂载路径 /home/xiao/build/tomcat/test 下新建文件
web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  6. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  7. </web-app>

index.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <body>
  4. <h2>Hello World!</h2>
  5. <h2>xiao</h2>
  6. <%
  7. System.out.println("my tomcat project logs");
  8. %>
  9. </body>
  10. </html>

7、浏览器访问

image.png

8、查看日志卷

image.png

完成从构建镜像到部署项目🎈🎈