制作Tomcat的Dockerfile

接下来将编写一个Tomcat的Dockerfile文件,以此构建一个Docker镜像,并基于该镜像启动得到一个Tomcat容器,最后试着在该容器中部署一个测试项目

思路:

  • 基于centos镜像,配置一个jdk环境和tomcat环境,并进行环境变量的配置

1 jdk和tomcat下载

自行到网上下载jdk和tomcat的安装包,并上传到自己的服务器的任意目录下,如下图,我这里放在了/opt/web_app/docker_test目录下:
在这里插入图片描述

2 编写Dockerfile文件

在jdk和tomcat的安装包的同级目录也就是/opt/web_app/docker_test目录下,编写Dockerfile文件。

注:编写的Dockerfile文件的命名,最好就叫做Dockerfile,官方推荐的命名方式就是Dockerfile,这么命名,那么在使用docker build构建的时候,就会自动去寻找这个文件,而不需要使用-f去指定了文件!

Dockerfile文件的内容如下:

FROM centos

#声明镜像是谁写的
MAINTAINER suhaha<[email protected]>

#从本地将jdk和tomcat的压缩包,复制到容器的/usr/local目录下去,并自动解压
ADD jdk-8u181-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.54.tar.gz /usr/local

#声明工作目录,并通过WORKDIR命令,切换到工作目录
ENV MYPATH /usr/local
WORKDIR $MYPATH

#环境变量设置,最终将java和tomcat都配置到PATH中
ENV JAVA_HOME /usr/local/jdk1.8.0_181
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:#JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.54
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.54
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

#使用RUN命令,通过yum安装一些日常的常用工具
RUN yum -y install vim
RUN yum -y install net-tools

#对外暴露8080端口
EXPOSE 8080

#执行tomcat的启动命令,并通过tail -f命令,持续跟踪日志输出
CMD /usr/local/apache-tomcat-9.0.54/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.54/bin/logs/catalina.out

3 构建

使用如下命令构建

docker build -t my-diy-tomcat .

构建日志如下:

[[email protected] web_app]# docker build -t my-diy-tomcat .
Sending build context to Docker daemon  197.2MB
Step 1/15 : FROM centos
 ---> 5d0da3dc9764
Step 2/15 : MAINTAINER suhaha<[email protected]>
 ---> Running in 0b9bccd0217d
Removing intermediate container 0b9bccd0217d
 ---> a97454015399
Step 3/15 : ADD jdk-8u181-linux-x64.tar.gz /usr/local
 ---> fc5754e8aa9d
Step 4/15 : ADD apache-tomcat-9.0.54.tar.gz /usr/local
 ---> b804f9920f02
Step 5/15 : ENV MYPATH /usr/local
 ---> Running in c69d11a02e1d
Removing intermediate container c69d11a02e1d
 ---> 65fd2b51b346
Step 6/15 : WORKDIR $MYPATH
 ---> Running in 89ffdb849840
Removing intermediate container 89ffdb849840
 ---> ff9bf6740a9f
Step 7/15 : ENV JAVA_HOME /usr/local/jdk1.8.0_181
 ---> Running in cb3e97f0f5c3
Removing intermediate container cb3e97f0f5c3
 ---> 86e4466b3027
Step 8/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:#JAVA_HOME/lib/tools.jar
 ---> Running in f5f0a404edd8
Removing intermediate container f5f0a404edd8
 ---> 319cf44a7329
Step 9/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.54
 ---> Running in 10bd17f04960
Removing intermediate container 10bd17f04960
 ---> 2ee589f0ba3b
Step 10/15 : ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.54
 ---> Running in 0d94d8c3b3bd
Removing intermediate container 0d94d8c3b3bd
 ---> cbc2b4c9bad3
Step 11/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
 ---> Running in 2703da794149
Removing intermediate container 2703da794149
 ---> af6be7727c8a
Step 12/15 : RUN yum -y install vim
 ---> Running in a52f4bb0f584
CentOS Linux 8 - AppStream                       12 MB/s | 9.3 MB     00:00    
CentOS Linux 8 - BaseOS                          10 MB/s | 7.5 MB     00:00    
CentOS Linux 8 - Extras                         5.2 kB/s |  10 kB     00:02    
Dependencies resolved.
================================================================================
 Package             Arch        Version                   Repository      Size
================================================================================
Installing:
 vim-enhanced        x86_64      2:8.0.1763-15.el8         appstream      1.4 M
Installing dependencies:
 gpm-libs            x86_64      1.20.7-17.el8             appstream       39 k
 vim-common          x86_64      2:8.0.1763-15.el8         appstream      6.3 M
 vim-filesystem      noarch      2:8.0.1763-15.el8         appstream       48 k
 which               x86_64      2.21-12.el8               baseos          49 k

