06 自定义Dockerfile

自己尝试编写一个tomcat9的Dockerfile

FROM centos
MAINTAINER simon<cm.t@qq.com>

#把宿主机当前上下文的c.txt拷贝到容器/usr/local/目录下
COPY c.txt /usr/local/cincontainer.txt

#把java与Tomcat添加到容器中
ADD apache-tomcat-9.0.38.tar.gz /usr/local/
ADD jdk-8u261-linux-x64.tar.gz /usr/local/

#安装wget工具
RUN yum install -y wget

#设置工作访问时候的workdir路径,登陆落脚点
ENV MYPATH /usr/local
WORKDIR $MYPATH

#配置java与tomcat环境变量

ENV JAVA_HOME /usr/local/jdk1.8.0_261
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.38
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.38
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

#容器运行时监听的端口
EXPOSE 8080

#启动tomcat
# ENTRYPOINT ["/usr/local/apache-tomcat-9.0.38/bin/startup.sh"]
# CMD ["/usr/local/apache-tomcat-9.0.38/bin/catalina.sh","run"]
CMD /usr/local/apache-tomcat-9.0.38/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.38/bin/logs/catalina.out

构建镜像

[root@localhost tomcat9]# podman build -f ./Dockerfile -t simon_tomcat9
STEP 1: FROM centos
STEP 2: MAINTAINER simon<cm.t@qq.com>
2f5a63fc1342c94164ccb83ca721898f16cf86185bcdc9346d79e027d9324a5d
STEP 3: COPY c.txt /usr/local/cincontainer.txt
b756e1663e8712bea344cf0aed0ac2ebde59e3f2a2e919a2c9ec1d46761bc278
STEP 4: ADD apache-tomcat-9.0.38.tar.gz /usr/local/
0443497b3f8cbcd489c89d053d0b258bb6ae4a65c4595101300dfe43e95dceff
STEP 5: ADD jdk-8u261-linux-x64.tar.gz /usr/local/
9a02c2aad25863a758d78230aa256088afedfe4b89812da3def99c2baac353b9
STEP 6: RUN yum install -y wget
CentOS-8 - AppStream                            4.1 MB/s | 5.8 MB     00:01    
CentOS-8 - Base                                 1.1 MB/s | 2.2 MB     00:01    
CentOS-8 - Extras                                10 kB/s | 8.1 kB     00:00    
Last metadata expiration check: 0:00:01 ago on Fri Sep 18 23:41:52 2020.
Dependencies resolved.
================================================================================
 Package                    Arch      Version                Repository    Size
================================================================================
Installing:
 wget                       x86_64    1.19.5-8.el8_1.1       AppStream    735 k
Installing dependencies:
 libpsl                     x86_64    0.20.2-5.el8           BaseOS        61 k
 publicsuffix-list-dafsa    noarch    20180723-1.el8         BaseOS        56 k

Transaction Summary
================================================================================
Install  3 Packages

Total download size: 852 k
Installed size: 3.0 M
Downloading Packages:
(1/3): libpsl-0.20.2-5.el8.x86_64.rpm           247 kB/s |  61 kB     00:00    .2 kB     --:-- ETA
(2/3): publicsuffix-list-dafsa-20180723-1.el8.n 168 kB/s |  56 kB     00:00    
(3/3): wget-1.19.5-8.el8_1.1.x86_64.rpm         2.0 MB/s | 735 kB     00:00    
--------------------------------------------------------------------------------
Total                                           648 kB/s | 852 kB     00:01     
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/wget-1.19.5-8.el8_1.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream                            977 kB/s | 1.6 kB     00:00    
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 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       : publicsuffix-list-dafsa-20180723-1.el8.noarch          1/3 
  Installing       : libpsl-0.20.2-5.el8.x86_64                             2/3 
  Installing       : wget-1.19.5-8.el8_1.1.x86_64                           3/3 
  Running scriptlet: wget-1.19.5-8.el8_1.1.x86_64                           3/3 
  Verifying        : wget-1.19.5-8.el8_1.1.x86_64                           1/3 
  Verifying        : libpsl-0.20.2-5.el8.x86_64                             2/3 
  Verifying        : publicsuffix-list-dafsa-20180723-1.el8.noarch          3/3 

