因為想學學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的,如果訪問不到可以容果日志去排查錯誤。