FROM nginx:1.21.0-alpine ADD index.html /usr/share/nginx/html/index.html
1 2 3 4 5
docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE bitnami/nginx 1.18.0 dfe237636dde 28 minutes ago 89.3MB nginx 1.21.0-alpine a6eb2a334a9f 2 days ago 22.6MB nginx 1.21.0 d1a364dc548d 2 days ago 133MB
FROM ubuntu:21.04 RUN apt-get update RUN apt-get install -y wget RUN wget https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.1/ipinfo_2.0.1_linux_amd64.tar.gz RUN tar zxf ipinfo_2.0.1_linux_amd64.tar.gz RUNmv ipinfo_2.0.1_linux_amd64 /usr/bin/ipinfo RUNrm -rf ipinfo_2.0.1_linux_amd64.tar.gz
#建立鏡像檔 -f 參數可以指定dockerfile檔案 docker image build -f Dockerfile.bad -t ipinfo-bad . $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ipinfo latest 97bb429363fb 4 minutes ago 138MB ubuntu 21.04 478aa0080b60 4 days ago 74.1MB $ docker image history 97b IMAGE CREATED CREATED BY SIZE COMMENT 97bb429363fb 4 minutes ago RUN /bin/sh -c rm -rf ipinfo_2.0.1_linux_amd… 0B buildkit.dockerfile.v0 <missing> 4 minutes ago RUN /bin/sh -c mv ipinfo_2.0.1_linux_amd64 /… 9.36MB buildkit.dockerfile.v0 <missing> 4 minutes ago RUN /bin/sh -c tar zxf ipinfo_2.0.1_linux_am… 9.36MB buildkit.dockerfile.v0 <missing> 4 minutes ago RUN /bin/sh -c wget https://github.com/ipinf… 4.85MB buildkit.dockerfile.v0 <missing> 4 minutes ago RUN /bin/sh -c apt-get install -y wget # bui… 7.58MB buildkit.dockerfile.v0 <missing> 4 minutes ago RUN /bin/sh -c apt-get update # buildkit 33MB buildkit.dockerfile.v0 <missing> 4 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 4 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B <missing> 4 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B <missing> 4 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B <missing> 4 days ago /bin/sh -c #(nop) ADD file:d6b6ba642344138dc… 74.1MB
2.2. Dockerfile.good
1 2 3 4 5 6 7
FROM ubuntu:21.04 RUN apt-get update && \ apt-get install -y wget && \ wget https://github.com/ipinfo/cli/releases/download/ipinfo-2.0.1/ipinfo_2.0.1_linux_amd64.tar.gz && \ tar zxf ipinfo_2.0.1_linux_amd64.tar.gz && \ mv ipinfo_2.0.1_linux_amd64 /usr/bin/ipinfo && \ rm -rf ipinfo_2.0.1_linux_amd64.tar.gz
我們可以看到下面的結果,只有一筆RUN的分層
1 2 3 4 5 6 7 8 9 10 11 12 13
$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ipinfo-new latest fe551bc26b92 5 seconds ago 124MB ipinfo latest 97bb429363fb 16 minutes ago 138MB ubuntu 21.04 478aa0080b60 4 days ago 74.1MB $ docker image history fe5 IMAGE CREATED CREATED BY SIZE COMMENT fe551bc26b92 16 seconds ago RUN /bin/sh -c apt-get update && apt-get… 49.9MB buildkit.dockerfile.v0 <missing> 4 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 4 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B <missing> 4 days ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B <missing> 4 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 811B <missing> 4 days ago /bin/sh -c #(nop) ADD file:d6b6ba642344138dc… 74.1MB
3. 文件複製和目錄操作(ADD,COPY,WORKDIR)
往鏡像裡複製文件有兩種方式COPY、ADD。
3.1. 複製普通文件
COPY和ADD都可以把local的一個文件複製到鏡像裡,如果目標目錄不存在,則會自動建立
1 2
FROM python:3.9.5-alpine3.13 COPY hello.py /app/hello.py
比如把本地的hello.py複製到/app 目錄下,但app目錄不存在,所以會自動建立
3.2. 複製壓縮文件
ADD比較不一樣的地方是,如果複製的是一個gzip等壓縮文件時,ADD會幫助我們自動去解壓縮文件
1 2
FROM python:3.9.5-alpine3.13 ADD hello.tar.gz /app/
3.3. 如何選擇
所有文件複製可以均使用COPY指令,僅在需要自動解壓縮的場合使用ADD
3.4. 實驗記錄 COPY
1 2 3 4 5 6 7 8
# 建立tmp資料夾 root@kite-virtual-machine:/home/kite/Desktop/genDockerImagesFolder# mkdir tmp root@kite-virtual-machine:/home/kite/Desktop/genDockerImagesFolder# cd tmp # 建a文件 root@kite-virtual-machine:/home/kite/Desktop/genDockerImagesFolder/tmp# touch a # 建b文件 root@kite-virtual-machine:/home/kite/Desktop/genDockerImagesFolder/tmp# touch b root@kite-virtual-machine:/home/kite/Desktop/genDockerImagesFolder/tmp# cd ..
root@kite-virtual-machine:/home/kite/Desktop/genDockerImagesFolder# docker run -it --rm ipinfo ipinfo 8.8.8.8 Core - IP 8.8.8.8 - Anycast true - Hostname dns.google - City Mountain View - Region California - Country United States (US) - Location 37.4056,-122.0775 - Organization AS15169 Google LLC - Postal 94043 - Timezone America/Los_Angeles
把CMD[]設為空之後,再重新build一個新的鏡像,這樣子在輸入docker run --it ipinfo 時,就不會通過,要求使用者一定要在後面再接一個參數,比如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#創建容器,出現錯誤提示 root@kite-virtual-machine:/home/kite/Desktop/genDockerImagesFolder# docker run -it ipinfo docker: Error response from daemon: No command specified. See 'docker run --help'. #帶參數 root@kite-virtual-machine:/home/kite/Desktop/genDockerImagesFolder# docker run -it ipinfo ipinfo 8.8.8.8 Core - IP 8.8.8.8 - Anycast true - Hostname dns.google - City Mountain View - Region California - Country United States (US) - Location 37.4056,-122.0775 - Organization AS15169 Google LLC - Postal 94043 - Timezone America/Los_Angeles
6. 容器啟動命令ENTRYPOINT
ENTRYPOINT也可以設置容器啟動時要執行的命令,但是和CMD是有區別的。
CMD設置的命令,可以在docker container run 時傳入其它命令,覆蓋掉CMD的命令,但是ENTRYPOINT所設置的命令是一定會被執行的。
ENTRYPOINT和CMD可以聯合使用,ENTRYPOINT設置執行的命令,CMD傳遞參數
7. SHELL格式和EXEC格式
CMD和ENTRYPOINT同時支持SHELL格式和EXEC格式。
7.1. shell格式
1
CMDecho"hello docker"
1
ENTRYPOINTecho"hello docker"
7.2. exec格式
1
ENTRYPOINT ["echo", "hello docker"]
1
CMD ["echo", "hello docker"]
7.3. 注意事項舉例
比方說要print 出hello docker
shell版本
1 2 3
FROM ubuntu:21.04 ENV NAME=docker CMDecho"hello $NAME"
我們試著改成exec格式,如下面這樣,其實是不行的喔。
1 2 3
FROM ubuntu:21.04 ENV NAME=docker CMD ["echo", "hello $NAME"]
具體方式要以shell腳本的方式去執行
1 2 3
FROM ubuntu:21.04 ENV NAME=docker CMD ["sh", "-c", "echo hello $NAME"]
#建立一個資料夾名稱為pythonworkspace kite@kite-virtual-machine:~/Desktop$ mkdir pythonworkspace #進入pythonworkspace資料夾 kite@kite-virtual-machine:~/Desktop$ cd pythonworkspace/ #安裝python3-virtualenv套件 kite@kite-virtual-machine:~/Desktop/pythonworkspace$ sudo apt install python3-virtualenv Reading package lists... Done Building dependency tree Reading state information... Done python3-virtualenv is already the newest version (20.0.17-1ubuntu0.4). The following packages were automatically installed and are no longer required: libc++1 libc++1-10 libc++abi1-10 libsss-nss-idmap0 Use 'sudo apt autoremove' to remove them. 0 upgraded, 0 newly installed, 0 to remove and 36 not upgraded. #建立一個獨立空間的python環境,名稱為flaskwebsite kite@kite-virtual-machine:~/Desktop/pythonworkspace$ python3 -m venv flaskwebsite #執行進入到flaskwebsite空間 kite@kite-virtual-machine:~/Desktop/pythonworkspace$ source ./flaskwebsite/bin/activate #發現前面就會帶(flaskwebsite)空間名稱囉,再用pip list看目前套件清單 (flaskwebsite) kite@kite-virtual-machine:~/Desktop/pythonworkspace$ pip list Package Version ------------- ------- pip 20.0.2 pkg-resources 0.0.0 setuptools 44.0.0 #安裝flask套件 (flaskwebsite) kite@kite-virtual-machine:~/Desktop/pythonworkspace$ pip install Flask
root@kite-virtual-machine:/home/kite/Desktop/cworkspace# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-apline latest aed1c277cad0 About a minute ago 6.55MB gcc 9.4 426442df2c0a 3 weeks ago 1.14GB alpine 3.13.5 6dbb9cc54074 10 months ago 5.61MB