1、准备创建好自己的目录

[root@paperchen paperchen]# pwd
/home/paperchen

[root@paperchen paperchen]# mkdir -p build/tomcat

[root@paperchen tomcat]# pwd
/home/paperchen/build/tomcat

2、在上述pwd的目录,即tomcat目录中创建文件等

touch readme.txt

vim Dockerfile

# vim Dockerfile
FROM centos

MAINTAINER paperchen<609120776@qq.com>

# 把宿主机当前上下文的readme.txt拷贝到容器/usr/local/路径下
COPY readme.txt /usr/local/cincontainer.txt

# 把java和tomcat添加到容器中(会自动解压)
ADD jdk-8u201-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.24.tar.gz /usr/local/

# 安装vim编辑器
RUN yum -y install vim
# 设置启动访问的初始位置,即工作目录,登录落脚点
ENV MYPATH /usr/local
WORKDIR $MYPATH

# 配置java和tomcat的环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_201
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINE_HOME /usr/local/apache-tomcat-9.0.24
ENV CATALINE_BASE /usr/local/apache-tomcat-9.0.24
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINE_HOME/lib:$CATALINE_HOME/bin

EXPOSE 8080
# 容器启动后要执行的命令
CMD /usr/local/apache-tomcat-9.0.24/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.24/bin/logs/catalina.out

3、当前tomcat目录下的所有文件内容

image

[root@paperchen tomcat]# ls -ll
total 197996
-rw-r--r-- 1 root root  10914435 Oct  6 08:46 apache-tomcat-9.0.24.tar.gz
-rw-r--r-- 1 root root       646 Oct  6 09:20 Dockerfile
-rw-r--r-- 1 root root 191817140 Oct  6 09:06 jdk-8u201-linux-x64.tar.gz
-rw-r--r-- 1 root root         0 Oct  6 08:44 readme.txt

4、在tomcat下构建镜像、查看镜像

当dockerfile文件直接命名为Dockerfile,且在同一目录下时可以省略-f dockerfile文件路径

[root@paperchen tomcat]# docker build -t paperchen/diytomcat
......
Successfully built b64cda437ad2
Successfully tagged paperchen/diytomcat:latest

查看构建后的镜像,确认构建成功

[root@paperchen tomcat]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
paperchen/diytomcat   latest              b64cda437ad2        37 minutes ago      684MB

5、运行启动构建的镜像

[root@paperchen tomcat]# docker run -d --privileged=true -p 9090:8080 --name mydiytomcat -v /home/paperchen/build/tomcat/test:/usr/local/apache-tomcat-9.0.24/webapps/test -v /home/paperchen/build/tomcat/tomcat9logs/:/usr/local/apache-tomcat-9.0.24/logs paperchen/diytomcat

测试访问:

curl localhost:9090或直接外网访问如:IP:9090

6、结合数据卷内容在外部直接发布测试应用test

[root@paperchen test]# pwd
/home/paperchen/build/tomcat/test
[root@paperchen test]# mkdir WEB-INF
[root@paperchen test]# cd WEB-INF/
[root@paperchen WEB-INF]# vim web.xml
[root@paperchen WEB-INF]# cd ..
[root@paperchen test]# vim index.jsp

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>hello,paperchen</title>
</head>
<body>
-----------welcome------------
<br>
" my docker tomcat,paperchen "
<br>
<%
    System.out.println("-------my docker tomcat-------");
%>
</body>
</html>

测试访问:

curl localhost:9090/test/index.jsp或直接外网访问如:IP:9090/test/index.jsp

image

成功!

7、查看日志信息

[root@paperchen tomcat]# cd tomcat9logs/
[root@paperchen tomcat9logs]# ls -ll
total 24
-rw-r----- 1 root root 6995 Oct  6 09:30 catalina.2020-10-06.log
-rw-r----- 1 root root 7057 Oct  6 09:37 catalina.out
-rw-r----- 1 root root    0 Oct  6 09:26 host-manager.2020-10-06.log
-rw-r----- 1 root root  406 Oct  6 09:26 localhost.2020-10-06.log
-rw-r----- 1 root root  245 Oct  6 09:37 localhost_access_log.2020-10-06.txt
-rw-r----- 1 root root    0 Oct  6 09:26 manager.2020-10-06.log
[root@paperchen tomcat9logs]# cat catalina.out
......
-------my docker tomcat-------
-------my docker tomcat-------

谢谢您看完这篇技术文章

如果能对您有所帮助

那将是一件很美好的事情

保持好奇心的终身学习也是极棒的事

愿世界简单又多彩

转载请注明出处

​ ——纸飞机


纸飞机78
18 声望4 粉丝