Transaction Summary
================================================================================
Install  5 Packages

Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm        2.9 MB/s |  39 kB     00:00    
(2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 3.5 MB/s |  48 kB     00:00    
(3/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm   15 MB/s | 1.4 MB     00:00    
(4/5): vim-common-8.0.1763-15.el8.x86_64.rpm     49 MB/s | 6.3 MB     00:00    
(5/5): which-2.21-12.el8.x86_64.rpm             468 kB/s |  49 kB     00:00    
--------------------------------------------------------------------------------
Total                                           8.9 MB/s | 7.8 MB     00:00     
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream                      1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <[email protected]>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : which-2.21-12.el8.x86_64                               1/5 
  Installing       : vim-filesystem-2:8.0.1763-15.el8.noarch                2/5 
  Installing       : vim-common-2:8.0.1763-15.el8.x86_64                    3/5 
  Installing       : gpm-libs-1.20.7-17.el8.x86_64                          4/5 
  Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          4/5 
  Installing       : vim-enhanced-2:8.0.1763-15.el8.x86_64                  5/5 
  Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64                  5/5 
  Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64                    5/5 
  Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          1/5 
  Verifying        : vim-common-2:8.0.1763-15.el8.x86_64                    2/5 
  Verifying        : vim-enhanced-2:8.0.1763-15.el8.x86_64                  3/5 
  Verifying        : vim-filesystem-2:8.0.1763-15.el8.noarch                4/5 
  Verifying        : which-2.21-12.el8.x86_64                               5/5 

Installed:
  gpm-libs-1.20.7-17.el8.x86_64         vim-common-2:8.0.1763-15.el8.x86_64    
  vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch
  which-2.21-12.el8.x86_64             

Complete!
Removing intermediate container a52f4bb0f584
 ---> 789176169e0a
Step 13/15 : RUN yum -y install net-tools
 ---> Running in 1c2d4d8e49d9
Last metadata expiration check: 0:00:08 ago on Thu Oct 21 05:37:29 2021.
Dependencies resolved.
================================================================================
 Package         Architecture Version                        Repository    Size
================================================================================
Installing:
 net-tools       x86_64       2.0-0.52.20160912git.el8       baseos       322 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm   1.9 MB/s | 322 kB     00:00    
--------------------------------------------------------------------------------
Total                                           600 kB/s | 322 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1 
  Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64              1/1 
  Verifying        : net-tools-2.0-0.52.20160912git.el8.x86_64              1/1 

Installed:
  net-tools-2.0-0.52.20160912git.el8.x86_64                                     

Complete!
Removing intermediate container 1c2d4d8e49d9
 ---> 5f0bbc8724b7
Step 14/15 : EXPOSE 8080
 ---> Running in af27169bf09e
Removing intermediate container af27169bf09e
 ---> 47dff1593e42
Step 15/15 : CMD /usr/local/apache-tomcat-9.0.54/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.54/bin/logs/catalina.out
 ---> Running in d4bfa4792e38
Removing intermediate container d4bfa4792e38
 ---> 33a8ac2f0649
Successfully built 33a8ac2f0649
Successfully tagged my-diy-tomcat:latest

构建图示如下:
在这里插入图片描述
在这里插入图片描述
构建完成之后,使用docker images命令,可以查看到我们构建的镜像:

[[email protected] web_app]# docker images | grep diy
my-diy-tomcat         latest    4c3b2ced86e8   About a minute ago   629MB

4 启动镜像

注:启动的时候,注意到我们这里通过-v命令挂载容器内的tomcat的webapps和logs日志目录到本地宿主机上,方便部署项目到容器内,也方便在本地宿主机查看日志。
命令如下

docker run -it -p 8080:8080 --name zhy_tomcat -v /home/docker/my-diy-tomcat/webapps/test:/usr/local/apache-tomcat-9.0.54/webapps/test -v /home/docker/my-diy-tomcat/logs/:/usr/local/apache-tomcat-9.0.54/logs my-diy-tomcat:latest

图示如下:
在这里插入图片描述
由上图可以看到tomcat启动成功
由于我们启动时通过-p 8080:8080命令做了端口映射(即将本地宿主机的8080端口,映射到容器内的8080端口上),因此接下来,测试从网页通过ip:8080端口去连接我们启动的tomcat,如下图,连接成功,证明tomcat容器启动成功,说明我们前面制作的tomcat的Dockerfile文件是没问题的!
在这里插入图片描述

5 部署项目tomcat中

接下来测试将一个测试项目test部署到容器中。

注:因为我们在上面启动镜像的时候,已经通过-v /home/docker/my-diy-tomcat/webapps/test:/usr/local/apache-tomcat-9.0.54/webapps/test命令做了数据卷映射,将本地宿主机的/home/docker/my-diy-tomcat/webapps/test目录,映射到了容器内的/usr/local/apache-tomcat-9.0.54/webapps/test目录。因此接下来我们测试部署项目到tomcat里面时,就不需要进入到容器内去部署了,只需要将项目部署到本地宿主机的/home/docker/my-diy-tomcat/webapps/test即可。

进入到目录下,创建一个WEB-INF目录,并进入到这新建的WEB-INF目录下,并创建一个web.xml文件,文件内容如下:
在这里插入图片描述
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">

  <display-name>Welcome to zjxl</display-name>
  <description>
     Welcome to zjxl
  </description>

</web-app>

接着回到test目录下,然后新建一个index.jsp文件,内容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello,zjxl</title>
</head>
<body>
zhanghongyan say: Hello World!<br/>
<%
System.out.println("----my test web logs----");
%>
</body>
</html>

如此,测试项目test就算是部署完成了,接着通过网页,去试着访问我们部署的测试项目!如下图,访问成功,项目部署ok!
在这里插入图片描述
并且因为在我们的index.jsp中,有如下这么一行代码

System.out.println("----my test web logs----");

所以,我们每在页面访问一次,就会打印此一次日志到tomcat日志中。我们可以在宿主机上直接查看到该日志(因为做了数据卷映射!):
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_20068025/article/details/120887532

相关文章