Docker - Docker Image及Image指令詳解
什麼是鏡像(Image)
Docker鏡像(Docker Image)就是一個隻讀的模闆。比如,一個鏡像可以包含一個完整的Ubuntu作業系統環境。鏡像可以用來建立Docker容器。
在Docker的術語裡,一個隻讀層被稱為鏡像,一個鏡像是永遠不會變的。
由于Docker使用一個統一檔案系統,Docker程序認為整個檔案系統是以讀寫方式挂載的。 但是所有的變更都發生在頂層的可寫層,而下層的原始的隻讀鏡像檔案并未變化。由于鏡像不可寫,是以鏡像是無狀态的。
父鏡像
每一個鏡像都可能依賴于由一個或多個下層鏡像組成的另一個鏡像。我們有時說,下層那個鏡像是上層鏡像的父鏡像。而沒有任何父鏡像的鏡像,謂之基礎鏡像(Base Image)。
可以看一下這篇部落格,會讓你對Docker的整體架構有一個很清晰的了解:Docker - 這應該就是你想要的Docker架構分析。
Docker Image Help
通過該指令可以檢視Image下有哪些指令。
docker image help
[root@izoq008ryseuupz ~]# docker image help
Usage: docker image COMMAND
Manage images
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
Run 'docker image COMMAND --help' for more information on a command.
通過
docker image COMMAND --help
來知道每一個Image指令的詳細資訊。
Run 'docker image COMMAND --help' for more information on a command.
Docker Image LS
docker image ls --help
[root@izoq008ryseuupz ~]# docker image ls --help
Usage: docker image ls [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
ls, images, list
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 Don't truncate output
-q, --quiet Only show numeric IDs
比如
docker image ls -a
,展示所有Image的關鍵資訊。
[root@izoq008ryseuupz ~]# docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
node latest 5377c9a2fb1f 5 weeks ago 943MB
openzipkin/zipkin latest 1850194f377c 3 months ago 160MB
rancher/server stable 98d8bb571885 6 months ago 1.08GB
redis 5.0.7 7eed8df88d3b 8 months ago 98.2MB
hello-world latest bf756fb1ae65 10 months ago 13.3kB
docker images
、
docker image ls
和
docker image ls -a
是一樣的效果。
比如
docker image ls -q
,就隻輸出Image的IMAGE ID 。
[root@izoq008ryseuupz ~]# docker image ls -q
5377c9a2fb1f
1850194f377c
98d8bb571885
7eed8df88d3b
bf756fb1ae65
其他的指令選項可以自己試一試。
Docker Image Pull
拉取鏡像。
docker image pull --help
[root@izoq008ryseuupz ~]# docker image pull --help
Usage: docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
拉取
centos:7
鏡像。
docker image pull centos:7
[root@izoq008ryseuupz ~]# docker image pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
拉取
centos:6
鏡像,隻想要輸出
pull
的簡要資訊,不輸出
pull
的詳細資訊,如
Downloading
的進度。
docker image pull -q centos:6
[root@izoq008ryseuupz ~]# docker image pull -q centos:6
docker.io/library/centos:6
其他的指令選項可以自己試一試。
Docker Image Prune
此指令會把所有未使用的鏡像進行删除(
Remove unused images
),慎用!
docker image prune --help
[root@izoq008ryseuupz ~]# docker image prune --help
Usage: docker image prune [OPTIONS]
Remove unused images
Options:
-a, --all Remove all unused images, not just dangling ones
--filter filter Provide filter values (e.g. 'until=<timestamp>')
-f, --force Do not prompt for confirmation
docker image prune
[root@izoq008ryseuupz ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
Docker Image RM
删除指定鏡像。
docker image rm --help
[root@izoq008ryseuupz ~]# docker image rm --help
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
删除
centos:7
。
docker image rm centos:7
[root@izoq008ryseuupz ~]# docker image rm centos:7
Untagged: centos:7
Untagged: centos@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Deleted: sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf
Deleted: sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02
強制删除
centos:6
。
docker image rm -f centos:6
[root@izoq008ryseuupz ~]# docker image rm -f centos:6
Untagged: centos:6
Untagged: centos@sha256:dec8f471302de43f4cfcf82f56d99a5227b5ea1aa6d02fa56344986e1f4610e7
Deleted: sha256:d0957ffdf8a2ea8c8925903862b65a1b6850dbb019f88d45e927d3d5a3fa0c31
Deleted: sha256:af6bf1987c2eb07d73f33836b0d8fd825d7c785273526b077e46780e8b4b2ae9
其他的指令選項可以自己試一試。
Docker Image Tag
對原有鏡像打
tag
,會生成新鏡像。
docker image tag --help
[root@izoq008ryseuupz ~]# docker image tag --help
Usage: docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker image tag centos:6 centos:kaven.blog
Docker Image Save
儲存鏡像。
docker image save --help
[root@izoq008ryseuupz ~]# docker image save --help
Usage: docker image save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
docker image save centos:kaven.blog > kaven.blog.tar
[root@izoq008ryseuupz ~]# docker image save centos:kaven.blog > kaven.blog.tar #儲存到目前目錄
[root@izoq008ryseuupz ~]# ls kaven*
kaven.blog.tar
docker image save centos:kaven.blog -o /usr/kaven.blog.tar
[root@izoq008ryseuupz ~]# docker image save centos:kaven.blog -o /usr/kaven.blog.tar #儲存到别的目錄
[root@izoq008ryseuupz ~]# ls /usr/kaven*
/usr/kaven.blog.tar
Docker Image Load
加載鏡像。
docker image load --help
[root@izoq008ryseuupz ~]# docker image load --help
Usage: docker image load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
docker image load < kaven.blog.tar
[root@izoq008ryseuupz ~]# docker image rm centos:kaven.blog
Untagged: centos:kaven.blog
[root@izoq008ryseuupz ~]# docker image load < kaven.blog.tar
Loaded image: centos:kaven.blog
docker image load -i /usr/kaven.blog.tar
[root@izoq008ryseuupz ~]# docker image rm centos:kaven.blog
Untagged: centos:kaven.blog
[root@izoq008ryseuupz ~]# docker image load -i /usr/kaven.blog.tar
Loaded image: centos:kaven.blog
其他的指令選項可以自己試一試。
Docker Image History
顯示鏡像的操作曆史。
docker image history --help
[root@izoq008ryseuupz ~]# docker image history --help
Usage: docker image 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 Don't truncate output
-q, --quiet Only show numeric IDs
docker image history centos:kaven.blog
[root@izoq008ryseuupz ~]# docker image history centos:kaven.blog
IMAGE CREATED CREATED BY SIZE COMMENT
d0957ffdf8a2 20 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 20 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 20 months ago /bin/sh -c #(nop) ADD file:0065316a41144e95b… 194MB
<missing> 2 years ago /bin/sh -c #(nop) MAINTAINER https://github… 0B
隻想展示鏡像操作曆史的簡要資訊(IMAGE)。
docker image history -q centos:6
[root@izoq008ryseuupz ~]# docker image history -q centos:6
d0957ffdf8a2
<missing>
<missing>
<missing>
其他的指令選項可以自己試一試。
Docker Image Inspect
顯示鏡像的詳細資訊。
docker image inspect --help
[root@izoq008ryseuupz ~]# docker image inspect --help
Usage: docker image inspect [OPTIONS] IMAGE [IMAGE...]
Display detailed information on one or more images
Options:
-f, --format string Format the output using the given Go template
docker image inspect centos:6
[root@izoq008ryseuupz ~]# docker image inspect centos:6
[
{
"Id": "sha256:d0957ffdf8a2ea8c8925903862b65a1b6850dbb019f88d45e927d3d5a3fa0c31",
"RepoTags": [
"centos:6",
"centos:kaven.blog"
],
"RepoDigests": [
"centos@sha256:dec8f471302de43f4cfcf82f56d99a5227b5ea1aa6d02fa56344986e1f4610e7"
],
"Parent": "",
"Comment": "",
"Created": "2019-03-14T21:20:11.486358099Z",
"Container": "d519f3e5c41d16388d3fba0dac626427b21deb98cce150dee80c180b9baf9435",
"ContainerConfig": {
"Hostname": "d519f3e5c41d",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/bash\"]"
],
"ArgsEscaped": true,
"Image": "sha256:143abcd43bce45f4fd9ba51c7361051d7ea9e9e1eadb66e5c94a9c1b7754524f",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20181006",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "18.06.1-ce",
"Author": "https://github.com/CentOS/sig-cloud-instance-images",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"ArgsEscaped": true,
"Image": "sha256:143abcd43bce45f4fd9ba51c7361051d7ea9e9e1eadb66e5c94a9c1b7754524f",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20181006",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 193901906,
"VirtualSize": 193901906,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/ae0f8ac6c397e63561b52142c4e945909b8af2351d28e5af66f3490ff7078587/merged",
"UpperDir": "/var/lib/docker/overlay2/ae0f8ac6c397e63561b52142c4e945909b8af2351d28e5af66f3490ff7078587/diff",
"WorkDir": "/var/lib/docker/overlay2/ae0f8ac6c397e63561b52142c4e945909b8af2351d28e5af66f3490ff7078587/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:af6bf1987c2eb07d73f33836b0d8fd825d7c785273526b077e46780e8b4b2ae9"
]
},
"Metadata": {
"LastTagTime": "2020-11-22T11:46:01.910880961+08:00"
}
}
]
其他的指令選項可以自己試一試。
Docker Image Import
從歸檔檔案中建立鏡像。
docker image import --help
[root@izoq008ryseuupz ~]# docker image import --help
Usage: docker image import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Set commit message for imported image
--platform string Set platform if server is multi-platform capable
docker image import kaven.blog.tar centos:6.kaven
[root@izoq008ryseuupz ~]# docker image import kaven.blog.tar centos:6.kaven
sha256:3a568400d73096f71c5f5165f110414bb83e8024bca2ee3eea77336a194e920f
其他的指令選項可以自己試一試。
Docker Image Push
上傳鏡像到倉庫。
docker image push --help
[root@izoq008ryseuupz ~]# docker image push --help
Usage: docker image push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
--disable-content-trust Skip image signing (default true)
Docker Image Build
建立鏡像。
docker image build --help
[root@izoq008ryseuupz ~]# docker image build --help
Usage: docker image build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
--build-arg list Set build-time variables
--cache-from strings Images to consider as cache sources
--cgroup-parent string Optional parent cgroup for the container
--compress Compress the build context using gzip
--cpu-period int Limit the CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit the CPU CFS (Completely Fair Scheduler) quota
-c, --cpu-shares int CPU shares (relative weight)
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--disable-content-trust Skip image verification (default true)
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
--force-rm Always remove intermediate containers
--iidfile string Write the image ID to the file
--isolation string Container isolation technology
--label list Set metadata for an image
-m, --memory bytes Memory limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--network string Set the networking mode for the RUN instructions during build (default "default")
--no-cache Do not use cache when building the image
-o, --output stringArray Output destination (format: type=local,dest=path)
--platform string Set platform if server is multi-platform capable
--progress string Set type of progress output (auto, plain, tty). Use plain to show container output (default "auto")
--pull Always attempt to pull a newer version of the image
-q, --quiet Suppress the build output and print image ID on success
--rm Remove intermediate containers after a successful build (default true)
--secret stringArray Secret file to expose to the build (only if BuildKit enabled): id=mysecret,src=/local/secret
--security-opt strings Security options
--shm-size bytes Size of /dev/shm
--squash Squash newly built layers into a single new layer
--ssh stringArray SSH agent socket or keys to expose to the build (only if BuildKit enabled) (format: default|<id>[=<socket>|<key>[,<key>]])
--stream Stream attaches to server to negotiate build context
-t, --tag list Name and optionally a tag in the 'name:tag' format
--target string Set the target build stage to build.
--ulimit ulimit Ulimit options (default [])
這裡會介紹将一個簡單的C語言程式,Build成一個Image。
先在目前目錄下建立一個
hello.c
程式。
vim hello.c
程式如下:
#include<stdio.h>
int main()
{
printf("hello kaven\n");
printf("this is docker\n");
}
不知道怎麼退出VIM,可以看一下這篇部落格:怎麼儲存退出 vim 編輯。
[root@izoq008ryseuupz ~]# ls
hello.c kaven.blog.tar logs mall.jar
[root@izoq008ryseuupz ~]# gcc hello.c -o hello
[root@izoq008ryseuupz ~]# ls
hello hello.c kaven.blog.tar logs mall.jar
[root@izoq008ryseuupz ~]# ./hello
hello kaven
this is docker
在目前目錄下建立
Dockerfile
。
vim Dockerfile
輸入:
FROM scratch
ADD hello /
CMD ["/hello"]
現在看不懂沒關系,自己跟着體驗一次,之後的部落格會詳細講解這些
Dockerfile
操作。
docker image build -t kaven/hello:v1 .
[root@izoq008ryseuupz ~]# docker image build -t kaven/hello:v1 .
Sending build context to Docker daemon 564.4MB
Step 1/3 : FROM scratch
--->
Step 2/3 : ADD hello /
---> 5d49f50c725b
Step 3/3 : CMD ["/hello"]
---> Running in b0b3bdce9078
Removing intermediate container b0b3bdce9078
---> b1013ca7925d
Successfully built b1013ca7925d
Successfully tagged kaven/hello:v1
這樣
kaven/hello:v1
就有了。