第138天学习打卡(Docker 实战测试 Tomcat)

Posted doudoutj

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第138天学习打卡(Docker 实战测试 Tomcat)相关的知识,希望对你有一定的参考价值。

实战测试

Docker Hub中99%镜像都是从这个基础镜像过来的FROM scratch ,然后配置需要的软件和配置来进行的构建。

image-20210526143825129

创建一个自己的centos

[root@kuangshen ~]# cd /
[root@kuangshen /]# clear
[root@kuangshen /]# cd /home
[root@kuangshen home]# ls
apache-tomcat-9.0.44-windows-x64  ceshi                    kuangshen       qinjiang                         test.java
apache-tomcat-9.0.45              docker-test-volume       kuangshen.java  redis                            www
apache-tomcat-9.0.45.tar.gz       jdk-8u291-linux-x64.rpm  mysql           swagger-demo-0.0.1-SNAPSHOT.jar
[root@kuangshen home]# mkdir dockerfile
[root@kuangshen home]# ls
apache-tomcat-9.0.44-windows-x64  ceshi               jdk-8u291-linux-x64.rpm  mysql     swagger-demo-0.0.1-SNAPSHOT.jar
apache-tomcat-9.0.45              dockerfile          kuangshen                qinjiang  test.java
apache-tomcat-9.0.45.tar.gz       docker-test-volume  kuangshen.java           redis     www
[root@kuangshen home]# cd dockerfile
[root@kuangshen dockerfile]# ls
[root@kuangshen dockerfile]# vim mydockerfile-centos

image-20210526150457461

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oBbrR4fE-1622102572550)(C:\\Users\\HP\\AppData\\Roaming\\Typora\\typora-user-images\\image-20210526145407592.png)]

pwd 命令来查看”当前工作目录“的完整路径

添加功能:

FROM centos
MAINTAINER kuangshen<24736743@qq.com>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80

CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash


image-20210526150118043

然后按esc键,输入:wq命令保存退出。

image-20210526150719271

#1. 编写Dockerfile的文件
[root@kuangshen dockerfile]# cat mydockerfile-centos
FROM centos
MAINTAINER kuangshen<24736743@qq.com>

ENV MYPATH /usr/local
WORKDIR $MYPATH

RUN yum -y install vim
RUN yum -y install net-tools

EXPOSE 80

CMD echo $MYPATH
CMD echo "----end----"
CMD /bin/bash

