DockerFile

我们要研究自己如何做一个镜像,而且我们写的微服务项目以及springboot打包上云部署,Docker就是 最方便的。

微服务打包成镜像,任何装了Docker的地方,都可以下载使用,极其的方便。

流程:开发应用=>DockerFile=>打包为镜像=>上传到仓库(私有仓库,公有仓库)=> 下载镜像 => 启动 运行。

还可以方便移植!

什么是DockerFile

dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本。 构建步骤:

  1. 编写DockerFile文件
  2. docker build 构建镜像
  3. docker run
  4. docker push 发布镜像(DockerHub 、阿里云镜像仓库)

dockerfile文件我们刚才已经编写过了一次,这里我们继续使用 centos 来看!

地址:https://hub.docker.com/_/centos

DockerFile_第1张图片

DockerFile_第2张图片

FROM scratch
ADD centos-7-x86_64-docker.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="20200504" \
    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-05-04 00:00:00+01:00"

CMD ["/bin/bash"]

DockerFile构建过程

基础知识 

  1. 每条保留字指令都必须为大写字母且后面要跟随至少一个参数
  2. 指令按照从上到下,顺序执行
  3. # 表示注释
  4. 每条指令都会创建一个新的镜像层,并对镜像进行提交

流程 

  1. docker从基础镜像运行一个容器
  2. 执行一条指令并对容器做出修改
  3. 执行类似 docker commit 的操作提交一个新的镜像层
  4. Docker再基于刚提交的镜像运行一个新容器
  5. 执行dockerfile中的下一条指令直到所有指令都执行完成!

说明

从应用软件的角度来看,DockerFile,docker镜像与docker容器分别代表软件的三个不同阶段。

  • DockerFile 是软件的原材料 (代码)
  • Docker 镜像则是软件的交付品 (.apk)
  • Docker 容器则是软件的运行状态 (客户下载安装执行)

DockerFile 面向开发,Docker镜像成为交付标准,Docker容器则涉及部署与运维,三者缺一不可

DockerFile_第3张图片

DockerFile:需要定义一个DockerFile,DockerFile定义了进程需要的一切东西。DockerFile涉及的内容 包括执行代码或者是文件、环境变量、依赖包、运行时环境、动态链接库、操作系统的发行版、服务进 程和内核进程(当引用进行需要和系统服务和内核进程打交道,这时需要考虑如何设计 namespace的权 限控制)等等。

Docker镜像:在DockerFile 定义了一个文件之后,Docker build 时会产生一个Docker镜像,当运行 Docker 镜像时,会真正开始提供服务;

Docker容器:容器是直接提供服务的。

DockerFile指令

关键字

FROM                           # 基础镜像,当前新镜像是基于哪个镜像的
MAINTAINER                     # 镜像维护者的姓名混合邮箱地址
RUN                            # 容器构建时需要运行的命令
EXPOSE                         # 当前容器对外保留出的端口
WORKDIR                        # 指定在创建容器后,终端默认登录的进来工作目录,一个落脚点
ENV                            # 用来在构建镜像过程中设置环境变量
ADD                            # 将宿主机目录下的文件拷贝进镜像且 ADD 命令会自动处理 URL 和解压 tar 压缩包
COPY                           # 类似ADD,拷贝文件和目录到镜像中!
VOLUME                         # 容器数据卷,用于数据保存和持久化工作
CMD                            # 指定一个容器启动时要运行的命令,dockerFile 中可以有多个 CMD 指令,但只有最后一个生效!
ENTRYPOINT                     # 指定一个容器启动时要运行的命令!和CMD一样
ONBUILD                        # 当构建一个被继承的 DockerFile 时运行命令,父镜像在被子镜像继承后,父镜像的 ONBUILD 被触发

DockerFile_第4张图片

DockerFile_第5张图片

实战:构建自己的CentOs

Docker Hub 中99% 的镜像都是通过在base镜像(Scratch)中安装和配置需要的软件构建出来的

DockerFile_第6张图片

自定义一个 centos

编写DockerFile

查看下官方默认的CentOS的情况:

