阅读 2913
Docker高级学习(二) Dockerfile

Docker高级学习(二) Dockerfile

Dockerfile

简介

  • dockerfile简介
    • dockerfile是用来构建dokcer像的文件!命令参数脚本!
  • 构建步骤
    • 编写一个 dockerfile 文件
    • dockerbuild构建成为一个镜像
    • dockerrun运行镜像
    • Docker push 发布镜像(DockerHub、阿里云像仓库)
  • 查看官方是怎样构建的
    • image.png
    • 点击红色的框,会跳转到github的地址中
    • image.png
    • 很多官方镜像都是基础包,很多的功能都没有,我们通常会自己搭建自己的镜像
    • 官方既然可以制作镜像,我们也可以
  • Dockerfile的编写规范
    1. 每一个保留关键字(指令)都是必须是大写字母
    2. 执行从上到下顺序执行
    3. #表示注释
    4. 每一个指令都会创建提交一个新的镜像层,并提交
  • image.png
  • Dockerfile的深远影响
    1. dockerfile是面向开发的,我们以后要发布项目,做像,就需要编写dockerfile文件,这个文件十分简单!
    2. Docker镜像逐渐成为企业交付的标准,必须要掌握!
    3. 步骤:开发,部署,运维。。。缺一不可!
      • DockerFiIe构建文件,定义了一切的步骤,源代码 (自己开发)
      • Dockerlmages 通过DockerFiIe构建生成的像,最终发布和运行的产品! (使用)
      • Docker容器容器就是镜像运行起来提供服务器

Dockerfile指令

  • image.png
  • image.png

实战

  • Dockerfile的命令
FROM centos

MAINTAINER waws<16601203140@163.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
复制代码
  • 安装过程的效果
Sending build context to Docker daemon  2.048kB
Step 1/10 : FROM centos                                                                                 # 安装从centos开始 
---> 0d120b6ccaa8
Step 2/10 : MAINTAINER waws<16601203140@163.com>                     # 作者信息
---> Running in dd1d454e365b
Removing intermediate container dd1d454e365b
---> 0eb159a21f67
Step 3/10 : ENV MYPATH /usr/local                                                               #  环境变量
---> Running in 4b61abda7759
Removing intermediate container 4b61abda7759
---> 2597f9cf4496
Step 4/10 : WORKDIR @MYPATH                                                                   # 设定工作目录
---> Running in 527632c7d29f
Removing intermediate container 527632c7d29f
---> d6cd1780dfb5
Step 5/10 : RUN yum -y install vim                                                                   # 从网络上下载vim 和 net-tools
---> Running in 5867417fe57a
CentOS-8 - AppStream                            2.7 MB/s | 5.8 MB     00:02    
CentOS-8 - Base                                 2.0 MB/s | 2.2 MB     00:01    
CentOS-8 - Extras                               7.4 kB/s | 8.1 kB     00:01    
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         37 kB/s |  39 kB     00:01    
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 545 kB/s |  48 kB     00:00    
(3/5): which-2.21-12.el8.x86_64.rpm             194 kB/s |  49 kB     00:00    
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm  771 kB/s | 1.4 MB     00:01    
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm    1.3 MB/s | 6.3 MB     00:04    
--------------------------------------------------------------------------------
Total                                           1.2 MB/s | 7.8 MB     00:06     
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 5867417fe57a
---> c72800950e21
Step 6/10 : RUN yum -y install net-tools
---> Running in 09f5890b9264
Last metadata expiration check: 0:00:12 ago on Sat Oct  3 09:46:06 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   1.7 MB/s | 323 kB     00:00    
--------------------------------------------------------------------------------
Total                                           504 kB/s | 323 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.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 09f5890b9264
---> cd3a6c56f9e6
Step 7/10 : EXPOSE 80                                                                                     # 暴露80端口
---> Running in 5e71dc82b66d
Removing intermediate container 5e71dc82b66d
---> 7699d971e2de
Step 8/10 : CMD echo $MYPATH                                                                  # 执行命令
---> Running in cd1156844880
Removing intermediate container cd1156844880
---> 5f62b3148180
Step 9/10 : CMD echo "-------end-------"
---> Running in 44fd371fabe7
Removing intermediate container 44fd371fabe7
---> fc4fcc5350bc
Step 10/10 : CMD /bin/bash
---> Running in 47d3d9910051
Removing intermediate container 47d3d9910051
---> b6b678359c7e
Successfully built b6b678359c7e                                                                  # 构建成功的标志
Successfully tagged mycentos:latest
复制代码
  • 效果展示
    • image.png
    • image.png

CMD和ENTRYPOINT的区别

  • CMD是替换命令
    • dockerfile的内容
      FROM centos
      CMD ["ls","-a"]
      复制代码
    • 效果
      • image.png
  • ENTRYPOINT是追加命令
    • dockerfile的内容
      FROM centos
      ENTRYPOINT ["ls","-a"]
      复制代码
    • 效果
      • image.png

实战tomcat

  • 首先准备镜像需要的文件,tomcat的安装包,jdk安装包(这个地方下载就行,注意数字对应)
    • image.png
  • 编写dockerfile文件,官方命名,build会直接自动寻找这个文件,就不需要-f进行文件的指定
FROM centos
MAINTAINER waws<16601203140@163.com>
COPY readme.txt /usr/local/readme.txt
ADD jdk-8u101-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.27.tar.gz /usr/local/
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_101
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.27
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.27/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.27/logs/catalina.out
复制代码
  • 构建镜像
    • Docker build -t diytomcat:1.0
  • 启动镜像
    docker run -d -p 9090:8080 --name tomcat01 -v /home/test:/usr/local/apache-tomcat-9.0.27/webapps/test -v /home/tomcatlogs/:/usr/local/apache-tomcat-9.0.27/logs diytomcat:1.0
    复制代码
  • 发布项目(由于做了卷挂载,我们直接在本地编写项目就可以发布了)
文章分类
开发工具
文章标签