DockerFile理解與應用
阿新 • • 發佈:2020-12-06
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_HOMEENV 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 [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 "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 [email protected] ---> 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) <[email protected]>" 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也可以使用了。