DockerFile_第7张图片

目的:使我们自己的镜像具备如下:登陆后的默认路径、vim编辑器、查看网络配置ifconfig支持

准备编写DockerFlie文件

[root@xiaoyequ home]# mkdir dockerfile
[root@xiaoyequ home]# ls
dockerfile  docker-test-volume
[root@xiaoyequ home]# cd dockerfile
[root@xiaoyequ dockerfile]# vim mydockerfile-centos
[root@xiaoyequ dockerfile]# cat mydockerfile-centos
FROM centos
MAINTAINER xiaoyequ<[email protected]>

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

构建

docker build -f dockerfile地址 -t 新镜像名字:TAG .

会看到 docker build 命令最后有一个 .                . 表示当前目录****

[root@xiaoyequ dockerfile]# docker build -f mydockerfile-centos -t mycentos:0.1 .
Sending build context to Docker daemon  2.048kB
Step 1/10 : FROM centos
 ---> 470671670cac
Step 2/10 : MAINTAINER xiaoyequ<[email protected]>
 ---> Running in c97d0a352bc2
Removing intermediate container c97d0a352bc2
 ---> c56092384a02
Step 3/10 : ENV MYPATH /usr/local
 ---> Running in ff73ff482805
Removing intermediate container ff73ff482805
 ---> 3a3934c9346a
Step 4/10 : WORKDIR $MYPATH
 ---> Running in 80b1aeaa7b48
Removing intermediate container 80b1aeaa7b48
 ---> 44ef2034ac3b
Step 5/10 : RUN yum -y install vim
 ---> Running in 8515e9f531e1
CentOS-8 - AppStream                            317 kB/s | 7.0 MB     00:22    
CentOS-8 - Base                                 873 kB/s | 2.2 MB     00:02    
CentOS-8 - Extras                               8.1 kB/s | 6.7 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-10.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        210 kB/s |  39 kB     00:00    
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 147 kB/s |  48 kB     00:00    
(3/5): which-2.21-10.el8.x86_64.rpm             223 kB/s |  49 kB     00:00    
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm  542 kB/s | 1.4 MB     00:02    
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm    551 kB/s | 6.3 MB     00:11    
--------------------------------------------------------------------------------
Total                                           637 kB/s | 7.8 MB     00:12     
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) "
 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-10.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-10.el8.x86_64                               5/5 

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

Complete!
Removing intermediate container 8515e9f531e1
 ---> 95b49a652fcf
Step 6/10 : RUN yum -y install net-tools
 ---> Running in 81e5c3e7ad4b
Last metadata expiration check: 0:00:21 ago on Mon Jun 15 13:39:27 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   889 kB/s | 323 kB     00:00    
--------------------------------------------------------------------------------
Total                                           377 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 81e5c3e7ad4b
 ---> fe17a32549a5
Step 7/10 : EXPOSE 80
 ---> Running in 2808f77c4346
Removing intermediate container 2808f77c4346
 ---> c449a929e0e2
Step 8/10 : CMD echo $MYPATH
 ---> Running in ce2adef5c76c
Removing intermediate container ce2adef5c76c
 ---> 28288525952a
Step 9/10 : CMD echo "----------end--------"
 ---> Running in fa70e120126c
Removing intermediate container fa70e120126c
 ---> 68f2037e2ddf
Step 10/10 : CMD /bin/bash
 ---> Running in 9c21b869a3ad
Removing intermediate container 9c21b869a3ad
 ---> 0deef898a52e
Successfully built 0deef898a52e
Successfully tagged mycentos:0.1

运行

docker run -it 新镜像名字:TAG

docker run -it mycentos:0.1

 DockerFile_第8张图片

可以看到,我们自己的新镜像已经支持 vim、ifconfig的命令,扩展OK!

列出镜像地的变更历史

docker history 镜像名

docker history 0deef898a52e

DockerFile_第9张图片

CMD 和 ENTRYPOINT 的区别

两个命令都是指定一个容器启动时要运行的命令

CMD:Dockerfile 中可以有多个CMD 指令,但只有最后一个生效,CMD 会被 docker run 之后的参数 替换!

