Docker構(gòu)建文件Dockerfile簡單入門
1.Dockerfile介紹
在之前Docker的使用中,我們直接從倉庫下載需要的鏡像到本地,然后稍加配置就可以應(yīng)用了,通常從倉庫下載下來的鏡像都是通用的,無任何私有化的東西,我們拿過來就需要加很多的配置,每次使用就很麻煩。如果我們想定制化某一個(gè)鏡像可以嗎?比如Nginx,我不想每次使用都加很多的配置,而是定制化后,我再按照定制化的規(guī)則去使用,及時(shí)的安裝部署我需要的Nginx環(huán)境。DockerFile就是做這個(gè)工作的,一個(gè)用基礎(chǔ)鏡像來構(gòu)建新鏡像的文本文件,里面包含構(gòu)建鏡像需要的各種指令。
1.1 編寫Dockerfile
使用Dockerfile構(gòu)建Nginx鏡像,在一個(gè)空目錄下創(chuàng)建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
Dockerfile內(nèi)容如下:
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 構(gòu)建鏡像
[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 '構(gòu)建本地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 運(yùn)行鏡像
docker run -d -p 80:80 zhangbao/nginx001:1.0
首頁可以查看 welcome,this is a nginx image of build local.
2. Dockerfile構(gòu)建過程
構(gòu)建一個(gè)新鏡像,我們可以大致分為三個(gè)步驟:編寫Dockerfile文件 > docker build > docker run
2.1 基礎(chǔ)知識(shí)
Dockerfile的基礎(chǔ)知識(shí)
每個(gè)關(guān)鍵字(指令)都必須大寫
執(zhí)行過程從上到下,順序執(zhí)行
#為注釋
每個(gè)指令代表一層,都會(huì)創(chuàng)建提交一個(gè)新的鏡像層
Dockerfile指令
FROM 指定基礎(chǔ)鏡像 MAINTAINER 指定作者 RUN 執(zhí)行參數(shù)中定義的命令,構(gòu)建鏡像時(shí)需要的命令 EXPOSE 向容器外部公開的端口號(hào) WORKDIR 設(shè)置容器內(nèi)默認(rèn)工作目錄 USER 指定用戶 ENTROYPOINT 指定一個(gè)容器啟動(dòng)時(shí)運(yùn)行的命令 ENV 設(shè)置環(huán)境變量 ADD|COPY 復(fù)制文件到鏡像中 VOLUME 容器數(shù)據(jù)卷,向鏡像創(chuàng)建的容器添加卷 CMD 容器啟動(dòng)時(shí)要運(yùn)行的命令,可以有多個(gè),但只有最后一個(gè)生效
2.2 構(gòu)建過程
Dockerfile的構(gòu)建過程就是build的執(zhí)行過程
1.把當(dāng)前目錄和子目錄當(dāng)做上下文傳遞給docker服務(wù),命令最后的點(diǎn)表示當(dāng)前上下文。 2.從當(dāng)前目錄(不包括子目錄)找到Dockerfile文件,如果不指定文件,必須是此文件名。 3.檢查docker語法。 4.從基礎(chǔ)鏡像運(yùn)行一個(gè)容器。 5.執(zhí)行指令,修改容器,如上面操作添加數(shù)據(jù)卷,修改首頁。 6.對修改后的容器提交一個(gè)新的鏡像層,也可叫做中間層鏡像。 7.針對中間層生成的鏡像,運(yùn)行新的容器。 8.重復(fù)執(zhí)行修改容器、提交鏡像、運(yùn)行容器指令,直到所有指令執(zhí)行完成。
注:每條指令都會(huì)生成新的鏡像,也就是生成中間層鏡像,這樣執(zhí)行速度其實(shí)會(huì)很慢,可以不使用緩存。在構(gòu)建命令中加上
--no-cache=true
即可??筛鶕?jù)實(shí)際場景選擇是否需要緩存。
3. 構(gòu)建一個(gè)自己的centos
我們已經(jīng)熟悉了構(gòu)建Dockerfile的基礎(chǔ)指令,現(xiàn)在可以做一些基本實(shí)踐了。
3.1 運(yùn)行一個(gè)官方centos
首先我們運(yùn)行一個(gè)原生的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 /]#
可以看到,我們已經(jīng)運(yùn)行一個(gè)centos,然后發(fā)現(xiàn)一些基礎(chǔ)的命令是沒有的,這就說明這個(gè)contos是一個(gè)壓縮版的,如果我們想運(yùn)行一個(gè)有這些命令的鏡像呢?這就需要在Dockerfile中做一些操作了?
3.2 編寫Dockerfile文件
記住在一個(gè)空白的目錄下創(chuàng)建Dockerfile文件,我這里文件名dockerfile-mycentos,內(nèi)容如下:
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 構(gòu)建新鏡像
如果是自定義Dockerfile文件名,則構(gòu)建時(shí)必須指定文件名,否則文件名必須是: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 問題及修復(fù)
這時(shí)候發(fā)現(xiàn)失敗了,查問題是centos8的官方源已經(jīng)下線了,導(dǎo)致yum下載失敗。那剛好,我們在自定義鏡像的時(shí)候可以修復(fù)這些問題。只需修改yum的默認(rèn)源就可以了,這個(gè)操作也放在Dockerfile文件內(nèi)。我們修復(fù)后Dockerfile文件如下:
FROM centos #指定作者 MAINTAINER zhangbao<zhangbaohpu@163.com> #ENV kv格式,這里指定工作目錄,及默認(rèn)進(jìn)入容器的目錄 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 重新構(gòu)建鏡像
重新構(gòu)建下修改后的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步,即每個(gè)指令一步,也表示每一步算一層。整個(gè)構(gòu)建過程還是很詳細(xì)的。
3.6 運(yùn)行自定義鏡像
自定義鏡像已經(jīng)構(gòu)建完成,下面我們開始運(yùn)行鏡像,并檢測下基礎(chǔ)命令
[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
命令已經(jīng)可以使用,ll
命令還需要下載對應(yīng)的工具包,這里就不在下載了。
更多關(guān)于Docker構(gòu)建文件Dockerfile的說明請查看下面的相關(guān)鏈接
相關(guān)文章
docker中通過nginx+confd動(dòng)態(tài)生成配置的解決方案
這篇文章主要介紹了docker:nginx+confd動(dòng)態(tài)生成配置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03Docker安裝ELK并實(shí)現(xiàn)JSON格式日志分析的方法
這篇文章主要介紹了Docker安裝ELK并實(shí)現(xiàn)JSON格式日志分析的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10docker基礎(chǔ)知識(shí)之掛載本地目錄的方法
本篇文章主要介紹了docker基礎(chǔ)知識(shí)之掛載本地目錄的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04docker+gitlab+jenkins從零搭建自動(dòng)化部署
通過幾天的學(xué)習(xí)和自己的理解,整理了下Docker+Jenkins的自動(dòng)部署教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06將spring boot應(yīng)用打入docker中運(yùn)行的實(shí)現(xiàn)方法
這篇文章主要介紹了將spring boot應(yīng)用打入docker中運(yùn)行的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07