在之前Docker的使用中,我們直接從倉庫下載需要的映象到本地,然後稍加配置就可以應用了,通常從倉庫下載下來的映象都是通用的,無任何私有化的東西,我們拿過來就需要加很多的配置,每次使用就很麻煩。如果我們想定製化某一個映象可以嗎?比如Nginx,我不想每次使用都加很多的配置,而是定製化後,我再按照定製化的規則去使用,及時的安裝部署我需要的Nginx環境。DockerFile就是做這個工作的,一個用基礎映象來構建新映象的文字檔案,裡面包含構建映象需要的各種指令。
1.1 編寫Dockerfile
使用Dockerfile構建Nginx映象,在一個空目錄下建立Dockerfile檔案
[root@hadoop101 nginx]# pwd
/home/docker_test/docker_files/nginx
[root@hadoop101 nginx]# vim Dockerfile
[root@hadoop101 nginx]# ll
total 4
-rw-r--r-- 1 root root 153 Mar 29 23:33 Dockerfile
FROM nginx
VOLUME ["volume01","volume02"]
RUN echo 'welcome,this is a nginx image of build local.' > /usr/share/nginx/html/index.html
RUN echo '----end----'
RUN /bin/bash
1.2 構建映象
[root@hadoop101 nginx]# docker build -t zhangbao/nginx001:1.0 .
[root@hadoop101 nginx]# docker build -t zhangbao/nginx001:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/5 : FROM nginx
---> 605c77e624dd
Step 2/5 : VOLUME ["volume01","volume02"]
---> Running in d9ff85f068bf
Removing intermediate container d9ff85f068bf
---> 801484ddbb09
Step 3/5 : RUN echo '構建本地nginx映象' > /usr/share/nginx/html/index.html
---> Running in 6614ebace633
Removing intermediate container 6614ebace633
---> 29a385ed17b6
Step 4/5 : RUN echo '----end----'
---> Running in 3449cc6cf814
----end----
Removing intermediate container 3449cc6cf814
---> 5d6941c830a4
Step 5/5 : RUN /bin/bash
---> Running in 1f106444b093
Removing intermediate container 1f106444b093
---> 440fd4b943c7
Successfully built 440fd4b943c7
Successfully tagged zhangbao/nginx001:1.0
[root@hadoop101 nginx]#
1.3 執行映象
docker run -d -p 80:80 zhangbao/nginx001:1.0
首頁可以檢視 welcome,this is a nginx image of build local.
2. Dockerfile構建過程
構建一個新映象,我們可以大致分為三個步驟:編寫Dockerfile檔案 > docker build > docker run
2.1 基礎知識
Dockerfile的基礎知識
-
每個關鍵字(指令)都必須大寫
-
執行過程從上到下,順序執行
-
#為註釋
-
每個指令代表一層,都會建立提交一個新的映象層
Dockerfile指令
FROM 指定基礎映象
MAINTAINER 指定作者
RUN 執行引數中定義的命令,構建映象時需要的命令
EXPOSE 向容器外部公開的埠號
WORKDIR 設定容器內預設工作目錄
USER 指定使用者
ENTROYPOINT 指定一個容器啟動時執行的命令
ENV 設定環境變數
ADD|COPY 複製檔案到映象中
VOLUME 容器資料卷,向映象建立的容器新增捲
CMD 容器啟動時要執行的命令,可以有多個,但只有最後一個生效
2.2 構建過程
Dockerfile的構建過程就是build的執行過程
1.把當前目錄和子目錄當做上下文傳遞給docker服務,命令最後的點表示當前上下文。
2.從當前目錄(不包括子目錄)找到Dockerfile檔案,如果不指定檔案,必須是此檔名。
3.檢查docker語法。
4.從基礎映象執行一個容器。
5.執行指令,修改容器,如上面操作新增資料卷,修改首頁。
6.對修改後的容器提交一個新的映象層,也可叫做中間層映象。
7.針對中間層生成的映象,執行新的容器。
8.重複執行修改容器、提交映象、執行容器指令,直到所有指令執行完成。
注:每條指令都會生成新的映象,也就是生成中間層映象,這樣執行速度其實會很慢,可以不使用快取。在構建命令中加上
--no-cache=true
即可。可根據實際場景選擇是否需要快取。
3. 構建一個自己的centos
我們已經熟悉了構建Dockerfile的基礎指令,現在可以做一些基本實踐了。
3.1 執行一個官方centos
首先我們執行一個原生的centos,然後做一些linux的基本操作
[root@hadoop101 ~]# docker run -it centos /bin/bash
[root@a0db59573b95 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@a0db59573b95 /]# ll
bash: ll: command not found
[root@a0db59573b95 /]# vim test
bash: vim: command not found
[root@a0db59573b95 /]# ifconfig
bash: ifconfig: command not found
[root@a0db59573b95 /]#
可以看到,我們已經執行一個centos,然後發現一些基礎的命令是沒有的,這就說明這個contos是一個壓縮版的,如果我們想執行一個有這些命令的映象呢?這就需要在Dockerfile中做一些操作了?
3.2 編寫Dockerfile檔案
記住在一個空白的目錄下建立Dockerfile檔案,我這裡檔名dockerfile-mycentos,內容如下:
FROM centos
#指定作者
MAINTAINER zhangbao<zhangbaohpu@163.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
#下載需要的工具類
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo "--end--"
CMD /bin/bash
3.3 構建新映象
如果是自定義Dockerfile檔名,則構建時必須指定檔名,否則檔名必須是:Dockerfile
[root@hadoop101 mycentos]# docker build -f dockerfile-mycentos -t mysentos:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/9 : FROM centos
---> 5d0da3dc9764
Step 2/9 : MAINTAINER zhangbao<zhangbaohpu@163.com>
---> Running in d9b4afe40ef6
Removing intermediate container d9b4afe40ef6
---> 87e522a4dcb6
Step 3/9 : ENV MYPATH /usr/local
---> Running in 2fe975209f1e
Removing intermediate container 2fe975209f1e
---> 30491c2e1634
Step 4/9 : WORKDIR $MYPATH
---> Running in cd9b1810f244
Removing intermediate container cd9b1810f244
---> d2addbefe4f6
Step 5/9 : RUN yum -y install vim
---> Running in d6a8caa012e9
CentOS Linux 8 - AppStream 64 B/s | 38 B 00:00
Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
The command '/bin/sh -c yum -y install vim' returned a non-zero code: 1
[root@hadoop101 mycentos]#
3.4 問題及修復
這時候發現失敗了,查問題是centos8的官方源已經下線了,導致yum下載失敗。那剛好,我們在自定義映象的時候可以修復這些問題。只需修改yum的預設源就可以了,這個操作也放在Dockerfile檔案內。我們修復後Dockerfile檔案如下:
FROM centos
#指定作者
MAINTAINER zhangbao<zhangbaohpu@163.com>
#ENV kv格式,這裡指定工作目錄,及預設進入容器的目錄
ENV MYPATH /usr/local
WORKDIR $MYPATH
#將原有的yum配置備份下
RUN tar cvf /etc/yum.repos.d.tar /etc/yum.repos.d
#修改yum源配置,
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
#刪除掉多於的repo檔案
RUN rm -rf /etc/yum.repos.d/CentOS-Linux-*
#使yum配置更新
RUN yum clean all
RUN yum makecache
#下載需要的工具類
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo "--end--"
CMD /bin/bash
3.5 重新構建映象
重新構建下修改後的Dockerfile檔案
docker build -f dockerfile-mycentos -t mysentos:1.0 .
[root@hadoop101 mycentos]# docker build -f dockerfile-mycentos -t mysentos:1.0 .
Sending build context to Docker daemon 2.56kB
Step 1/14 : FROM centos
---> 5d0da3dc9764
Step 2/14 : MAINTAINER zhangbao<zhangbaohpu@163.com>
---> Running in 79ff0ddc554d
Removing intermediate container 79ff0ddc554d
---> 0cc1bf4a4ba0
Step 3/14 : ENV MYPATH /usr/local
---> Running in 59d3d432c58c
Removing intermediate container 59d3d432c58c
---> 839ed1e4b944
Step 4/14 : WORKDIR $MYPATH
---> Running in d085e4887d92
Removing intermediate container d085e4887d92
---> 41c10774900b
Step 5/14 : RUN tar cvf /etc/yum.repos.d.tar /etc/yum.repos.d
---> Running in 1ab58ab850c0
/etc/yum.repos.d/
/etc/yum.repos.d/CentOS-Linux-AppStream.repo
/etc/yum.repos.d/CentOS-Linux-BaseOS.repo
/etc/yum.repos.d/CentOS-Linux-ContinuousRelease.repo
/etc/yum.repos.d/CentOS-Linux-Debuginfo.repo
/etc/yum.repos.d/CentOS-Linux-Devel.repo
/etc/yum.repos.d/CentOS-Linux-Extras.repo
/etc/yum.repos.d/CentOS-Linux-FastTrack.repo
/etc/yum.repos.d/CentOS-Linux-HighAvailability.repo
/etc/yum.repos.d/CentOS-Linux-Media.repo
/etc/yum.repos.d/CentOS-Linux-Plus.repo
/etc/yum.repos.d/CentOS-Linux-PowerTools.repo
/etc/yum.repos.d/CentOS-Linux-Sources.repo
tar: Removing leading `/' from member names
Removing intermediate container 1ab58ab850c0
---> bfd1c4cd694c
Step 6/14 : RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
---> Running in 73ebef623f8a
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2495 100 2495 0 0 12351 0 --:--:-- --:--:-- --:--:-- 12351
Removing intermediate container 73ebef623f8a
---> 22812c68f967
Step 7/14 : RUN rm -rf /etc/yum.repos.d/CentOS-Linux-*
---> Running in eb8426f499b0
Removing intermediate container eb8426f499b0
---> 8dc6bd0b4e84
Step 8/14 : RUN yum clean all
---> Running in a508fb503158
0 files removed
Removing intermediate container a508fb503158
---> c6831cfda20f
Step 9/14 : RUN yum makecache
---> Running in 67c3f0dc0a6a
CentOS-8.5.2111 - Base - mirrors.aliyun.com 16 MB/s | 4.6 MB 00:00
CentOS-8.5.2111 - Extras - mirrors.aliyun.com 45 kB/s | 10 kB 00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.co 16 MB/s | 8.4 MB 00:00
Metadata cache created.
Removing intermediate container 67c3f0dc0a6a
---> 6ca3b2fbeb13
Step 10/14 : RUN yum -y install vim
---> Running in e81b9f1a4705
Last metadata expiration check: 0:00:05 ago on Tue Apr 5 09:35:43 2022.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-16.el8 AppStream 1.4 M
Installing dependencies:
gpm-libs x86_64 1.20.7-17.el8 AppStream 39 k
vim-common x86_64 2:8.0.1763-16.el8 AppStream 6.3 M
vim-filesystem noarch 2:8.0.1763-16.el8 AppStream 49 k
which x86_64 2.21-16.el8 base 49 k
Transaction Summary
================================================================================
Install 5 Packages
Total download size: 7.8 M
Installed size: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm 235 kB/s | 39 kB 00:00
(2/5): which-2.21-16.el8.x86_64.rpm 203 kB/s | 49 kB 00:00
(3/5): vim-filesystem-8.0.1763-16.el8.noarch.rp 776 kB/s | 49 kB 00:00
(4/5): vim-common-8.0.1763-16.el8.x86_64.rpm 16 MB/s | 6.3 MB 00:00
(5/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm 5.4 MB/s | 1.4 MB 00:00
--------------------------------------------------------------------------------
Total 19 MB/s | 7.8 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : vim-filesystem-2:8.0.1763-16.el8.noarch 1/5
Installing : vim-common-2:8.0.1763-16.el8.x86_64 2/5
Installing : gpm-libs-1.20.7-17.el8.x86_64 3/5
Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64 3/5
Installing : which-2.21-16.el8.x86_64 4/5
Installing : vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_64 5/5
Verifying : which-2.21-16.el8.x86_64 1/5
Verifying : gpm-libs-1.20.7-17.el8.x86_64 2/5
Verifying : vim-common-2:8.0.1763-16.el8.x86_64 3/5
Verifying : vim-enhanced-2:8.0.1763-16.el8.x86_64 4/5
Verifying : vim-filesystem-2:8.0.1763-16.el8.noarch 5/5
Installed:
gpm-libs-1.20.7-17.el8.x86_64 vim-common-2:8.0.1763-16.el8.x86_64
vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch
which-2.21-16.el8.x86_64
Complete!
Removing intermediate container e81b9f1a4705
---> 3d731b0904e6
Step 11/14 : RUN yum -y install net-tools
---> Running in f45f6c7c9c8c
Last metadata expiration check: 0:00:17 ago on Tue Apr 5 09:35:43 2022.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.52.20160912git.el8 base 322 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 934 kB/s | 322 kB 00:00
--------------------------------------------------------------------------------
Total 930 kB/s | 322 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.52.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.52.20160912git.el8.x86_64
Complete!
Removing intermediate container f45f6c7c9c8c
---> adc0f183e07e
Step 12/14 : EXPOSE 80
---> Running in 9422041e18c2
Removing intermediate container 9422041e18c2
---> e94ae254891f
Step 13/14 : CMD echo "--end--"
---> Running in 9183b6a1444d
Removing intermediate container 9183b6a1444d
---> c3978bb3cb17
Step 14/14 : CMD /bin/bash
---> Running in 536c8de964d3
Removing intermediate container 536c8de964d3
---> 68f0ddc7c99f
Successfully built 68f0ddc7c99f
Successfully tagged mysentos:1.0
[root@hadoop101 mycentos]#
總共分為14步,即每個指令一步,也表示每一步算一層。整個構建過程還是很詳細的。
3.6 執行自定義映象
自定義映象已經構建完成,下面我們開始執行映象,並檢測下基礎命令
[root@hadoop101 mycentos]# docker run -it mysentos:1.0
[root@2f70cc22b493 local]# pwd
/usr/local
[root@2f70cc22b493 local]# ll
bash: ll: command not found
[root@2f70cc22b493 local]# vim test
[root@2f70cc22b493 local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 8 bytes 656 (656.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@2f70cc22b493 local]#
可以看到 vim
和 ifconfig
命令已經可以使用,ll
命令還需要下載對應的工具包,這裡就不在下載了。