Dockerfile构建镜像实战
因为想学学kubernetes,所以回头来实战一下使用Dockerfile构建镜像
Dockerfile是构建容器镜像的描述文件,从上到下依次执行。
一、常见Dockerfile指令
FROM # 基于哪个基础镜像构建
MAINTAINER # 镜像作者的信息
RUN # 容器镜像构建的时候需要运行的命令
ADD # 在这个镜像中放置的软件压缩包,会自动解压
WORKDIR # 镜像的工作目录
VOLUME # 给镜像指定挂在卷
EXPOSE # 对外暴露的端口
CMD # 容器启动后执行的命令,可以被替代,只有最后一个会生效, (通常用于启动我们的应用)
ENTRYPOINT # 容器启动后执行的命令,可以追加
ONBUILD
COPY # 和add类似,将压缩包拷贝进镜像中
ENV # 构建时,设置环境变量,如设置mysql的用户名密码
二、编写Centos Dockerfile
2.1、编写Dockerfile
FROM centos
MAINTAINER changwu
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
EXPOSE 80
CMD /bin/bash
2.2、构建
# 构建镜像 docker build -f dockerfile1 -t 自定义镜像名:[tag] .
自定义的镜像名不能出现大写字母
-t后的自定义镜像名如果没有添加TAG标签的话,默认使用latest
最后的点不能省略
[root@VM-0-15-centos ~]# docker build -f dockerfile1-centos -t mycentos:0.1 .
Sending build context to Docker daemon 19.97kB
Step 1/7 : FROM centos
latest: Pulling from library/centos
3c72a8ed6814: Pull complete
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
---> 0d120b6ccaa8
Step 2/7 : MAINTAINER changwu
---> Running in cf9ecee84b06
Removing intermediate container cf9ecee84b06
---> 72f5724759c5
Step 3/7 : ENV MYPATH /usr/local
---> Running in fc50b2e8d6a3
Removing intermediate container fc50b2e8d6a3
---> fe0efcf04175
Step 4/7 : WORKDIR $MYPATH
---> Running in 9249bf02b95e
Removing intermediate container 9249bf02b95e
---> e76653512fe4
Step 5/7 : RUN yum -y install vim
---> Running in bc296c183fd2
CentOS-8 - AppStream 4.6 MB/s | 5.8 MB 00:01
CentOS-8 - Base 826 kB/s | 2.2 MB 00:02
CentOS-8 - Extras 4.2 kB/s | 7.9 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 251 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 1.5 MB/s | 48 kB 00:00
(3/5): which-2.21-12.el8.x86_64.rpm 305 kB/s | 49 kB 00:00
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 1.5 MB/s | 1.4 MB 00:00
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 4.0 MB/s | 6.3 MB 00:01
--------------------------------------------------------------------------------
Total 3.0 MB/s | 7.8 MB 00:02
CentOS-8 - AppStream 1.6 MB/s | 1.6 kB 00:00
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
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 bc296c183fd2
---> fd7d572c56f9
Step 6/7 : EXPOSE 80
---> Running in 9a732d4f6795
Removing intermediate container 9a732d4f6795
---> b08191ccdcba
Step 7/7 : CMD /bin/bash
---> Running in 4c89c2dfacf0
Removing intermediate container 4c89c2dfacf0
---> 23a4dbeeadaf
Successfully built 23a4dbeeadaf
Successfully tagged mycentos:0.1
2.3、查看Docker镜像
Dockerfile中指定From centos,如果本地没有这个镜像的话,他会先去远程仓库拉取镜像。再基于这个镜像构建我们自己的镜像。
2.4、运行镜像
上面配置的workdir工作目录,就是一进入容器我们切换进去的目录
三、CMD和ENTRYPOINT
3.1、CMD的作用
CMD指定容器启动后执行的命令,如果我们再编写上面的dockerfile时,没有使用CMD执行/bin/bash,然后build,构建的过程不会出错,但是当我们直接启动容器时会碰到下面的问题
没有和容器有任何交互,容器启动后会直接退出
所以我们得在启动参数中添加/bin/bash
3.2、CMD和ENTRYPOINT的区别
3.2.1、CMD
在一个Dockerfile中的存在CMD会发生替换,比如下面的Dockerfile
Dockerfile如下:
[root@VM-0-15-centos ~]# cat dockerfile2-centos
FROM centos
MAINTAINER changwu:0.6
CMD ["ls","-a"]
启动容器:
Dockerfile如下:
[root@VM-0-15-centos ~]# cat dockerfile2-centos
FROM centos
MAINTAINER changwu:0.6
CMD ["ls","-a"]
CMD ["ls","-la"]
直接启动容器:发现,只有最后一个CMD执行了。
而且手动指定容器启动后执行的命令:可以发现指定的 ls -a 又把它前面的命令替换掉了
3.2.2、ENTRYPONIT
编写如下的dockerfile,指定启动后的命令是 ls -a
直接启动
携带启动参数启动:可以看到添加的参数和原来的参数同时生效
四、制作Tomcat镜像
准备两份压缩包,jdk、tomcat
编写Dockerfile文件
如果文件名为Dockerfile,那在build 镜像时都可以不使用 -f 执行Dockerfile文件,默认就会找这个Dockerfile文件。
FROM mycentos
MAINTAINER changwu:0.0.1
COPY readme.md /usr/local/readme.md
ADD jdk-8u181-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.38.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
# 先手动解压一下上面的jdk-8u181-linux-x64.tar.gz
# 名字就写它解压后的目录名,因为它解压后的目录名字可不一定是jdk-8u181-linux-x64
# 这里的JAVA_HOME写不对,tomcat中的catalina一定启动不了
ENV JAVA_HOME /usr/local/jdk1.8.0_181
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
# CMD中可以通过&&添加多个命令
# 比如我可以tail -f打印tomcat的启动日志(当然你想看它的启动日志就别加 -d 这个后台启动的参数)
# 启动tomcat不加这个打印日志,容器会直接退出的
CMD /usr/local/apache-tomcat-9.0.38/bin/startup.sh && tail -f /usr/local/apache-tomcat-9.0.38/logs/catalina.out
构建镜像
[root@VM-0-15-centos ~]# docker build -t my-tomcat:0.0.1 .
Sending build context to Docker daemon 191.5MB
Step 1/15 : FROM centos
---> 0d120b6ccaa8
Step 2/15 : MAINTAINER changwu:0.0.1
---> Running in c3450003f9fc
Removing intermediate container c3450003f9fc
---> 25e1b48f3ff1
Step 3/15 : COPY readme.md /usr/local/readme.md
---> 2810ef454d5b
Step 4/15 : ADD jdk-8u181-linux-x64.tar.gz /usr/local
---> 32cf8aff411b
Step 5/15 : ADD apache-tomcat-8.5.58-src.tar.gz /usr/local
---> c36c10a1bc8c
Step 6/15 : RUN yum -y install vim
---> Running in 8949f3d8e659
CentOS-8 - AppStream 1.0 MB/s | 5.8 MB 00:05
CentOS-8 - Base 1.1 MB/s | 2.2 MB 00:01
CentOS-8 - Extras 4.4 kB/s | 7.9 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 371 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 508 kB/s | 48 kB 00:00
(3/5): which-2.21-12.el8.x86_64.rpm 5.3 MB/s | 49 kB 00:00
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 825 kB/s | 1.4 MB 00:01
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 2.8 MB/s | 6.3 MB 00:02
--------------------------------------------------------------------------------
Total 2.4 MB/s | 7.8 MB 00:03
CentOS-8 - AppStream 1.6 MB/s | 1.6 kB 00:00
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
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 8949f3d8e659
---> 7cd3b5b3d194
Step 7/15 : ENV MYPATH /usr/local
---> Running in 71997b4e7eed
Removing intermediate container 71997b4e7eed
---> 2247f3b03469
Step 8/15 : WORKDIR $MYPATH
---> Running in bab05a01d8a6
Removing intermediate container bab05a01d8a6
---> 43c93cf458c8
Step 9/15 : ENV JAVA_HOME /usr/local/jdk-8u181
---> Running in 9b441de5f74d
Removing intermediate container 9b441de5f74d
---> 8665de1607de
Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/rt.jar:$JAVA_HOME/lib/tools.jar:
---> Running in 9ef5e5b77b38
Removing intermediate container 9ef5e5b77b38
---> 5a3b9626c8c7
Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.58
---> Running in b933e49bbcc9
Removing intermediate container b933e49bbcc9
---> 45c951327a53
Step 12/15 : ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.58
---> Running in eb1442c32bdc
Removing intermediate container eb1442c32bdc
---> 3653a2916add
Step 13/15 : ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
---> Running in d0b53ca7c763
Removing intermediate container d0b53ca7c763
---> f44bb4749b4b
Step 14/15 : EXPOSE 8080
---> Running in 91ed915a9882
Removing intermediate container 91ed915a9882
---> 6d0f3874bf63
Step 15/15 : CMD /usr/local/apache-tomcat-8.5.58/bin/startup.sh && tail -f /usr/local/apache-tomcat-8.5.58/bin/logs/catalina.out
---> Running in a5baeca60b88
Removing intermediate container a5baeca60b88
---> e1aa0ad30a8f
Successfully built e1aa0ad30a8f
Successfully tagged my-tomcat:0.0.1
启动:并将tomcat的webapp目录、日志目录挂载到宿主机上
docker run --name my-tomcat -d -p 9999:8080 -v /home/mytomcat/test:/usr/local/apache-tomcat-9.0.38/webapps/test -v /home/mytomcat/tomcatlog:/usr/local/apache-tomcat-9.0.38/logs 2b3ce00d3bed
将服务器的安全组中相应的端口打开,通过浏览器通过ip+port是可以访问到tomcat的,如果访问不到可以容果日志去排查错误。