Docker 学习笔记(九)-- Dockerfile 构建Tomcat 实战测试

1、准备镜像文件

Tomcat 压缩包: Apache Tomcat® - Welcome!

依赖的 JDK 压缩包 : [Java Downloads | Oracle](https://www.aliyundrive.com/s/YzH6o5G5QUu)

下载 后缀 tar.gz 文件。

[root@localhost home]$ mkdir -p mianbao/build/tomcat
[root@localhost home]$ cd mianbao/build/tomcat/
[root@localhost tomcat]$ ll
总用量 155812
-rw-r--r--. 1 root root  11543791 75 23:53 apache-tomcat-9.0.60.tar.gz
-rw-r--r--. 1 root root 148003421 75 23:53 jdk-8u333-linux-x64.tar.gz

2、编写 DockerFile 文件

[root@localhost tomcat]$ touch readme.txt
# 官方命名为 Dockerfile,build 会自动寻找这个文件,就不需要 -f 指定 dockerfile 文件了
[root@localhost tomcat]$ vim Dockerfile

Dockerfile 内容:

# 基础镜像:tag
FROM centos:centos7
# 作者信息
MAINTAINER mianabo<[email protected]>
# 拷贝文件
COPY readme.txt /usr/local/readme.txt
# 添加文件并解压到 /usr/local 目录下
ADD jdk-8u333-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.60.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_333
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.60
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.60
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
# 暴露端口
EXPOSE 8080
# 运行 Tomcat 和 输出日志
CMD /usr/local/apache-tomcat-9.0.60/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.60/logs/catalina.out

3、构建 Tomcat 镜像

# . 表示当前目录找 Dockerfile 文件
[root@localhost tomcat]$ docker build -t my_tomcat:1.0 .
....
Successfully built 18c352ccb430
Successfully tagged my_tomcat:1.0
# 查看生成的镜像
[root@localhost tomcat]$ docker images
REPOSITORY        TAG       IMAGE ID       CREATED              SIZE
my_tomcat         1.0       18c352ccb430   About a minute ago   821MB

4、运行镜像

防火墙开启 9090 端口:

firewall-cmd --zone=public --add-port=9090/tcp --permanent
firewall-cmd --add-port=9090/tcp
firewall-cmd --reload
# 查看开启的端口
firewall-cmd --list-ports

运行镜像:

docker run -d -p 9090:8080 \
--name diy_tomcat \
-v /home/mianbao/build/tomcat/webapps/test:/usr/local/apache-tomcat-9.0.60/webapps/test \
-v /home/mianbao/build/tomcat/logs:/usr/local/apache-tomcat-9.0.60/logs \
my_tomcat:1.0

启动成功后查看容器:

[root@localhost tomcat]$ docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED         STATUS         PORTS                   NAMES
eab3b1d2c9c7   my_tomcat:1.0   "/bin/sh -c '/usr/lo…"   6 seconds ago   Up 5 seconds   0.0.0.0:9090->8080/tcp  diy_tomcat

Docker 学习笔记(九)-- Dockerfile 构建Tomcat 实战测试_第1张图片

5、访问测试

可以通过 :http://虚拟机IP:9090/ 访问到 Tomcat 的首页。

6、发布项目

由于我们做了卷挂载,我们直接在宿主机的挂载目录编写项目就可以发布了。即在 ..../test 目录下:

[root@localhost test]$ mkdir WEB-INF
[root@localhost test]$ cd WEB-INF/
[root@localhost WEB-INF]$ vim web.xml
[root@localhost WEB-INF]$ cd ../
[root@localhost test]$ vim index.jsp

web.xml


<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">
    
	<display-name>testdisplay-name>    
web-app>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
DOCTYPE html>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>hello, mianbaotitle>
    head>
    <body>
        -----------welcome------------
        <br />
        <%=" my docker tomcat. mianbao"%>
        <br />
        <br />
        <% System.out.println("-------my docker tomcat-------");%>
    body>
html>

7、访问测试

访问:http://IP:9090/test/

Docker 学习笔记(九)-- Dockerfile 构建Tomcat 实战测试_第2张图片

8、查看日志

[root@localhost tomcat]$ ll
drwxr-xr-x. 2 root root      4096 76 22:25 logs
[root@localhost tomcat]$ cd logs/
[root@localhost logs]$ ll
总用量 48
-rw-r-----. 1 root root  6289 76 00:27 catalina.2022-07-05.log
-rw-r-----. 1 root root  6710 76 22:32 catalina.2022-07-06.log
-rw-r-----. 1 root root 13092 76 22:45 catalina.out
-rw-r-----. 1 root root     0 76 00:27 host-manager.2022-07-05.log
-rw-r-----. 1 root root     0 76 22:25 host-manager.2022-07-06.log
-rw-r-----. 1 root root   408 76 00:27 localhost.2022-07-05.log
-rw-r-----. 1 root root   408 76 22:25 localhost.2022-07-06.log
-rw-r-----. 1 root root   747 76 00:30 localhost_access_log.2022-07-05.txt
-rw-r-----. 1 root root   458 76 22:45 localhost_access_log.2022-07-06.txt
-rw-r-----. 1 root root     0 76 00:27 manager.2022-07-05.log
-rw-r-----. 1 root root     0 76 22:25 manager.2022-07-06.log
[root@localhost logs]$ tail -f catalina.out 
06-Jul-2022 14:25:30.369 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/apache-tomcat-9.0.60/webapps/test]
06-Jul-2022 14:25:30.528 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/apache-tomcat-9.0.60/webapps/test] has finished in [159] ms
06-Jul-2022 14:25:30.541 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
06-Jul-2022 14:25:30.643 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [3011] milliseconds
06-Jul-2022 14:32:50.806 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.reload Reloading context [/test]
06-Jul-2022 14:32:50.806 INFO [Catalina-utility-2] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/test] has started
06-Jul-2022 14:32:50.962 INFO [Catalina-utility-2] org.apache.catalina.core.StandardContext.reload Reloading Context with name [/test] is completed
-------my docker tomcat-------

你可能感兴趣的:(Docker,docker,tomcat)