# 2.通过这个文件构建镜像
# docker build -f dockerfile文件路径 -t 镜像名:[tag]
[root@kuangshen dockerfile]# docker build -f mydockerfile-centos
"docker build" requires exactly 1 argument.
See 'docker build --help'.

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile
[root@kuangshen dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .
Sending build context to Docker daemon  2.048kB
Step 1/10 : FROM centos
 ---> 300e315adb2f
Step 2/10 : MAINTAINER kuangshen<24736743@qq.com>
 ---> Running in 3a7a2a9a9aab
Removing intermediate container 3a7a2a9a9aab
 ---> 801c422e5b05
Step 3/10 : ENV MYPATH /usr/local
 ---> Running in 9e146d3f70c0
Removing intermediate container 9e146d3f70c0
 ---> f1af1d780034
Step 4/10 : WORKDIR $MYPATH
 ---> Running in 9f891871174c
Removing intermediate container 9f891871174c
 ---> 6676cda251f0
Step 5/10 : RUN yum -y install vim
 ---> Running in 906117676d62
CentOS Linux 8 - AppStream                      3.7 MB/s | 6.3 MB     00:01    
CentOS Linux 8 - BaseOS                         1.2 MB/s | 2.3 MB     00:01    
CentOS Linux 8 - Extras                         6.8 kB/s | 9.6 kB     00:01    
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-15.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-15.el8.x86_64.rpm        115 kB/s |  39 kB     00:00    
(2/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 296 kB/s |  48 kB     00:00    
(3/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm  1.8 MB/s | 1.4 MB     00:00    
(4/5): which-2.21-12.el8.x86_64.rpm             113 kB/s |  49 kB     00:00    
(5/5): vim-common-8.0.1763-15.el8.x86_64.rpm    6.2 MB/s | 6.3 MB     00:01    
--------------------------------------------------------------------------------
Total                                           4.2 MB/s | 7.8 MB     00:01     
warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - AppStream                      373 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       : 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-15.el8.x86_64                          4/5 
  Running scriptlet: gpm-libs-1.20.7-15.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-15.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-15.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 906117676d62
 ---> f56ca51b90b8
Step 6/10 : RUN yum -y install net-tools
 ---> Running in 384928d98541
Last metadata expiration check: 0:00:10 ago on Wed May 26 07:10:37 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   462 kB/s | 322 kB     00:00    
--------------------------------------------------------------------------------
Total                                           272 kB/s | 322 kB     00:01     
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 384928d98541
 ---> d7e9bb2d1271
Step 7/10 : EXPOSE 80
 ---> Running in f75080cd5eed
Removing intermediate container f75080cd5eed
 ---> 82b43e520a75
Step 8/10 : CMD echo $MYPATH
 ---> Running in e99fa39a873d
Removing intermediate container e99fa39a873d
 ---> f54e6fa1b453
Step 9/10 : CMD echo "----end----"
 ---> Running in 5a443ddf2f6b
Removing intermediate container 5a443ddf2f6b
 ---> aa4d15151e3e
Step 10/10 : CMD /bin/bash
 ---> Running in 13c55005ed95
Removing intermediate container 13c55005ed95
 ---> 300c9b0fd545
Successfully built 300c9b0fd545
Successfully tagged mycentos:0.1

# 3.测试运行

对比:之前原生的centos

image-20210526153559043

我们增加后的镜像:

image-20210526153505274

我们可以列出本地进行的变更历史:

image-20210526153832134

我们平时拿到一个镜像,可以研究一下它是怎么做的了。

CMD 和 ENTRYPOINT的区别

CMD        # 指定这个容器启动的时候要运行的命令,只有最后一个会生效, 可被替代
ENTRYPOINT #指定这个容器启动的时候要运行的命令,可以追加命令!

测试cmd:

[root@kuangshen dockerfile]# ls
mydockerfile-centos
#编写dockerfile文件
[root@kuangshen dockerfile]# vim dockerfile-cmd-test

FROM centos
CMD ["ls","-a"]

image-20210526154634298

#构建镜像
[root@kuangshen dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest .
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos
 ---> 300e315adb2f
Step 2/2 : CMD ["ls","-a"]
 ---> Running in 749b16e55bab
Removing intermediate container 749b16e55bab
 ---> ec083efa7e49
Successfully built ec083efa7e49
Successfully tagged cmdtest:latest
#run 运行     CMD  # 指定这个容器启动的时候要运行的命令,只有最后一个会生效, 可被替代
[root@kuangshen dockerfile]# docker run ec083efa7e49 #发现ls -a生效了
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
#项追加一个命令-l  返回的是   ls -al 展示列表的详细数据
[root@kuangshen dockerfile]# docker run ec083efa7e49 -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:367: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
#cmd的情况下,-l替换了CMD["LS","-a"] 命令,-l不是命令所以报错!
# 必须写完整命令才可以查看到
[root@kuangshen dockerfile]# docker run ec083efa7e49 ls -al
total 56
drwxr-xr-x   1 root root 4096 May 26 07:59 .
drwxr-xr-x   1 root root 4096 May 26 07:59 ..
-rwxr-xr-x   1 root root    0 May 26 07:59 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin

测试ENTRYPOINT

[root@kuangshen dockerfile]# vim dockerfile-cmd-entrypoint


ENTRYPOINTFROM centos
ENTRYPOINT ["ls","-a"]

[root@kuangshen dockerfile]# docker build -f dockerfile-cmd-entrypoint
"docker build" requires exactly 1 argument.
See 'docker build --help'.

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile
[root@kuangshen dockerfile]# docker build -f dockerfile-cmd-entrypoint -t entrypoint-test .
Sending build context to Docker daemon  4.096kB
Step 1/2 : FROM centos
 ---> 300e315adb2f
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in 5cd3782eb276
Removing intermediate container 5cd3782eb276
 ---> 76e236292fce
Successfully built 76e236292fce
Successfully tagged entrypoint-test:latest
[root@kuangshen dockerfile]# docker run 76e236292fce
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
# 追加的命令,是直接拼接在我们的ENTRYPOINT命令后面的!
[root@kuangshen dockerfile]# docker run 76e236292fce -l
total 56
drwxr-xr-x   1 root root 4096 May 26 08:09 .
drwxr-xr-x   1 root root 4096 May 26 08:09 ..
-rwxr-xr-x   1 root root    0 May 26 08:09 .dockerenv
lrwxrwxrwx   1 root root    7 Nov  3  2020 bin -> usr/bin
drwxr-xr-x   5 root root  340 May 26 08:09 dev
drwxr-xr-x   1 root root 4096 May 26 08:09 etc
drwxr-xr-x   2 root root 4096 Nov  3  2020 home
lrwxrwxrwx   1 root root    7 Nov  3  2020 lib -> usr/lib
lrwxrwxrwx   1 root root    9 Nov  3  2020 lib64 -> usr/lib64
drwx------   2 root root 4096 Dec  4 17:37 lost+found
drwxr-xr-x   2 root root 4096 Nov  3  2020 media
drwxr-xr-x   2 root root 4096 Nov  3  2020 mnt
drwxr-xr-x   2 root root 4096 Nov  3  2020 opt
dr-xr-xr-x 137 root root    0 May 26 08:09 proc
dr-xr-x---   2 root root 4096 Dec  4 17:37 root
drwxr-xr-x  11 root root 4096 Dec  4 17:37 run
lrwxrwxrwx   1 root root    8 Nov  3  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root 4096 Nov  3  2020 srv
dr-xr-xr-x  13 root root    0 May 24 10:50 sys
drwxrwxrwt   7 root root 4096 Dec  4 17:37 tmp
drwxr-xr-x  12 root root 4096 Dec  4 17:37 usr
drwxr-xr-x  20 root root 4096 Dec  4 17:37 var

image-20210526160848845

image-20210526160427399

Dockerfile中很多的命令都是十分相似的,需要了解它们的区别,最好的学习方式就是对比它们然后测试效果。

实战:Tomcat镜像

1.准备镜像文件tomcat压缩包,jdk压缩包!

image-20210526163040413

image-20210527160225067

image-20210526164004624

2.编写dockerfile文件,官方命令 Dockerfile, build 会自动寻找这个文件,就不需要-f指定了。

[root@kuangshen home]# touch readme.txt
[root@kuangshen home]# vim Dockerfile


FROM centos
MAINTAINER kuangshen<24736743@qq.com>

COPY readme.txt /usr/local/readme.txt

ADD jdk-8u291-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.45.tar.gz /usr/local/

RUN yum -y install vim

ENV MYPATH /usr/local
WORKDIR $MYPATH

ENV JAVA_HOME /usr/local/jdk1.8.0_291
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.45
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.45
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin

EXPOSE 8080

CMD /usr/local/apache-tomcat-9.0.45/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.45/bin/logs/catalina.out

3.构建镜像

[root@kuangshen home]# docker build -t diytomcat .
  1. 启动镜像
[root@kuangshen home]# docker run -d -p 9092:8080 --name kuangshentomcat5 -v /home/test:/usr/local/apache-tomcat-9.0.45/webapps/test -v /home/tomcatlogs/:/usr/local/apache-tomcat-9.0.45/logs diytomcat
3f119c6138ed7a1f5b2e2707f8a942cf15ccfec35f937dc158efae2747446198
[root@kuangshen home]# docker exec -it 3f119c6138ed7 /bin/bash
[root@3f119c6138ed local]# ls
aegis  apache-tomcat-9.0.45  bin  etc  games  include  jdk1.8.0_291  lib  lib64  libexec  readme.txt  sbin  share  src
[root@3f119c6138ed local]# pwd
/usr/local
[root@3f119c6138ed local]# ls -l
total 56
drwxr-xr-x 3 root  root  4096 May 26 12:10 aegis
drwxr-xr-x 1 root  root  4096 May 26 09:18 apache-tomcat-9.0.45
drwxr-xr-x 2 root  root  4096 Nov  3  2020 bin
drwxr-xr-x 2 root  root  4096 Nov  3  2020 etc
drwxr-xr-x 2 root  root  4096 Nov  3  2020 games
drwxr-xr-x 2 root  root  4096 Nov  3  2020 include
drwxr-xr-x 8 10143 10143 4096 Apr  7 19:26 jdk1.8.0_291
drwxr-xr-x 2 root  root  4096 Nov  3  2020 lib
drwxr-xr-x 3 root  root  4096 Dec  4 17:37 lib64
drwxr-xr-x 2 root  root  4096 Nov  3  2020 libexec
-rw-r--r-- 1 root  root     0 May 26 09:18 readme.txt
drwxr-xr-x 2 root  root  4096 Nov  3  2020 sbin
drwxr-xr-x 5 root  root  4096 Dec  4 17:37 share
drwxr-xr-x 2 root  root  4096 Nov  3  2020 src
[root@3f119c6138ed local]# cd apache-tomcat-9.0.45/
[root@3f119c6138ed apache-tomcat-9.0.45]# ls
BUILDING.txt  CONTRIBUTING.md  LICENSE	NOTICE	README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  temp  webapps  work
[root@3f119c6138ed apache-tomcat-9.0.45]# ls -l
total 152
-rw-r----- 1 root root 18984 Mar 30 10:29 BUILDING.txt
-rw-r----- 1 root root  5587 Mar 30 10:29 CONTRIBUTING.md
-rw-r----- 1 root root 57092 Mar 30 10:29 LICENSE
-rw-r----- 1 root root  2333 Mar 30 10:29 NOTICE
-rw-r----- 1 root root  3257 Mar 30 10:29 README.md
-rw-r----- 1 root root  6898 Mar 30 10:29 RELEASE-NOTES
-rw-r----- 1 root root 16507 Mar 30 10:29 RUNNING.txt
drwxr-x--- 2 root root  4096 Mar 30 10:29 bin
drwx------ 1 root root  4096 May 26 12:10 conf
drwxr-x--- 2 root root  4096 Mar 30 10:29 lib
drwxr-x--- 1 root root  4096 May 26 12:10 logs
drwxr-x--- 2 root root  4096 Mar 30 10:29 temp
drwxr-x--- 7 root root  4096 Mar 30 10:29 webapps
drwxr-x--- 1 root root  4096 May 26 12:10 work
[root@3f119c6138ed apache-tomcat-9.0.45]# 

image-20210526201841423

image-20210526201922124

报这个错误的时候: curl: (56) Recv failure: Connection reset by peer

解决方法:

​ 1.看配置文件写错没有

​ 2.看jdk的版本是否对应自己的压缩包的版本,注意这里的配置得一样

ADD jdk-8u291-linux-x64.tar.gz /usr/local/

ENV JAVA_HOME /usr/local/jdk1.8.0_291

对应的端口要去阿里云的安全组里面配置:

5.访问测试:

image-20210526202419540

6.发布项目(由于做了卷挂载,我们直接在本地编写项目就可以发布了)

[root@kuangshen home]# cd test/
[root@kuangshen test]# pwd
/home/test
[root@kuangshen test]# mkdir WEB-INF
[root@kuangshen test]# ls
WEB-INF
[root@kuangshen test]# cd WEB-INF/
[root@kuangshen WEB-INF]# vim web.xml
[root@kuangshen WEB-INF]# cd ..
[root@kuangshen test]# ls
WEB-INF
[root@kuangshen test]# vim index.jsp
[root@kuangshen test]# cat 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 contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<h1>登录</h1>
<%--居中--%>
<div >
<%--    这里表单表示的意思是:以post方式提交表单,提交到外面的login请求--%>
    <form action="${pageContext.request.contextPath}/login" method="post">
        用户名: <input type="text" name="username"><br>
        密码: <input type="password"name="password"><br>
        爱好:
<%--        checkbox多选框--%>
        <input type="checkbox" name="hobbies" value="女孩">女孩
        <input type="checkbox"name="hobbies" value="代码">代码
        <input type="checkbox"name="hobbies" value="唱歌">唱歌
        <input type="checkbox"name="hobbies" value="电影">电影
        <br>
        <input type="submit">
    </form>
</div>
</body>
</html>


项目部署成功,可以直接访问页面:

image-20210527155729172

打印日志:

image-20210527155647577

B站学习地址:【狂神说Java】Docker最新超详细版教程通俗易懂_哔哩哔哩 (゜-゜)つロ 干杯~-bilibili

以上是关于第138天学习打卡(Docker 实战测试 Tomcat)的主要内容,如果未能解决你的问题,请参考以下文章

物联网服务NodeJs-5天学习第三天实战篇② ——基于物联网的WiFi自动打卡考勤系统

第135天学习打卡(Docker 可视化 镜像讲解 分层理解 commit镜像)

第141天学习打卡(Docker Docker Compose )

第139天学习打卡(Docker 发布自己的镜像 Docker网络)

第139天学习打卡(Docker 发布自己的镜像 Docker网络)

第142天学习打卡(Docker yaml规则 开源项目 Docker Swarm)