ENTRYPOINT: docker run 之后的参数会被当做参数传递给 ENTRYPOINT,之后形成新的命令组合!

CMD命令

构建dockerfile

[root@xiaoyequ home]# vim dockerfile-cmd-test
[root@xiaoyequ home]# cat dockerfile-cmd-test
FROM centos
CMD [ "ls", "-a" ]

build 镜像

[root@xiaoyequ home]# docker build -f dockerfile-cmd-test -t cmdtest .
Sending build context to Docker daemon   5.12kB
Step 1/2 : FROM centos
 ---> 470671670cac
Step 2/2 : CMD [ "ls", "-a" ]
 ---> Running in 3f0f8a4b3848
Removing intermediate container 3f0f8a4b3848
 ---> ad2fb09d1b81
Successfully built ad2fb09d1b81
Successfully tagged cmdtest:latest

执行

[root@xiaoyequ home]# docker run ad2fb09d1b81
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

如果我们希望用 -l 列表展示信息,我们就需要加上 -l参数

[root@xiaoyequ home]# docker run cmdtest -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 
  • 问题:我们可以看到可执行文件找不到的报错,executable file not found。
  • 之前我们说过,跟在镜像名后面的是 command,运行时会替换 CMD 的默认值。
  • 因此这里的 -l 替换了原来的 CMD,而不是添加在原来的 ls -a 后面。而 -l 根本不是命令,所 以自然找不到。
  • 那么如果我们希望加入 -l 这参数,我们就必须重新完整的输入这个命令:

docker run cmdtest ls -al

[root@xiaoyequ home]# docker run cmdtest ls -al
total 56
drwxr-xr-x  1 root root 4096 Jun 16 12:56 .
drwxr-xr-x  1 root root 4096 Jun 16 12:56 ..
-rwxr-xr-x  1 root root    0 Jun 16 12:56 .dockerenv
lrwxrwxrwx  1 root root    7 May 11  2019 bin -> usr/bin
drwxr-xr-x  5 root root  340 Jun 16 12:56 dev
drwxr-xr-x  1 root root 4096 Jun 16 12:56 etc
drwxr-xr-x  2 root root 4096 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 4096 Jan 13 21:48 lost+found
drwxr-xr-x  2 root root 4096 May 11  2019 media
drwxr-xr-x  2 root root 4096 May 11  2019 mnt
drwxr-xr-x  2 root root 4096 May 11  2019 opt
dr-xr-xr-x 97 root root    0 Jun 16 12:56 proc
dr-xr-x---  2 root root 4096 Jan 13 21:49 root
drwxr-xr-x 11 root root 4096 Jan 13 21:49 run
lrwxrwxrwx  1 root root    8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x  2 root root 4096 May 11  2019 srv
dr-xr-xr-x 13 root root    0 Jun 14 13:03 sys
drwxrwxrwt  7 root root 4096 Jan 13 21:49 tmp
drwxr-xr-x 12 root root 4096 Jan 13 21:49 usr
drwxr-xr-x 20 root root 4096 Jan 13 21:49 var

 

ENTRYPOINT命令 

构建dockerfile

[root@xiaoyequ home]# vim dockerfile-entrypoint-test
[root@xiaoyequ home]# cat dockerfile-entrypoint-test
FROM centos
ENTRYPOINT [ "ls", "-a" ]

build 镜像

[root@xiaoyequ home]# docker build -f dockerfile-entrypoint-test -t entrypointtest .
Sending build context to Docker daemon  6.144kB
Step 1/2 : FROM centos
 ---> 470671670cac
Step 2/2 : ENTRYPOINT [ "ls", "-a" ]
 ---> Running in 672fb009096a
Removing intermediate container 672fb009096a
 ---> 81aedb452f1c
Successfully built 81aedb452f1c
Successfully tagged entrypointtest:latest

执行

[root@xiaoyequ home]# docker run 81aedb452f1c
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

测试 -l 参数

