• Docker的学习第六天


    DockerFIle  

    Dockerfile介绍

    Dockerfile 是用来构建 docker镜像的文件,命令的参数脚本

    构建步骤:

    1、编写一个dockerfile文件

    2、docker build 构建成一个镜像

    3、docker run 运行镜像

    4、docker push 发布镜像(docker hub 、阿里云)

    找一个官方镜像,点击跳转到github路径下,如以下一个实例:

    FROM scratch
    ADD CentOS-8-Container-8.1.1911-20200113.3-layer.x86_64.tar.xz  /
    
    LABEL org.label-schema.schema-version="1.0" 
        org.label-schema.name="CentOS Base Image" 
        org.label-schema.vendor="CentOS" 
        org.label-schema.license="GPLv2" 
        org.label-schema.build-date="20200114" 
        org.opencontainers.image.title="CentOS Base Image" 
        org.opencontainers.image.vendor="CentOS" 
        org.opencontainers.image.licenses="GPL-2.0-only" 
        org.opencontainers.image.created="2020-01-14 00:00:00-08:00"
    
    CMD ["/bin/bash"]

    很多官方镜像都是基础包,很多功能没有,我们通常会自己搭建自己的镜像

    官方既然可以制作镜像,我们也可以

    DockerFile的构建过程

    基础知识:

    1、每个保留关键字都必须是大写

    2、执行从上到下顺序执行

    3、#表示注释

    4、每一个指令都会创建提交一个新的镜像层并提交

    Dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件,这个文件十分简单

    Docker镜像已经 成为了企业交互的标准,所以必须要掌握这部分内容

    Dockerfile:构建文件,定义了一切的步骤,类似于源代码

    DockerImage:镜像,通过Dockerfile构建生成的镜像,最终发布和运行的产品,类似于原来的war包

    dockerContrainer:容器,就是镜像运行起来提供服务

    DockerFile的指令

     以前都是使用别人的镜像,现在通过下面的命令生成自己的DockerFile,构建自己的镜像

     FROM :这是一个基础镜像

    MAINTAINER:镜像是谁写的,留姓名+邮箱

    RUN:镜像构建的时候需要运行的命令

    ADD:步骤,如添加tomcat镜像,这个tomcat压缩包,就是我们要添加的内容

    WORKDIR:镜像的工作目录

    VOLUME:要挂载的目录 

    EXPOSE:保留端口配置

    RUN:指定要运行的

    CMD:指定容器启动的时候要运行的命令,只有最后一个命令才生效,可被替代

    ENTRYPOINT:指定这个容器启动的时候要运行的命令,可以追加命令

    ONBUILD:当构建一个被继承 DockerFile 这个时候运行ONBUILD的指令,触发指令

    COPY: 类似ADD,uqf我们文件拷贝到镜像中

    ENV: 构建时设置环境变量

     实战练习:

    Docker hub中,99%镜像都是在这个基础镜像来FROM Search ,然后配置需要的软件和配置来进行构建

    #编辑一个DokerFIle文件
    [root@fengfang dockerfiles]# clear [root@fengfang dockerfiles]# pwd /home/dockerfiles [root@fengfang dockerfiles]# cat mydockerfile-centos FROM centos MAINTAINER aaron<liuxh616@126.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 [root@fengfang dockerfiles]#

    构建Dockerfile文件命令:

    docker build -f 构建文件路径 -t 镜像名:版本号 . 

    注意后面有一个点(.)

    以下为上述实例的构建过程代码

    [root@fengfang dockerfiles]# docker build -f mydockerfile-centos -t mycentos:1.01 .   #构建命令
    Sending build context to Docker daemon  2.048kB
    Step 1/10 : FROM centos
     ---> 831691599b88
    Step 2/10 : MAINTAINER aaron<liuxh616@126.com>
     ---> Running in d9583935fdb7
    Removing intermediate container d9583935fdb7
     ---> 872b0d94f40f
    Step 3/10 : ENV MYPATH /usr/local
     ---> Running in 3e1901548470
    Removing intermediate container 3e1901548470
     ---> bcd157a757e4
    Step 4/10 : WORKDIR $MYPATH
     ---> Running in 4dddaceafd0e
    Removing intermediate container 4dddaceafd0e
     ---> 8a3373442d03
    Step 5/10 : RUN yum -y install vim
     ---> Running in 8b7d5e75b2b4
    CentOS-8 - AppStream                            410 kB/s | 5.8 MB     00:14    
    CentOS-8 - Base                                 242 kB/s | 2.2 MB     00:09    
    CentOS-8 - Extras                                11 kB/s | 7.0 kB     00:00    
    Dependencies resolved.
    ================================================================================
     Package             Arch        Version                   Repository      Size
    ================================================================================
    Installing:
     vim-enhanced        x86_64      2:8.0.1763-13.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-13.el8         AppStream      6.3 M
     vim-filesystem      noarch      2:8.0.1763-13.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: 31 M
    Downloading Packages:
    (1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm        154 kB/s |  39 kB     00:00    
    (2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 267 kB/s |  48 kB     00:00    
    (3/5): which-2.21-12.el8.x86_64.rpm             357 kB/s |  49 kB     00:00    
    (4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm  355 kB/s | 1.4 MB     00:03    
    (5/5): vim-common-8.0.1763-13.el8.x86_64.rpm    396 kB/s | 6.3 MB     00:16    
    --------------------------------------------------------------------------------
    Total                                           209 kB/s | 7.8 MB     00:38     
    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-8 - AppStream                            1.6 MB/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-13.el8.noarch                2/5 
      Installing       : vim-common-2:8.0.1763-13.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-13.el8.x86_64                  5/5 
      Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64                  5/5 
      Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64                    5/5 
      Verifying        : gpm-libs-1.20.7-15.el8.x86_64                          1/5 
      Verifying        : vim-common-2:8.0.1763-13.el8.x86_64                    2/5 
      Verifying        : vim-enhanced-2:8.0.1763-13.el8.x86_64                  3/5 
      Verifying        : vim-filesystem-2:8.0.1763-13.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-13.el8.x86_64    
      vim-enhanced-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch
      which-2.21-12.el8.x86_64             
    
    Complete!
    Removing intermediate container 8b7d5e75b2b4
     ---> 1951731eb6d1
    Step 6/10 : RUN yum -y install net-tools
     ---> Running in d473ddb1187f
    Last metadata expiration check: 0:00:44 ago on Mon Jul 27 07:11:43 2020.
    Dependencies resolved.
    ================================================================================
     Package         Architecture Version                        Repository    Size
    ================================================================================
    Installing:
     net-tools       x86_64       2.0-0.51.20160912git.el8       BaseOS       323 k
    
    Transaction Summary
    ================================================================================
    Install  1 Package
    
    Total download size: 323 k
    Installed size: 1.0 M
    Downloading Packages:
    net-tools-2.0-0.51.20160912git.el8.x86_64.rpm   377 kB/s | 323 kB     00:00    
    --------------------------------------------------------------------------------
    Total                                            14 kB/s | 323 kB     00:23     
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
      Preparing        :                                                        1/1 
      Installing       : net-tools-2.0-0.51.20160912git.el8.x86_64              1/1 
      Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64              1/1 
      Verifying        : net-tools-2.0-0.51.20160912git.el8.x86_64              1/1 
    
    Installed:
      net-tools-2.0-0.51.20160912git.el8.x86_64                                     
    
    Complete!
    Removing intermediate container d473ddb1187f
     ---> 8c80d175f619
    Step 7/10 : EXPOSE 80
     ---> Running in 4eaadf509842
    Removing intermediate container 4eaadf509842
     ---> 4721d1005156
    Step 8/10 : CMD echo $MYPATH
     ---> Running in 0b022380972b
    Removing intermediate container 0b022380972b
     ---> e5cb250036ee
    Step 9/10 : CMD echo "--------end------"
     ---> Running in d17888ac0d47
    Removing intermediate container d17888ac0d47
     ---> 04988036cac7
    Step 10/10 : CMD /bin/bash
     ---> Running in c1653fb0fa45
    Removing intermediate container c1653fb0fa45
     ---> c0cfb44a7745
    Successfully built c0cfb44a7745
    Successfully tagged mycentos:1.01
    [root@fengfang dockerfiles]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
    mycentos            1.01                c0cfb44a7745        About a minute ago   295MB   #新构建成功的镜像
    aaron/centos        1.0                 c20bce8652d2        5 hours ago          215MB
    tomcat_aaron        1.0                 671de9f0cd4f        3 days ago           652MB
    tomcat              9.0                 b4f6a90c69a4        3 days ago           647MB
    tomcat              latest              b4f6a90c69a4        3 days ago           647MB
    mysql               5.7                 8679ced16d20        4 days ago           448MB
    nginx               latest              8cf1bfb43ff5        5 days ago           132MB
    centos              latest              831691599b88        5 weeks ago          215MB
    [root@fengfang dockerfiles]# 

    3、运行新构建的镜像

    [root@fengfang dockerfiles]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    mycentos            1.01                c0cfb44a7745        3 minutes ago       295MB
    aaron/centos        1.0                 c20bce8652d2        5 hours ago         215MB
    tomcat_aaron        1.0                 671de9f0cd4f        3 days ago          652MB
    tomcat              9.0                 b4f6a90c69a4        3 days ago          647MB
    tomcat              latest              b4f6a90c69a4        3 days ago          647MB
    mysql               5.7                 8679ced16d20        4 days ago          448MB
    nginx               latest              8cf1bfb43ff5        5 days ago          132MB
    centos              latest              831691599b88        5 weeks ago         215MB
    [root@fengfang dockerfiles]# docker run -it mycentos:1.01    #运行自己创建的镜像
    [root@d4a4b4cd2628 local]# pwd    #工作目录就是我们在Dockerfile里面定义的WORKDIR目录
    /usr/local 
    [root@d4a4b4cd2628 local]# ifconfig    #这个命令也已经可以支持了
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
            ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
            RX packets 7  bytes 586 (586.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 0  bytes 0 (0.0 B)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 0  bytes 0 (0.0 B)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    [root@d4a4b4cd2628 local]# 

    我们还可以通过docker history 镜像Id  查看某一镜像的历史变更记录

    [root@fengfang dockerfiles]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    mycentos            1.01                c0cfb44a7745        6 minutes ago       295MB
    aaron/centos        1.0                 c20bce8652d2        5 hours ago         215MB
    tomcat_aaron        1.0                 671de9f0cd4f        3 days ago          652MB
    tomcat              9.0                 b4f6a90c69a4        3 days ago          647MB
    tomcat              latest              b4f6a90c69a4        3 days ago          647MB
    mysql               5.7                 8679ced16d20        4 days ago          448MB
    nginx               latest              8cf1bfb43ff5        5 days ago          132MB
    centos              latest              831691599b88        5 weeks ago         215MB
    [root@fengfang dockerfiles]# docker history c0cfb44a7745
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    c0cfb44a7745        6 minutes ago       /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B                  
    04988036cac7        6 minutes ago       /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B                  
    e5cb250036ee        6 minutes ago       /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B                  
    4721d1005156        6 minutes ago       /bin/sh -c #(nop)  EXPOSE 80                    0B                  
    8c80d175f619        6 minutes ago       /bin/sh -c yum -y install net-tools             22.8MB              
    1951731eb6d1        7 minutes ago       /bin/sh -c yum -y install vim                   57.2MB              
    8a3373442d03        8 minutes ago       /bin/sh -c #(nop) WORKDIR /usr/local            0B                  
    bcd157a757e4        8 minutes ago       /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B                  
    872b0d94f40f        8 minutes ago       /bin/sh -c #(nop)  MAINTAINER aaron<liuxh616…   0B                  
    831691599b88        5 weeks ago         /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
    <missing>           5 weeks ago         /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
    <missing>           5 weeks ago         /bin/sh -c #(nop) ADD file:84700c11fcc969ac0…   215MB               
    [root@fengfang dockerfiles]# 

     现在说下CMD和ENTRYPOINT的区别

    CMD               :指定这个容器启动的时候运行的命令,只有最后一个会生效,可被替代

    ENTRYPOINT:指定这个容器启动的时候要运行的命令,可以追加命令

     我们使用CMD方式,后面追加命令会出错

    dockerfile的内容

    [root@fengfang dockerfiles]# cat dockerfile-cmd-test 
    FROM centos
    CMD ["ls","-a"]
    
    
    [root@fengfang dockerfiles]# 

    追加命令执行时会出错

    [root@fengfang dockerfiles]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
    cmdtest             latest              462e7d919412        About a minute ago   215MB
    <none>              <none>              32c99d82cf43        4 minutes ago        215MB
    mycentos            1.01                c0cfb44a7745        2 hours ago          295MB
    aaron/centos        1.0                 c20bce8652d2        7 hours ago          215MB
    tomcat_aaron        1.0                 671de9f0cd4f        3 days ago           652MB
    tomcat              9.0                 b4f6a90c69a4        4 days ago           647MB
    tomcat              latest              b4f6a90c69a4        4 days ago           647MB
    mysql               5.7                 8679ced16d20        4 days ago           448MB
    nginx               latest              8cf1bfb43ff5        5 days ago           132MB
    centos              latest              831691599b88        5 weeks ago          215MB
    [root@fengfang dockerfiles]# docker run -it 462e7d919412
    .   .dockerenv    dev  home  lib64       media  opt   root  sbin    sys  usr
    ..  bin        etc  lib   lost+found  mnt    proc  run   srv    tmp  var
    [root@fengfang dockerfiles]# docker run -it 462e7d919412 -l
    docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: "-l": executable file not found in $PATH": unknown.
    ERRO[0000] error waiting for container: context canceled 

    我们使用entrypoint方式来执行命令:

    [root@fengfang dockerfiles]# vim dockerfile-entrypoint
    [root@fengfang dockerfiles]# cat vim dockerfile-entrypoint 
    cat: vim: 没有那个文件或目录
    FROM centos
    ENTYRPOINT ["ls","-a"]
    
    [root@fengfang dockerfiles]# cat dockerfile-entrypoint 
    FROM centos
    ENTRYPOINT ["ls","-a"]
    
    [root@fengfang dockerfiles]# 

    我们在运行时加参数时就不会出错

    [root@fengfang dockerfiles]# docker build -f dockerfile-entrypoint -t entrypoint-test .   #构建镜像
    Sending build context to Docker daemon  4.096kB
    Step 1/2 : FROM centos
     ---> 831691599b88
    Step 2/2 : ENTRYPOINT ["ls","-a"]
     ---> Running in cff416af2740
    Removing intermediate container cff416af2740
     ---> 1332ea7ae09c
    Successfully built 1332ea7ae09c
    Successfully tagged entrypoint-test:latest
    [root@fengfang dockerfiles]# docker run -it 1332ea7ae09c -t   #运行时加参数时不会出错,但使用CMD时是会出错的
    dev   .   .dockerenv  sys   run  var  lost+found  home    lib64  mnt  sbin
    proc  ..  etc          root  tmp  usr  bin      lib    media  opt  srv
    [root@fengfang dockerfiles]# docker run -it 1332ea7ae09c
    .   .dockerenv    dev  home  lib64       media  opt   root  sbin    sys  usr
    ..  bin        etc  lib   lost+found  mnt    proc  run   srv    tmp  var
    [root@fengfang dockerfiles]# docker run -it 1332ea7ae09c -l
    total 0
    drwxr-xr-x.   1 root root   6 Jul 27 09:10 .
    drwxr-xr-x.   1 root root   6 Jul 27 09:10 ..
    -rwxr-xr-x.   1 root root   0 Jul 27 09:10 .dockerenv
    lrwxrwxrwx.   1 root root   7 May 11  2019 bin -> usr/bin
    drwxr-xr-x.   5 root root 360 Jul 27 09:10 dev
    drwxr-xr-x.   1 root root  66 Jul 27 09:10 etc
    drwxr-xr-x.   2 root root   6 May 11  2019 home
    lrwxrwxrwx.   1 root root   7 May 11  2019 lib -> usr/lib
    lrwxrwxrwx.   1 root root   9 May 11  2019 lib64 -> usr/lib64
    drwx------.   2 root root   6 Jun 11 02:35 lost+found
    drwxr-xr-x.   2 root root   6 May 11  2019 media
    drwxr-xr-x.   2 root root   6 May 11  2019 mnt
    drwxr-xr-x.   2 root root   6 May 11  2019 opt
    dr-xr-xr-x. 160 root root   0 Jul 27 09:10 proc
    dr-xr-x---.   2 root root 162 Jun 11 02:35 root
    drwxr-xr-x.  11 root root 163 Jun 11 02:35 run
    lrwxrwxrwx.   1 root root   8 May 11  2019 sbin -> usr/sbin
    drwxr-xr-x.   2 root root   6 May 11  2019 srv
    dr-xr-xr-x.  13 root root   0 Jul 24 03:29 sys
    drwxrwxrwt.   7 root root 145 Jun 11 02:35 tmp
    drwxr-xr-x.  12 root root 144 Jun 11 02:35 usr
    drwxr-xr-x.  20 root root 262 Jun 11 02:35 var
    [root@fengfang dockerfiles]# 

     实战Tomcat

    1、准备需用到的文件,jdk和tomcat文件以及readme.txt

    [root@fengfang lxh]# ll
    总用量 150716
    -rw-r--r--. 1 root root  11211292 7月  28 10:01 apache-tomcat-9.0.37.tar.gz
    -rw-r--r--. 1 root root       628 7月  28 10:22 Dockerfile
    -rw-r--r--. 1 root root 143111803 7月  28 10:04 jdk-8u261-linux-x64.tar.gz
    -rw-r--r--. 1 root root         0 7月  28 10:10 readme.txt
    [root@fengfang lxh]# 

    2、生成Dockerfile文件,并输入下面这些命令

    FROM centos
    MAINTAINER aaron<liuxh616@126.com>
    COPY readme.txt /usr/local/readme.txt
    ADD jdk-8u261-linux-x64.tar.gz /usr/local/
    ADD apache-tomcat-9.0.37.tar.gz /usr/local/
    
    RUN yum -y install vim
    
    ENV MYPATH /usr/local
    WORKDIR $MYPATH
    
    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.37
    ENV CATALINA_BASH  /usr/local/apache-tomcat-9.0.37
    ENV PATH $PATH;$JAVA_HOME/bin;$CATALINA_lib;$CATALINA_HOME/bin
    
    EXPOSE 8080
    
    CMD /usr/local/apache-tomcat-9.0.37/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.37/logs/catalina.out

    3、构建镜像文件

    [root@fengfang lxh]# docker build -f Dockerfile -t diytomcat .     #构建镜像   这里建议不加版本,默认就会latest,后续运行时也就不用再加版本号
    Sending build context to Docker daemon  154.3MB
    Step 1/15 : FROM centos
     ---> 831691599b88
    Step 2/15 : MAINTAINER aaron<liuxh616@126.com>
     ---> Using cache
     ---> 872b0d94f40f
    Step 3/15 : COPY readme.txt /usr/local/readme.txt
     ---> 3792dc8ee191
    Step 4/15 : ADD jdk-8u261-linux-x64.tar.gz /usr/local/
     ---> f7d38a3369c3
    Step 5/15 : ADD apache-tomcat-9.0.37.tar.gz /usr/local/
     ---> 614bec8c110a
    Step 6/15 : RUN yum -y install vim
     ---> Running in 1756750d1e03
    CentOS-8 - AppStream                            1.4 MB/s | 5.8 MB     00:04    
    CentOS-8 - Base                                 811 kB/s | 2.2 MB     00:02    
    CentOS-8 - Extras                               329  B/s | 7.0 kB     00:21    
    Dependencies resolved.
    ================================================================================
     Package             Arch        Version                   Repository      Size
    ================================================================================
    Installing:
     vim-enhanced        x86_64      2:8.0.1763-13.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-13.el8         AppStream      6.3 M
     vim-filesystem      noarch      2:8.0.1763-13.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: 31 M
    Downloading Packages:
    (1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm        279 kB/s |  39 kB     00:00    
    (2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 723 kB/s |  48 kB     00:00    
    (3/5): which-2.21-12.el8.x86_64.rpm             343 kB/s |  49 kB     00:00    
    (4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm  1.4 MB/s | 1.4 MB     00:00    
    (5/5): vim-common-8.0.1763-13.el8.x86_64.rpm    1.6 MB/s | 6.3 MB     00:04    
    --------------------------------------------------------------------------------
    Total                                           169 kB/s | 7.8 MB     00:47     
    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-8 - AppStream                            1.6 MB/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-13.el8.noarch                2/5 
      Installing       : vim-common-2:8.0.1763-13.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-13.el8.x86_64                  5/5 
      Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64                  5/5 
      Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64                    5/5 
      Verifying        : gpm-libs-1.20.7-15.el8.x86_64                          1/5 
      Verifying        : vim-common-2:8.0.1763-13.el8.x86_64                    2/5 
      Verifying        : vim-enhanced-2:8.0.1763-13.el8.x86_64                  3/5 
      Verifying        : vim-filesystem-2:8.0.1763-13.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-13.el8.x86_64    
      vim-enhanced-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch
      which-2.21-12.el8.x86_64             
    
    Complete!
    Removing intermediate container 1756750d1e03
     ---> 68f2b6584227
    Step 7/15 : ENV MYPATH /usr/local
     ---> Running in 9626734e80b4
    Removing intermediate container 9626734e80b4
     ---> ca54555aa07e
    Step 8/15 : WORKDIR $MYPATH
     ---> Running in def9afa94368
    Removing intermediate container def9afa94368
     ---> 2fe98cd6b5a4
    Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.8.0_261
     ---> Running in d11a3bbc9347
    Removing intermediate container d11a3bbc9347
     ---> 580b4655df41
    Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar;$JAVA_HOME/lib/tools.jar
     ---> Running in a662118a881f
    Removing intermediate container a662118a881f
     ---> 0f0c3eed7b1e
    Step 11/15 : ENV CATALINA_HOME  /usr/local/apache-tomcat-9.0.37
     ---> Running in 420471056ea1
    Removing intermediate container 420471056ea1
     ---> eab3fe5b5696
    Step 12/15 : ENV CATALINA_BASH  /usr/local/apache-tomcat-9.0.37
     ---> Running in 8d41a754060b
    Removing intermediate container 8d41a754060b
     ---> 076f1418b8b9
    Step 13/15 : ENV PATH $PATH;$JAVA_HOME/bin;$CATALINA_lib;$CATALINA_HOME/bin
     ---> Running in a7c7d3ee3d9a
    Removing intermediate container a7c7d3ee3d9a
     ---> 7c66f716e0d2
    Step 14/15 : EXPOSE 8080
     ---> Running in 8ccf00a03786
    Removing intermediate container 8ccf00a03786
     ---> 86cba0d544b1
    Step 15/15 : CMD /usr/local/apache-tomcat-9.0.37/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.37/logs/catalina.out
     ---> Running in 14c24d5999b2
    Removing intermediate container 14c24d5999b2
     ---> d023013d48b7
    Successfully built d023013d48b7       #新镜像ID
    Successfully tagged diytomcat:1.01

    4、启动镜像,查看镜像列表

    [root@fengfang ~]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    diytomcat           latest              d023013d48b7        30 minutes ago      641MB
    entrypoint-test     latest              1332ea7ae09c        18 hours ago        215MB
    cmdtest             latest              462e7d919412        18 hours ago        215MB
    <none>              <none>              32c99d82cf43        18 hours ago        215MB
    mycentos            1.01                c0cfb44a7745        20 hours ago        295MB
    aaron/centos        1.0                 c20bce8652d2        25 hours ago        215MB
    tomcat_aaron        1.0                 671de9f0cd4f        3 days ago          652MB
    tomcat              9.0                 b4f6a90c69a4        4 days ago          647MB
    tomcat              latest              b4f6a90c69a4        4 days ago          647MB
    mysql               5.7                 8679ced16d20        5 days ago          448MB
    nginx               latest              8cf1bfb43ff5        6 days ago          132MB
    centos              latest              831691599b88        5 weeks ago         215MB
    [root@fengfang ~]# 

    运行镜像命令:

    docker run -d -p 8080:8080 --name tomcat01 -v /home/lxh/test:/usr/local/apache-tomcat-9.0.37/webapps/test -v /home/lxh/tomcatlogs:/usr/local/apache-tomcat-9.0.37/logs diytomcat #diytomcat为镜像名称

    5、访问测试

    [root@fengfang ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
    7050fc938250        diytomcat:1.01      "/bin/sh -c '/usr/lo…"   7 minutes ago       Up 7 minutes        0.0.0.0:8080->8080/tcp   tomcat01
    [root@fengfang ~]# curl localhost:8080
    
    
    
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8" />
            <title>Apache Tomcat/9.0.37</title>
            <link href="favicon.ico" rel="icon" type="image/x-icon" />
            <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
            <link href="tomcat.css" rel="stylesheet" type="text/css" />
        </head>
    
        <body>
            <div id="wrapper">
    .........后面还有内容未展示出来

    6、发布项目,因为做了挂载,所以直接在本地发布项目即可

    创建目录 WEB-INF,然后创建web.xml文件,内容以下:

    <?xml version="1.0" encoding="UTF-8"?>
       <web-app xmlns="http://java.sun.com/xml/ns/javaee"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                                   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
               version="2.5">
    
       

    </web-app>

    创建index.jsp文件,放在test目录下测试内容如下:

    <%@ 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>首页</title>
    <style>
        *{
        padding:0;
        margin:0;
        font-family:"微软雅黑";
    }
    .header{
        height:72px;
        background:#458fce ;
    }
    .header .logo{
        color:#fff ;
        line-height:70px;
        font-size:30px;
        margin-left:20px;
        display:inline-block;
        text-align:center;
    
    }
    a {
        color: #fff ;
        text-decoration: none ;
    }
    .header .login{
        float:right;
        color:#fff ;
        line-height:72px;
        margin-right:2px;
        display:inline-block;
    }
    .banner{
        height:380px;
        overflow:hidden;
        background: #ddd;
    }
    </style>
    </head>
    <body>
        <div class="header">
            <div class="logo">web实践</div>
            <div class ="login">
                <a href ="javascript:void(0)">登录</a>
                <span>|</span> 
                <a href ="javascript:void(0)">故事</a>
            </div>
        </div>
    </body>
    </html>

    通过命令:curl localhost:8080/test/index.jsp,则可以访问页面

    我们还可以进入我们挂载的目录 :/home/lxh/tomcatlogs目录下,访问 catalina.out日志文件,里面的日志也是正常写入的。

    访问页面如下图所示:

     发布自己的镜像

    1、在地址https://hub.docker.com/中注册一个自己的账号,里面有个dockerid,需要自己填写一个数字,比如自己的19830616,和别人不重复就行

    2、在邮箱确认后,保证这个账号能正常登陆的。

    3、使用docker login -u liuxh616@126.com

    再输入密码就可以登陆了

    4、登陆成功后,使用docker push diytomcat:tag,提交镜像到服务器上

    5,若镜像没有版本号,可以使用docker tag 镜像id 镜像名称:版本号,给镜像增加版本号

    6、docker push 到服务器上,尽量要带上版本号如:

    docker push diytomcat:1.01

    将自己的镜像发布到阿里云上去

    1、登陆阿里云

    2、找到容器镜像服务

    3、新建一个命名空间,再创建一个镜像容器,然后再根据文档说明进行推送即可

     

    **************只要学不死就往死里学!************
  • 相关阅读:
    winform_webApiSelfHost
    手机访问网站自动跳转到手机版
    Linux常用命令
    css居中
    固定菜单
    BOM操作写法实例
    表格(合并单元格)
    倒计时
    分享到插件
    jquery获得下拉框的值
  • 原文地址:https://www.cnblogs.com/lxhaaron/p/13385465.html
Copyright © 2020-2023  润新知