1.DockerFile是什么?
DockerFile是用來構建Docker鏡像的構建文件,一般分為四部分:基礎鏡像信息、維護者信息、鏡像操作指令和容器啟動時執行指令,’#’ 為 Dockerfile 中的注釋。
我們可以通過dockerfile來構建docker鏡像。
如Tomcat的dockerfile如下:
FROM adoptopenjdk:8-jdk-hotspot
ENV CATALINA_HOME /usr/local/tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
RUN mkdir -p "$CATALINA_HOME"
WORKDIR $CATALINA_HOME
ENV TOMCAT_NATIVE_LIBDIR $CATALINA_HOME/native-jni-lib
ENV LD_LIBRARY_PATH ${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$TOMCAT_NATIVE_LIBDIR
ENV GPG_KEYS 05AB33110949707C93A279E3D3EFE6B686867BA6
EXPOSE 8080
CMD ["catalina.sh", "run"]
2.DockerFile解析
1)基礎結構由大寫的保留字指令+參數構成。
2)指令從上到下順序執行。
3)#表示注解。
4)每條指令都會創建新的鏡像層,并對鏡像進行提交。
3.DockerFile保留字指令解析
1)FROM
基礎鏡像,當前新建的鏡像是基于哪個鏡像的。
2)MAINTAINER
鏡像維護者的姓名郵箱主頁啥的
3)RUN
容器構建的時候需要的命令
4)EXPOSE
當前容器對外暴露的端口
5)WORKDIR
指定在創建容器后,終端默認登錄的進來工作目錄,一個落腳點
6)ENV
鏡像構建過程中需要設置的環境變量
7)ADD
將宿主機目錄下的文件拷貝進鏡像且ADD命令會自動處理URL和解壓tar壓縮包
8)COPY
將宿主機目錄下的文件拷貝進鏡像,或者拷貝出來
9)VOLUME
容器數據卷,用于數據保存和持久化工作。可以將宿主機目錄和容器綁定,這樣兩個文件夾下的文件都能共通,如tomcat鏡像的項目部署文件夾,映射到宿主機后,部署項目不需要進入鏡像,直接部署到宿主機對應目錄就可以了。
10)CMD
制動一個容器啟動時要運行的命令,dockerfile中可以有多個cmd命令,但是只有最后一個會生效,之前的都會被覆蓋不會被執行。
11)ENTRYPOINT
指定一個容器啟動時要運行的命令,和cmd一樣,但是可以指定多個命令,在容器啟動時,會順序執行。
12)ONBUILD
當構建一個被繼承的dockerfile時運行命令,父鏡像在被子繼承后赴京向的onbuild被處罰
4.示例
1)基于centos構建一個能使用vim命令和ifconfig命令的,工作目錄是/usr/locald的自定義的centos。
a.自定義的dockerfile
FROM centos
MAINTAINER panda_zhu@qq.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 "sucess-----------ok"
CMD /bin/bash
b.執行以下命令,將dockerfile構建成docker鏡像
docker build -f ./mydockerfile -t mycentos:1.1 .
./mydockerfile是新寫的dockerfile
mycentos:1.1 是新鏡像的名稱和版本標簽
最后還有一個點別忘了
彈出的日志如下:
Sending build context to Docker daemon 2.048kB
Step 1/10 : FROM centos
---> 0d120b6ccaa8
Step 2/10 : MAINTAINER panda_zhu@qq.com
---> Running in e72b7682d2a1
Removing intermediate container e72b7682d2a1
---> 0c169e4db377
Step 3/10 : ENV MYPATH /usr/local
---> Running in 4bcac118de46
Removing intermediate container 4bcac118de46
---> 361b12647c93
Step 4/10 : WORKDIR $MYPATH
---> Running in 1f7a11a85882
Removing intermediate container 1f7a11a85882
---> c8ca5793760e
Step 5/10 : RUN yum -y install vim
---> Running in c10b463ce61a
CentOS-8 - AppStream 4.9 MB/s | 5.8 MB 00:01
CentOS-8 - Base 942 kB/s | 2.2 MB 00:02
CentOS-8 - Extras 11 kB/s | 8.6 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 450 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 2.4 MB/s | 48 kB 00:00
(3/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 8.4 MB/s | 1.4 MB 00:00
(4/5): which-2.21-12.el8.x86_64.rpm 496 kB/s | 49 kB 00:00
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 9.6 MB/s | 6.3 MB 00:00
--------------------------------------------------------------------------------
Total 3.5 MB/s | 7.8 MB 00:02
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 c10b463ce61a
---> e13ba4c98f4e
Step 6/10 : RUN yum -y install net-tools
---> Running in c44679e1b6f8
Last metadata expiration check: 0:00:10 ago on Sun Dec 6 09:09: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 1.8 MB/s | 323 kB 00:00
--------------------------------------------------------------------------------
Total 427 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 c44679e1b6f8
---> e4635d92ce93
Step 7/10 : EXPOSE 80
---> Running in 80175acc1bb0
Removing intermediate container 80175acc1bb0
---> 648de32c0418
Step 8/10 : CMD echo $MYPATH
---> Running in 4bef6d09313d
Removing intermediate container 4bef6d09313d
---> ecf14f315d3e
Step 9/10 : CMD echo "sucess-----------ok"
---> Running in 2d6a349f1799
Removing intermediate container 2d6a349f1799
---> e01edd932e43
Step 10/10 : CMD /bin/bash
---> Running in 9b09a2ac5278
Removing intermediate container 9b09a2ac5278
---> 7c4be87820a6
Successfully built 7c4be87820a6
Successfully tagged mycentos:1.1
可以看到,每一步都執行一條dockerfile中的命令。
c.使用docker images查詢鏡像,自定義的鏡像打好了。
d.啟動鏡像,可以看到工作目錄已經更改了
vim和ifconfig也可以使用了。