本文開啟了CI/CD基礎入門,遵循以下原則:理論 + 實戰 + 總結。
1.1 拉取鏡像
指令:
docker [image] pull [regirster]NAME [ :TAG]
其中:
regirster:倉庫位址,預設registry.hub.docker.com。
NAME:鏡像名稱,如nginx。
TAG:鏡像标簽,預設為 latest 最新的釋出版本。
pull 子指令支援的 選項主要包括:
-a,=true|false: 是否擷取倉庫中的所有鏡像,預設為否;
-disable-con:取消鏡像的内容校驗,預設為真。
–registry-mirror=proxy_URL:來指定鏡像代理服務位址。
執行 docker pull nginx:latest,過程如下:
Trying to pull repository docker.io/library/nginx ...
latest: Pulling from docker.io/library/nginx
33847f680f63: Pull complete
dbb907d5159d: Pull complete
8a268f30c42a: Pull complete
b10cf527a02d: Pull complete
c90b090c213b: Pull complete
1f41b2f2bf94: Pull complete
Digest: sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90
Status: Downloaded newer image for docker.io/nginx:latest
1.2 檢視鏡像資訊
1.2.1 列出鏡像
docker images
[[email protected] dockerStudy]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 08b152afcfae 3 weeks ago 133 MB
docker.io/mysql 5.7 2c9028880e58 3 months ago 447 MB
docker.io/redis latest 41de2cc0b30e 11 months ago 104 MB
Tips:
- 根據鏡像名稱模糊查找:docker images ngin*
-
根據過濾篩選:docker images -f “k1=v1” -f “k2=v2”
dangling=true|false:顯示标記為空的鏡像。
label:這個是根據标簽進行過濾,其中lable的值,是docker在編譯的時候配置的或者 在Dockerfile中配置的。
before:這個是根據時間來進行過濾,其中before的value(某個鏡像建構時間)之前的鏡像清單。
since:跟before正好相反,表示的是在某個鏡像建構之後建構的鏡像。
reference:這個是添加正則進行比對。
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 08b152afcfae 3 weeks ago 133 MB
nginx lfc 08b152afcfae 3 weeks ago 133 MB
docker.io/hyperf/hyperf 7.4-alpine-v3.12-swoole-v4.6 9ad354753fb9 5 weeks ago 110 MB
docker.io/mysql 5.7 2c9028880e58 3 months ago 447 MB
docker.io/redis latest 41de2cc0b30e 11 months ago 104 MB
[[email protected] ~]# docker images -f "before=nginx:lfc"
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hyperf/hyperf 7.4-alpine-v3.12-swoole-v4.6 9ad354753fb9 5 weeks ago 110 MB
docker.io/mysql 5.7 2c9028880e58 3 months ago 447 MB
docker.io/redis latest 41de2cc0b30e 11 months ago 104 MB
[[email protected] ~]# docker images -f "since=docker.io/redis:latest" -f "before=nginx:lfc"
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hyperf/hyperf 7.4-alpine-v3.12-swoole-v4.6 9ad354753fb9 5 weeks ago 110 MB
docker.io/mysql 5.7 2c9028880e58 3 months ago 447 MB
[[email protected] ~]# docker images -f "label=maintainer"
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx lfc 08b152afcfae 3 weeks ago 133 MB
docker.io/nginx latest 08b152afcfae 3 weeks ago 133 MB
docker.io/hyperf/hyperf 7.4-alpine-v3.12-swoole-v4.6 9ad354753fb9 5 weeks ago 110 MB
1.2.2 添加鏡像标簽
docker tag NAME:TAG NAME2:TAG2
[[email protected] dockerStudy]# docker tag nginx:latest nginx:lfc
[[email protected] dockerStudy]# docker images nginx*
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 08b152afcfae 3 weeks ago 133 MB
nginx lfc 08b152afcfae 3 weeks ago 133 MB
可以發現IMAGE ID 和原來是一樣的,可以簡單了解為 Linux 的軟連結,Windows的快捷方式。
1.2.3 檢視鏡像資訊
docker inspect [-f {{".option"}}] NAME:TAG
傳回的是一個JSON數組結構。
[[email protected] dockerStudy]# docker inspect -f {{".RepoTags"}} nginx:lfc
[docker.io/nginx:latest nginx:lfc]
1.2.4 檢視鏡像曆史
docker history NAME:TAG
列出各層的鏡像資訊
[[email protected] dockerStudy]# docker history nginx:lfc
IMAGE CREATED CREATED BY SIZE COMMENT
08b152afcfae 3 weeks ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daem... 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) EXPOSE 80 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-en... 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) COPY file:09a214a3e07c91... 4.61 kB
<missing> 3 weeks ago /bin/sh -c #(nop) COPY file:0fd5fca330dcd6... 1.04 kB
<missing> 3 weeks ago /bin/sh -c #(nop) COPY file:0b866ff3fc1ef5... 1.96 kB
<missing> 3 weeks ago /bin/sh -c #(nop) COPY file:65504f71f5855c... 1.2 kB
<missing> 3 weeks ago /bin/sh -c set -x && addgroup --system... 63.9 MB
<missing> 3 weeks ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~buster 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) ENV NJS_VERSION=0.6.1 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.21.1 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL maintainer=NGINX ... 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0 B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:45f5dfa135c848a... 69.3 MB
1.3 删除和清理鏡像
1.3.1 使用标簽删除鏡像
docker image rm NAME:TAG
-f:強制删除,即使有容器依賴該鏡像,不建議使用該選項,而是 停容器->删容器->删鏡像。
-no-prune:不要清理未帶标簽的父鏡像。
注意: 隻剩下一個标簽的時候,會徹底删除鏡像(以及其下所有檔案層)。
1.3.2 使用鏡像ID删除鏡像
docker image rm IMAGE_ID
1.3.3 清理鏡像
使用Docker一段時間後, 系統中可能會遺留一些臨時的鏡像檔案, 以及一些沒有被使
用的鏡像。(類比于Windows中的程式垃圾,也是喜歡MacOS的理由,哈哈)
docker image prune
-a:删除所有無用鏡像(沒有該鏡像起的容器)。
-filter:清理滿足過濾條件的鏡像。
-f:強制删除鏡像。
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest 08b152afcfae 3 weeks ago 133 MB
nginx lfc 08b152afcfae 3 weeks ago 133 MB
docker.io/hyperf/hyperf 7.4-alpine-v3.12-swoole-v4.6 9ad354753fb9 5 weeks ago 110 MB
docker.io/mysql 5.7 2c9028880e58 3 months ago 447 MB
docker.io/redis latest 41de2cc0b30e 11 months ago 104 MB
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b63b1f311d10 hyperf/hyperf:7.4-alpine-v3.12-swoole-v4.6 "/bin/bash" 4 weeks ago Up 4 weeks 0.0.0.0:9501->9501/tcp hyperf
34bcf7a33154 mysql:5.7 "docker-entrypoint..." 3 months ago Up 5 weeks 0.0.0.0:3306->3306/tcp, 33060/tcp lfc-mysql
c3dcb2bac090 docker.io/redis "docker-entrypoint..." 11 months ago Up 5 weeks 0.0.0.0:6379->6379/tcp myredis
[[email protected] ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: docker.io/nginx:latest
untagged: docker.io/[email protected]:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90
untagged: nginx:lfc
deleted: sha256:08b152afcfae220e9709f00767054b824361c742ea03a9fe936271ba520a0a4b
deleted: sha256:97386f823dd75e356afac10af0def601f2cd86908e3f163fb59780a057198e1b
deleted: sha256:316cd969204ae854302bc55c610698829c9f23fa6fcd4e0f69afa6f29fedfd68
deleted: sha256:dcec23d16cb7cdbd725dc0024f38b39fd326066fc59784df92b40fc05ba3728f
deleted: sha256:1e294000374b3a304c2bfcfe51460aa599237149ed42e3423ac2c3f155f9b4a5
deleted: sha256:c0d318592b21711dc370e180acd66ad5d42f173d5b58ed315d08b9b09babb84a
deleted: sha256:814bff7343242acfd20a2c841e041dd57c50f0cf844d4abd2329f78b992197f4
Total reclaimed space: 133.2 MB
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hyperf/hyperf 7.4-alpine-v3.12-swoole-v4.6 9ad354753fb9 5 weeks ago 110 MB
docker.io/mysql 5.7 2c9028880e58 3 months ago 447 MB
docker.io/redis latest 41de2cc0b30e 11 months ago 104 MB
1.4 建立鏡像
1.4.1 基于已有的容器
docker [container] commit [OPTIONS] CONTAINER [REPOSITORY [:TAG]]
-a:作者。
-c:送出的時候執行Dockerfile指令, 包括 CMD I ENTRYPOINT | ENV I EXPOSE I LABEL I ONBUILD I USER I VOLUME I WORKDIR等。
-m:送出說明。
-p:送出時暫停容器。
[[email protected] ~]# docker run --name myNginx -p 8080:80 -d nginx
570c98d0654d1620c2f4850f2d0d3963510e43e1a0d35d2eb705218a557c4c15
[[email protected] ~]# docker commit -m "create my nginx image" -a "lfc" 570c98d0654d1620c2f4850f2d0d3963510e43e1a0d35d2eb705218a557c4c15 test:1.0
sha256:065ebeceed10c8661937a8c9a96a184d7f3ac5a94d66e7279028efda442213b0
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test 1.0 065ebeceed10 10 seconds ago 133 MB
docker.io/nginx latest 08b152afcfae 3 weeks ago 133 MB
docker.io/hyperf/hyperf 7.4-alpine-v3.12-swoole-v4.6 9ad354753fb9 5 weeks ago 110 MB
docker.io/mysql 5.7 2c9028880e58 3 months ago 447 MB
docker.io/redis latest 41de2cc0b30e 11 months ago 104 MB
1.4.2 基于Dockerfile
這是自定義鏡像的精髓,為了學習成體系。在此先簡單列出,之後的文章中會詳細道出。
1.5 導出和載入鏡像
1.5.1 導出
docker image save -o fileName NAME:TAG
[[email protected] ~]# docker image save -o nginxFile.tar nginx:latest
[[email protected] ~]# ll
-rw------- 1 root root 137441792 Aug 17 00:06 nginxFile.tar
1.5.2 載入
docker image load -i fileName
1.6 上傳鏡像
docker image push NAME[:TAG] [REGISTER_HOST[:REGISTER_PORT]/]NAME[:TAG]
接下來會需要登入資訊,填寫即可。
1.7 小結
本篇主要介紹的鏡像的基本操作,拉取、檢視、删除、建立、導出導入、上傳等。其中最關鍵的是基于Dockerfile建構自己的鏡像,其他的多用自然熟能生巧(工具的學習,不必去背,多用了就不會忘記)。
PS:理論部分參考楊保華的《Docker技術入門與實戰》,感興趣的可以閱讀之。