機械革命無界 16 筆記本 32GB 大記憶體版上架:i7-12700H / 2.5K 120Hz 屏,6299 元
一、映象概念
前置操作拉取映象:
# 拉取映象 # 拉取最新版本的映象 [root@hqs ~]# docker pull ubuntu Using default tag: latest latest: Pulling from library/ubuntu 7c3b88808835: Already exists Digest: sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9 Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest # 拉取指定版本映象 [root@hecs-hqs-01 ~]# docker pull ubuntu:14.04 14.04: Pulling from library/ubuntu 2e6e20c8e2e6: Pull complete 0551a797c01d: Pull complete 512123a864da: Pull complete Digest: sha256:60840958b25b5947b11d7a274274dc48ab32a2f5d18527f5dae2962b64269a3a Status: Downloaded newer image for ubuntu:14.04 docker.io/library/ubuntu:14.04
二、映象檢視
1、映象檢視示例
# 檢視本地主機上的映象 [root@hqs ~]# docker images 映象倉庫 標籤 映象ID 建立時間 映象大小 REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 54c9d81cbb44 4 weeks ago 72.8MB ubuntu 14.04 13b66b487594 11 months ago 197MB hello-world latest feb5d9fea6a5 5 months ago 13.3kB # 檢視本地某個映象 # 加上--no-trunc顯示完整映象ID:UUID形式表示,64個的十六進位制字元 # 實際上映象ID是映象的摘要值(Digest),是由雜湊函式sha256對映象配置檔案計算而來的 [root@hecs-hqs-01 ~]# docker images ubuntu --no-trunc REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest sha256:54c9d...f704f 4 weeks ago 72.8MB # 加上--digests選項可以顯示映象摘要值 [root@hqs ~]# docker images ubuntu --digests 映象倉庫 標籤 映象的摘要值 映象ID 建立時間 映象大小 REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE ubuntu latest sha256:8ae9b...3961f9 2b4cba85892a 4 days ago 72.8MB # 同時輸出摘要值和完整映象id [root@hqs ~]# docker images ubuntu --digests --no-trunc REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE ubuntu latest sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9 sha256:2b4cba85892afc2ad8ce258a8e3d9daa4a1626ba380677cee93ef2338da442ab 4 days ago 72.8MB ubuntu 14.04 sha256:60840958b25b5947b11d7a274274dc48ab32a2f5d18527f5dae2962b64269a3a sha256:13b66b487594a1f2b75396013bc05d29d9f527852d96c5577cc4f187559875d0 11 months ago 197MB
2、映象標識
映象可以通過映象ID、映象名稱(含標籤)、映象摘要值來標識或引用。
# 可以用映象倉庫名稱:標籤來標識映象 [root@hqs ~]# docker images ubuntu:14.04 --digests REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE ubuntu 14.04 <none> 13b66b487594 11 months ago 197MB # 可以用映象id來標識映象 [root@hqs docker-hello]# docker rmi 2b4cba85892a Untagged: ubuntu:latest Untagged: ubuntu@sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9 Deleted: sha256:2b4cba85892afc2ad8ce258a8e3d9daa4a1626ba380677cee93ef2338da442ab Deleted: sha256:68a85fa9d77ecac87de23805c4be8766bda08a86787e324036cbcf84b62640fa # 映象可以通過摘要值作為內容定址識別符號,格式為:倉庫名稱@摘要 [root@hqs ~]# docker images ubuntu@sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9 REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu <none> 2b4cba85892a 4 days ago 72.8MB
三、映象描述檔案Dockerfile
Docker所用的Dockerfile檔案用來描述映象,定義瞭如何構建Docker映象。
Dockerfile是一個文字檔案,包含了要構建映象的所有命令。
Docker通過讀取Dockerfile中的指令自動構建映象。
1、建立hello-world映象案例
# 1、找一個目錄建立目錄
[root@hqs hqs]# mkdir docker-hello
# 2、進入這個目錄後建立Dockerfile檔案:
vi Dockerfile
FROM scratch
COPY hello /
CMD ["/hello"] # 注意:CMD後要空格
# 3、再建立hello.c檔案
[root@hqs hqs]# vi hello.c
#include<stdio.h>
void main (){
printf("hello docker\n");
}
# 4、編譯hello檔案
# 首先安裝gcc、glibc-static
yum install -y gcc
yum install -y glibc-static
# 進行編譯
[root@hqs docker-hello]# gcc --static hello.c -o hello
[root@hqs docker-hello]# ls
Dockerfile hello hello.c
# 4、基於dockerfile建立映象
# Dockerfile檔案執行build命令來構建映象
[root@hqs docker-hello]# docker build -t my-hello .
Sending build context to Docker daemon 865.3kB
Step 1/3 : FROM scratch
--->
Step 2/3 : COPY hello /
---> 32b0ccadf376
Step 3/3 : CMD ["/hello"]
---> Running in fc65eb581596
Removing intermediate container fc65eb581596
---> 27f012ef630b
Successfully built 27f012ef630b
Successfully tagged my-hello:latest
# 5、檢視映象
[root@hqs docker-hello]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
my-hello latest 27f012ef630b 44 seconds ago 861kB
# 6、執行映象
[root@hqs docker-hello]# docker run my-hello
hello docker
2、dockerfile語法初解
父映象(Parent Image):該映象的Dockerfile檔案中由FROM指定的映象。後續指令都應用到這個父映象中。
基礎映象(Base Image):不依賴其他映象,從零開始構建的映象。如:未提供FROM指令或提供FROM scratch指令所構建的映象。
FROM scratch # FROM命令定義基礎映象,scratch是空白映象,表示從零開始構建
COPY hello / # 將檔案複製到映象根目錄
CMD ["/hello"] # 映象啟動容器時執行/hello這個可執行檔案
四、基於聯合檔案系統的映象分層
五、Docker映象操作命令
docker新版本提供了一個統一的映象操作命令:docker image.
操作和傳統的映象操作docker子命令相對應,個別語法不一樣。
1、拉取映象
從映象倉庫中下載映象到本地,一般是儲存在/var/lib/docker目錄
[root@hqs /]# docker image pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
# 傳統寫法
[root@hecs-hqs-01 ~]# docker pull ubuntu:14.04
[root@hecs-hqs-01 ~]# docker pull ubuntu
2、顯示本地映象列表
# 檢視語法
[root@hqs ~]# docker images --help
Usage: docker images [OPTIONS選項] [REPOSITORY倉庫[:TAG標籤]] # 檢視語法只能用映象倉庫或者映象倉庫+標籤
List images
Options:
-a, --all Show all images (default hides intermediate images) # 列出本地所有的映象(含中間映象)
--digests Show digests # 顯示映象摘要值
-f, --filter filter Filter output based on conditions provided # 顯示過濾的映象
--format string Pretty-print images using a Go template
--no-trunc Dont truncate output # 不截斷輸出(顯示完整的映象id)
-q, --quiet Only show numeric IDs # 只顯示映象ID
# 檢視本地映象案例
[root@hqs docker-hello]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-hello latest 7131e3b64d77 42 minutes ago 861kB
ubuntu latest 2b4cba85892a 5 days ago 72.8MB
[root@hqs docker-hello]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-hello latest 7131e3b64d77 42 minutes ago 861kB
ubuntu latest 2b4cba85892a 5 days ago 72.8MB
# -f選項可以通過dangling的布林值列出無標籤的映象(?沒有輸出)
# -f 選項還可以匹配映象名、映象ID、映象DIGEST識別符號
[root@hqs /]# docker images -f dangling=true
REPOSITORY TAG IMAGE ID CREATED SIZE
# -f before=(<映象名>[:標籤]|<映象ID>|<映象digest>) 過濾出指定映象之前建立的
# -f since=(<映象名>[:標籤]|<映象ID>|<映象digest>) 過濾出指定映象之後建立的
# 使用映象名加標籤標識
[root@hqs docker-hello]# docker images -f before=ubuntu:latest
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/httpd latest 2cc07fbb5000 3 years ago 258MB
centos/httpd version1.0 2cc07fbb5000 3 years ago 258MB
# 使用映象ID標識
[root@hqs docker-hello]# docker images -f before=2b4cba85892a
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/httpd latest 2cc07fbb5000 3 years ago 258MB
centos/httpd version1.0 2cc07fbb5000 3 years ago 258MB
# 使用digest值標識
[root@hqs docker-hello]# docker images -f since=golang@sha256:c72fa9afc50b3303e8044cf28fb358b48032a548e1825819420fd40155a131cb
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-hello latest 709f66f2ceec 35 minutes ago 861kB
ubuntu latest 2b4cba85892a 6 days ago 72.8MB
# 通過shell命令替換docker images命令實現映象的批量操作(重點)
[root@hqs /]# docker images -q
27f012ef630b
2b4cba85892a
5d0da3dc9764
13b66b487594
[root@hqs /]# docker rmi $(docker images centos -q)
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
3、設定映象標籤
標籤:描述映象的版本資訊,可以使用docker tag為映象新增標籤,即為映象命名。
映象倉庫可以有多個標籤。一個映象也可以有多個標籤。
如果一個映象有多個標籤,只有當最後一個標籤被刪除時,才真正刪除映象。
# 基本語法
# 建立標籤讓目標映象關聯到源映象
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
docker tag 源映象[:標籤] 目標映象[:標籤]
# 下載centos/httpd映象
[root@hqs ~]# docker pull centos/httpd
Using default tag: latest
latest: Pulling from centos/httpd
a02a4930cb5d: Pull complete
628eaef4a9e0: Pull complete
20c0ca1c0cd5: Pull complete
30cf2fb1a57e: Pull complete
Digest: sha256:26c6674463ff3b8529874b17f8bb55d21a0dcf86e025eafb3c9eeee15ee4f369
Status: Downloaded newer image for centos/httpd:latest
docker.io/centos/httpd:latest
# 為由映象ID標識的映象加標籤
[root@hqs ~]# docker tag 2cc07fbb5000 centos/httpd:version1.0
[root@hqs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/httpd latest 2cc07fbb5000 3 years ago 258MB
centos/httpd version1.0 2cc07fbb5000 3 years ago 258MB
# 為由倉庫名稱標識的映象加上標籤
[root@hqs docker-hello]# docker tag centos/httpd centos/httpd:version2.0
[root@hqs docker-hello]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/httpd latest 2cc07fbb5000 3 years ago 258MB
centos/httpd version1.0 2cc07fbb5000 3 years ago 258MB
centos/httpd version2.0 2cc07fbb5000 3 years ago 258MB
# 為由倉庫名稱和映象ID組合標識的映象加上標籤 存疑
[root@hqs ~]# docker tag centos/httpd:latest centos/httpd:version3.0.test
[root@hqs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos/httpd latest 2cc07fbb5000 3 years ago 258MB
centos/httpd version1.0 2cc07fbb5000 3 years ago 258MB
centos/httpd version2.0 2cc07fbb5000 3 years ago 258MB
centos/httpd version3.0.test 2cc07fbb5000 3 years ago
# 指定一個註冊伺服器的主機名(可能包含埠) 存疑
[root@hqs ~]# docker tag 2cc07fbb5000 aliyun:5000/centos/httpd:version4.0
[root@hqs ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
aliyun:5000/centos/httpd version4.0 2cc07fbb5000 3 years ago 258MB
4、檢視映象詳細資訊
docker inspect
命令檢視Docker物件(映象、容器、任務)的詳細資訊。
- 預設以JSON陣列格式輸出結果。
- 可以使用
-f(--format)
選項指定特定內容輸出
# 語法
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects 返回詳細docker物件資訊
Options:
-f, --format string Format the output using the given Go template # 格式化輸出
-s, --size Display total file sizes if the type is container # 容器型別顯示全部檔案大小
--type string Return JSON for specified type # 返回json格式資訊
[root@hqs ~]# docker inspect centos/httpd
[
{
"Id": "sha256:2cc07fbb5000234e85b7ef63b6253f397491959af2a24251b6ae20c207beb814",
...
"Metadata": {
"LastTagTime": "2022-03-10T02:35:56.032309948+08:00"
}
}
]
# 獲取映象的體系結構
[root@hqs ~]# docker inspect --format='{{.Architecture}}' centos/httpd
amd64
# 獲取映象的根檔案系統資訊
[root@hqs ~]# docker inspect --format='{{.RootFS}}' centos/httpd
{layers [sha256:071d8bd765171080d01682844524be57ac9883e53079b6ac66707e192ea25956 sha256:d15c61d3ecdaa582e75e2966792238c0325578ec9e0d2a1ed3183995345323d6 sha256:7c937d8a9f4f7a9761c292a6c7bcff1cc10384985282fa4fa5a04bfdb155bc90 sha256:920640105caf8a18f7db0df2638b863cbc73c2e0668d0559c5f9c93c474e8879] }
# 用json格式輸出
[root@hqs ~]# docker inspect --format='{{json .RootFS}}' centos/httpd # 注意json後空格
{"Type":"layers","Layers":["sha256:071d8bd765171080d01682844524be57ac9883e53079b6ac66707e192ea25956","sha256:d15c61d3ecdaa582e75e2966792238c0325578ec9e0d2a1ed3183995345323d6","sha256:7c937d8a9f4f7a9761c292a6c7bcff1cc10384985282fa4fa5a04bfdb155bc90","sha256:920640105caf8a18f7db0df2638b863cbc73c2e0668d0559c5f9c93c474e8879"]}
# grep過濾沒有-f好用
[root@hqs ~]# docker inspect golang | grep Architecture
"Architecture": "amd64",
[root@hqs ~]# docker inspect golang | grep RootFS
"RootFS": {
5、檢視映象的構建歷史
可以使用 docker history
來檢視映象的構建歷史,即 DockerFile的執行過程。
每一層映象都相當於一個子映象。
# 語法
Usage: docker history [OPTIONS] IMAGE
Show the history of an image
Options:
--format string Pretty-print images using a Go template
-H, --human Print sizes and dates in human readable format (default true)
--no-trunc Dont truncate output
-q, --quiet Only show numeric IDs
[root@hqs docker-hello]# docker history docker-hello
IMAGE CREATED CREATED BY (構建操作命令) SIZE COMMENT
709f66f2ceec 3 minutes ago /bin/sh -c #(nop) CMD ["/hello"] 0B
e5dd9d7e1e8f 3 minutes ago /bin/sh -c #(nop) COPY file:437c07ddb3f63bb1… 861kB
[root@hqs docker-hello]# docker history ubuntu
IMAGE CREATED CREATED BY SIZE COMMENT
2b4cba85892a 5 days ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 5 days ago /bin/sh -c #(nop) ADD file:8a50ad78a668527e9… 72.8MB
[root@hqs docker-hello]# docker history ubuntu:14.04
IMAGE CREATED CREATED BY SIZE COMMENT
13b66b487594 11 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 11 months ago /bin/sh -c mkdir -p /run/systemd && echo do… 7B
<missing> 11 months ago /bin/sh -c [ -z "$(apt-get indextargets)" ] 0B
<missing> 11 months ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 195kB
<missing> 11 months ago /bin/sh -c #(nop) ADD file:276b5d943a4d284f8… 196MB
輸出結果中 CREATED BY
列顯示的是每一層的構建操作命令。如果顯示不全,可以使用 --no-trunc
選項來顯示完整的操作命令。
[root@hqs docker-hello]# docker history ubuntu:14.04 --no-trunc
另外,執行該命令時,有時會輸出 <missing>
行,這表明響應的層在其他系統已經構建了,並且已經不可用嗎,可以忽略。
6、查詢映象
在命令列中可以使用 docker search
命令搜尋docker hub中的映象。
# 語法
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Dont truncate output
# 示例
[root@hqs docker-hello]# docker search mysql
映象倉庫(源)名稱 描述 星數(點贊收藏) 是否官方釋出 自動化
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 12227 [OK]
mariadb MariaDB Server is a high performing open sou… 4693 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 907 [OK]
7、刪除本地映象
使用映象的ID、標籤、映象摘要識別符號來指定刪除本地映象。
如果一個映象有多個標籤,則當最後一個標籤被刪除時,映象才真正刪除。
# docker子命令語法
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image # 強制刪除
--no-prune Do not delete untagged parents # 不刪除沒有標籤的父映象
# docker image 子命令語法
Usage: docker image rm [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Aliases: rm, rmi, remove
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
# 映象ID標識
[root@hqs docker-hello]# docker rmi 2cc07fbb5000
# 映象標籤標識
[root@hqs docker-hello]# docker rmi ubuntu:14.04
# 映象摘要標識
[root@hqs docker-hello]# docker image rm ubuntu@sha256:8ae9bafbb64f63a50caab98fd3a5e37b3eb837a3e0780b78e5218e63193961f9
# 特別案例:映象摘要標識,先刪了摘要再刪了映象
[root@hqs ~]# docker images --digests
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
golang latest sha256:c72fa9afc50b3303e8044cf28fb358b48032a548e1825819420fd40155a131cb 276895edf967 2 months ago 941MB
centos latest sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 5d0da3dc9764 5 months ago 231MB
[root@hqs ~]# docker image rm golang@sha256:c72fa9afc50b3303e8044cf28fb358b48032a548e1825819420fd40155a131cb
Untagged: golang@sha256:c72fa9afc50b3303e8044cf28fb358b48032a548e1825819420fd40155a131cb
[root@hqs ~]# docker images --digests -a
REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE
golang latest <none> 276895edf967 2 months ago 941MB
centos latest sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 5d0da3dc9764 5 months ago 231MB
[root@hqs ~]# docker image rm golang:latest
Untagged: golang:latest
Deleted: sha256:276895edf9673267f47528e8a99401f2d2947f6c9c00490f773d5ed8f559bef2
Deleted: sha256:f9925574d34663c6f0f2135512cd1e7b94490279982657a6a40fb0693ce9df41
Deleted: sha256:5ba934ce54ed16893dd8cae2c36bdcc25f9cb1a4d51dba9cbedda4b4f1bbf53f
Deleted: sha256:3a9da346a75c7c4cdacacd945f87d73b964a07007c4e5e8f9435c367176ceeb9
Deleted: sha256:cbce712ed17923285239f9d9c0528984aef065b7413d68a0290e2c8eecc98f4a
Deleted: sha256:aa56d037ee5925ebf11127c3e1f617874c4ce8bae6b6af7d132b7f7a4a606e6f
Deleted: sha256:97e5f44efb543d466c5847602654a8cb22c9466b61d04988d47ec44b197ea874
Deleted: sha256:11936051f93baf5a4fb090a8fa0999309b8173556f7826598e235e8a82127bce
8、驗證映象分層結構(構建映象)
驗證基於 Dockerfile
檔案的映象分層。
# 1、構建專案目錄,存放Dockerfile和相關檔案
[root@hqs ~]# mkdir /home/hqs/imglayers && cd /home/hqs/imglayers
# 2、建立app子目錄和py檔案
[root@hqs imglayers]# mkdir app && cd app
[root@hqs app]# vi app.py
#!/usr/bin/python
print("Hello, World!@!!!!!!")
# 3、編輯Dockerfile檔案
[root@hqs app]# cd ..
[root@hqs imglayers]# vi Dockerfile
FROM ubuntu:16.04
COPY ./app /app
RUN apt-get -y update && apt-get install -y python
CMD python /app/app.py
# 4、構建映象
[root@hqs imglayers]# docker build -t="imglayers-test" .
# 5、檢視映象的分層資訊
[root@hqs imglayers]# docker history imglayers-test
IMAGE CREATED CREATED BY SIZE COMMENT
9ef4ec73ba1e 8 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "pyth… 0B
aaec11036262 8 minutes ago /bin/sh -c apt-get -y update && apt-get inst… 64.1MB
c53a6ee7c2be 11 minutes ago /bin/sh -c #(nop) COPY dir:c47add2988178f681… 48B
b6f507652425 6 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 6 months ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 6 months ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B
<missing> 6 months ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B
<missing> 6 months ago /bin/sh -c #(nop) ADD file:11b425d4c08e81a3e… 135MB
# 6、執行映象
[root@hqs imglayers]# docker run imglayers-test
Hello, World!@!!!!!!