(五) Dockerfile 構建鏡像
Dockerfile 是一個文本檔案,記錄了鏡像構建的所有步驟,
(1)第一個dockerfile
用 Dockerfile 創建上節的 ubuntu-with-vi,其內容則為:
FROM ubuntu
RUN apt-get update && apt-get install -y vim
下面我們運行 docker build 命令構建鏡像并詳細分析每個細節,
root@cuiyongchao:/dockerfile# pwd ①
/dockerfile
root@cuiyongchao:/dockerfile# ls ②
Dockerfile
root@cuiyongchao:/dockerfile# docker build -t ubuntu-with-vim-dockerfile . ③
Sending build context to Docker daemon 2.048kB ④
Step 1/2 : FROM ubuntu ⑤
---> d70eaf7277ea
Step 2/2 : RUN apt-get update && apt-get install -y vim ⑥
---> Running in 0d4f882d4635 ⑦
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
,,,,,,
Setting up vim (2:8.1.2269-1ubuntu5) ...
--->b75528ee6f18 ⑧
Processing triggers for libc-bin (2.31-0ubuntu9.1) ...
Removing intermediate container 0d4f882d4635 ⑨
Successfully built b75528ee6f18 ⑩
Successfully tagged ubuntu-with-vim-dockerfile:latest
① 當前目錄為 /dockerfile,
② Dockerfile 準備就緒,
③ 運行 docker build 命令,-t 將新鏡像命名為 ubuntu-with-vim-dockerfile,命令末尾的 . 指明 build context 為當前目錄,Docker 默認會從 build context 中查找 Dockerfile 檔案,我們也可以通過 -f 引數指定 Dockerfile 的位置,
④ 從這步開始就是鏡像真正的構建程序, 首先 Docker 將 build context 中的所有檔案發送給 Docker daemon,build context 為鏡像構建提供所需要的檔案或目錄,Dockerfile 中的 ADD、COPY 等命令可以將 build context 中的檔案添加到鏡像,此例中,build context 為當前目錄 /dockerfile,該目錄下的所有檔案和子目錄都會被發送給 Docker daemon,所以,使用 build context 就得小心了,不要將多余檔案放到 build context,特別不要把 /、/usr` 作為 build context,否則構建程序會相當緩慢甚至失敗,
⑤ Step 1:執行 FROM,將 ubuntu 作為 base 鏡像,
ubuntu 鏡像 ID 為 d70eaf7277ea,
⑥ Step 2:執行 RUN,安裝 vim,具體步驟為 ⑦、⑧、⑨,
⑦ 啟動 ID 為 0d4f882d4635 的臨時容器,在容器中通過 apt-get 安裝 vim,
⑧ 安裝成功后,將容器保存為鏡像,其 ID 為 b75528ee6f18,
這一步底層使用的是類似 docker commit 的命令,
⑨ 洗掉臨時容器 0d4f882d4635,
⑩ 鏡像構建成功,
通過 docker images 查看鏡像資訊,
root@cuiyongchao:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-with-vim-dockerfile latest b75528ee6f18 2 minutes ago 167MB
ubuntu-with-vi latest 218e302a1e28 43 minutes ago 167MB
ubuntu latest d70eaf7277ea 3 days ago 72.9MB
? 鏡像 ID 為 b75528ee6f18,與構建時的輸出一致,在上面的構建程序中,我們要特別注意指令 RUN 的執行程序 ⑦、⑧、⑨,Docker 會在啟動的臨時容器中執行操作,并通過 commit 保存為新的鏡像,
(2)查看鏡像分層結構
? ubuntu-with-vim-dockerfile 是通過在 base 鏡像的頂部添加一個新的鏡像層而得到的,這個新鏡像層的內容由 RUN apt-get update && apt-get install -y vim 生成,這一點我們可以通過 docker history 命令驗證,
root@cuiyongchao:~# docker history ubuntu
IMAGE CREATED CREATED BY SIZE COMMENT
d70eaf7277ea 3 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 3 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B
<missing> 3 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B
<missing> 3 days ago /bin/sh -c #(nop) ADD file:435d9776fdd3a1834… 72.9MB
root@cuiyongchao:~# docker history ubuntu-with-vim-dockerfile:latest
IMAGE CREATED CREATED BY SIZE COMMENT
b75528ee6f18 10 minutes ago /bin/sh -c apt-get update && apt-get install… 94.1MB
d70eaf7277ea 3 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 3 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B
<missing> 3 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B
<missing> 3 days ago /bin/sh -c #(nop) ADD file:435d9776fdd3a1834… 72.9MB
root@cuiyongchao:~#
? docker history 會顯示鏡像的構建歷史,也就是 Dockerfile 的執行程序,ubuntu-with-vim-dockerfile 與 ubuntu 鏡像相比,確實只是多了頂部的一層 b75528ee6f18,由 apt-get 命令創建,大小為 94.01MB,docker history 也向我們展示了鏡像的分層結構,每一層由上至下排列,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/196053.html
標籤:其他
上一篇:容器技術(三)構建鏡像【7】