Installed:
  libpsl-0.20.2-5.el8.x86_64     publicsuffix-list-dafsa-20180723-1.el8.noarch  
  wget-1.19.5-8.el8_1.1.x86_64  

Complete!
0c5acb5eb45dc2a6d88ac8db608463379d3158bc879510a6809108970c4898f6
STEP 7: ENV MYPATH /usr/local
ea6e5fd9e0890904cff9db930888d2c7c07b5f4815998dad78bb9028c7f980b5
STEP 8: WORKDIR $MYPATH
b1d727ace50c74d6b7fbde364c628368fd3f467f626161834289df3686bcc4bb
STEP 9: ENV JAVA_HOME /usr/local/jdk1.8.0_261
45c660fe629c8b0a6f0e092886edfc8b05827316cd4c69ba9d54d054afa1ad34
STEP 10: ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
a6dd584e3dc4b362b59d341b07f8ad81059ec6c42c337dcb2f07e1e99ffa7c11
STEP 11: ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.38
ee4ace0c5fa9c222fb8311f4895a2d985bb67dbacec65a6ae4650756c47b8f2f
STEP 12: ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.38
004c6740731727c81b5dc3c0ac97a2089f06543d2121d15e709680d63095ecd8
STEP 13: ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
1abf909fb75a31b27d1d18186a5a7582a70762f3a7163502712545f221022ab0
STEP 14: EXPOSE 8080
54e72f5e97b52b90c27790d4ac0f691add010f8e7640c35398ec96111e35de92
STEP 15: CMD /usr/local/apache-tomcat-9.0.38/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.38/bin/logs/catalina.out
STEP 16: COMMIT simon_tomcat9
c379423344cf83efbe56e470e18a1d4892e6ecf3bd1e6711c4a07cfe99c2f827
[root@localhost tomcat9]#


[root@localhost tomcat9]# podman images
REPOSITORY                                       TAG           IMAGE ID       CREATED          SIZE
localhost/simon_tomcat9                          latest        c379423344cf   10 seconds ago   624 MB
docker.io/library/centos                         latest        0d120b6ccaa8   5 weeks ago      222 MB

验证:

[root@localhost tomcat9]# podman run -d -p 9080:8080 --name simon_tom9 -v /simon_data/dockerfile/tomcat9/test:/usr/local/apache-tomcat-9.0.38/webapps/test -v /simon_data/dockerfile/tomcat9/tomcat9logs/:/usr/local/apache-tomcat-9.0.38/logs --privileged=true simon_tomcat9
65068137427d29756a112d34df808494a525dfa33d2ea24d80110f5c18182663

      
[root@localhost tomcat9]# 
[root@localhost tomcat9]# podman ps
CONTAINER ID  IMAGE                                                       COMMAND               CREATED         STATUS             PORTS                   NAMES
65068137427d  localhost/simon_tomcat9:latest                              /bin/sh -c /usr/l...  51 seconds ago  Up 51 seconds ago  0.0.0.0:9080->8080/tcp  simon_tom9

jsp测试:

[root@localhost ~]# cd /simon_data/dockerfile/tomcat9/test/
[root@localhost test]# ls
[root@localhost test]# cat a.jsp

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
  <head>
    <title>JSP test page.</title>
  </head>
  <body>
    <% out.println("Hello,world!"); %>
  </body>

[root@localhost test]#

附集群测试代码:

集群测试页面:
在TomcatA上某context中(如/test),提供如下页面

<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA </font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("abc","abc"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

在TomcatB上某context中(如/test),提供如下页面
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB </font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("abc","abc"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

浏览器打开:http://localhost:9080/test/a.jsp

posted @ 2022-11-24 20:43  oldSimon  阅读(94)  评论(0编辑  收藏  举报