[root@xiaoyequ home]# docker run entrypointtest -l
total 56
drwxr-xr-x  1 root root 4096 Jun 16 13:08 .
drwxr-xr-x  1 root root 4096 Jun 16 13:08 ..
-rwxr-xr-x  1 root root    0 Jun 16 13:08 .dockerenv
lrwxrwxrwx  1 root root    7 May 11  2019 bin -> usr/bin
drwxr-xr-x  5 root root  340 Jun 16 13:08 dev
drwxr-xr-x  1 root root 4096 Jun 16 13:08 etc
drwxr-xr-x  2 root root 4096 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 4096 Jan 13 21:48 lost+found
drwxr-xr-x  2 root root 4096 May 11  2019 media
drwxr-xr-x  2 root root 4096 May 11  2019 mnt
drwxr-xr-x  2 root root 4096 May 11  2019 opt
dr-xr-xr-x 97 root root    0 Jun 16 13:08 proc
dr-xr-x---  2 root root 4096 Jan 13 21:49 root
drwxr-xr-x 11 root root 4096 Jan 13 21:49 run
lrwxrwxrwx  1 root root    8 May 11  2019 sbin -> usr/sbin
drwxr-xr-x  2 root root 4096 May 11  2019 srv
dr-xr-xr-x 13 root root    0 Jun 14 13:03 sys
drwxrwxrwt  7 root root 4096 Jan 13 21:49 tmp
drwxr-xr-x 12 root root 4096 Jan 13 21:49 usr
drwxr-xr-x 20 root root 4096 Jan 13 21:49 var

发现可以直接使用,这里就是一种追加,我们可以明显的知道 CMD 和 ENTRYPOINT 的区别了

自定义镜像 tomcat

jdk1.8的命令下载方式:

wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.tar.gz

在 /home/tomcat 目录下新建一个Dockerfile文件

FROM centos
MAINTAINER xiaoyequ<[email protected]>
#把宿主机当前上下文的read.txt拷贝到容器/usr/local/路径下
COPY read.txt /usr/local/cincontainer.txt
#把java与tomcat添加到容器中
ADD jdk-8u131-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.36.tar.gz /usr/local/
#安装vim编辑器
RUN yum -y install vim
#设置工作访问时候的WORKDIR路径,登录落脚点
ENV MYPATH /usr/local
WORKDIR $MYPATH
#配置java与tomcat环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_131
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.36
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.36
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
#容器运行时监听的端口
EXPOSE 8080
#启动时运行tomcat
# ENTRYPOINT ["/usr/local/apache-tomcat-9.0.36/bin/startup.sh" ]
# CMD ["/usr/local/apache-tomcat-9.0.36/bin/catalina.sh","run"]
CMD /usr/local/apache-tomcat-9.0.36/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.36/bin/logs/catalina.out

DockerFile_第10张图片

构建镜像

docker build -t diytomcat .
[root@xiaoyequ tomcat]# docker build -t diytomcat .
    ......
Successfully built 28e918d5b143
Successfully tagged diytomcat:latest

# 查看确定构建完毕!
[root@xiaoyequ tomcat]# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
diytomcat             latest              28e918d5b143        48 seconds ago      686MB

运行启动 run

docker run -d -p 3344:8080 --name mydiytomcat -v /home/tomcat/test:/usr/local/apache-tomcat-9.0.36/webapps/test -v /home/tomcat/tomcat9logs/:/usr/local/apache-tomcat-9.0.36/logs --privileged=true diytomcat

 注意容器内的路径,文件名

备注:Docker挂载主机目录Docker访问出现cannot open directory .: Permission denied

解决办法:在挂载目录后多加一个--privileged=true参数即可

验证测试访问! curl localhost:3344

DockerFile_第11张图片

结合前面学习的容器卷将测试的web服务test发布

DockerFile_第12张图片

web.xml:



test

a.jsp: 

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




hello,xiaoyequ


-----------welcome------------
<%=" my docker tomcat,xiaoyequ"%>


<% System.out.println("-------my docker tomcat-------");%>

测试

DockerFile_第13张图片

查看日志

DockerFile_第14张图片

你可能感兴趣的:(Docker)