Docker镜像生命周期(可以把docker镜像理解为虚拟机镜像)
实验内容:
搜索官方仓库镜像
[root@localhost ~]# docker search busybox //以查找busybox为例
搜索说明:name镜像名称 description镜像说明 stars点赞数量 official是否是官方的 automated是否是自动构建的
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
busybox Busybox base image. 3225 [OK]
radial/busyboxplus Full-chain, Internet enabled, busybox made f… 56 [OK]
yauritux/busybox-curl Busybox with CURL 25
arm32v7/busybox Busybox base image. 10
arm64v8/busybox Busybox base image. 8
odise/busybox-curl 4 [OK]
i386/busybox Busybox base image. 3
s390x/busybox Busybox base image. 3
prom/busybox Prometheus Busybox Docker base images 2 [OK]
p7ppc64/busybox Busybox base image for ppc64. 2
hugegraph/busybox test image 2
joeshaw/busybox-nonroot Busybox container with non-root user nobody 2
busybox42/zimbra-docker-centos A Zimbra Docker image, based in ZCS 8.8.9 an… 2 [OK]
openebs/busybox-client 1
amd64/busybox Busybox base image. 1
vukomir/busybox busybox and curl 1
ppc64le/busybox Busybox base image. 1
privatebin/chown Docker image providing busybox' chown, stat… 1
antrea/busybox 1
spotify/busybox Spotify fork of https://hub.docker.com/_/bus… 1
busybox42/nginx_php-docker-centos This is a nginx/php-fpm server running on Ce… 1 [OK]
busybox42/haraka-docker-centos CentOS Haraka build with spamassassin, redis… 1 [OK]
busybox42/alpine-pod 0
rancher/busybox 0
concourse/busyboxplus 0
拉取镜像
[root@localhost ~]# docker pull busybox //拉取busybox(不指定版本时,默认拉取最近版本)
Using default tag: latest
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
[root@localhost ~]# docker pull ubuntu:18.04 //拉取ubuntu(也可以指定版本拉取)
18.04: Pulling from library/ubuntu
284055322776: Pull complete
Digest: sha256:0fedbd5bd9fb72089c7bbca476949e10593cebed9b1fb9edf5b79dbbacddd7d6
Status: Downloaded newer image for ubuntu:18.04
[root@localhost ~]# docker pull registry.cn-hangzhou.aliyuncs.com/kongd/openlab:ubuntu_18.04 //由于docker拉取时是基于docker hub有时国内站点访问时可能会速度较慢,所以可以指定拉取国内阿里等站点的,也可以自己构建docker镜像站进行拉取。这里不做过多赘述。
ubuntu_18.04: Pulling from kongd/openlab
Digest: sha256:fc0d6af5ab38dab33aa53643c4c4b312c6cd1f044c1a2229b2743b252b9689fc
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/kongd/openlab:ubuntu_18.04
查看镜像信息
查看当前主机列表
[root@localhost ~]# docker images(也可以使用docker image ls命令来查看)
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest beae173ccac6 2 years ago 1.24MB
ubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB
registry.cn-hangzhou.aliyuncs.com/kongd/openlab ubuntu_18.04 5a214d77f5d7 2 years ago 63.1MB
查看镜像详细信息
[root@localhost ~]# docker inspect busybox //在此以查看busybox镜像信息为例
[{"Id": "sha256:beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a","RepoTags": ["busybox:latest"],"RepoDigests": ["busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678"],"Parent": "","Comment": "","Created": "2021-12-30T19:19:41.006954958Z","Loaded": "2024-03-13T16:37:54.485607571+08:00","Container": "a0007fa726185ffbcb68e90f8edabedd79a08949f32f4f0bcc6e5fed713a72c8","ContainerConfig": {"Hostname": "a0007fa72618","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 [\"sh\"]"],"Image": "sha256:da658412c37aa24e561eb7e16c61bc82a9711340d8fb5cf1a8f39d8e96d7f723","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": {},"Annotations": null},"DockerVersion": "20.10.7","Author": "","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": ["sh"],"Image": "sha256:da658412c37aa24e561eb7e16c61bc82a9711340d8fb5cf1a8f39d8e96d7f723","Volumes": null,"WorkingDir": "","Entrypoint": null,"OnBuild": null,"Labels": null,"Annotations": null},"Architecture": "amd64","Os": "linux","Size": 1239820,"VirtualSize": 1239820,"GraphDriver": {"Data": {"MergedDir": "/var/lib/docker/overlay2/4beb70f0835dd66d616bd0ae25176d019236743fd00eec1bda704ec6872297b3/merged","UpperDir": "/var/lib/docker/overlay2/4beb70f0835dd66d616bd0ae25176d019236743fd00eec1bda704ec6872297b3/diff","WorkDir": "/var/lib/docker/overlay2/4beb70f0835dd66d616bd0ae25176d019236743fd00eec1bda704ec6872297b3/work"},"Name": "overlay2"},"RootFS": {"Type": "layers","Layers": ["sha256:01fd6df81c8ec7dd24bbbd72342671f41813f992999a3471b9d9cbc44ad88374"]},"Metadata": {"LastTagTime": "0001-01-01T00:00:00Z"}}
]
删除镜像
[root@localhost ~]# docker rmi ubuntu:18.04(也可用docker image rm ubuntu:18.04命令完成)如果删除时需要进行强制删除可以采用-f参数
Untagged: ubuntu:18.04
Untagged: ubuntu@sha256:0fedbd5bd9fb72089c7bbca476949e10593cebed9b1fb9edf5b79dbbacddd7d6
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest beae173ccac6 2 years ago 1.24MB
myubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB
导出导入镜像
ip | 主机名 |
192.168.27.137 | 导出 |
192.168.27.141 | 导入 |
导出
[root@localhost ~]# docker save -o images.tar busybox:latest myubuntu:18.04
-o:指定导出镜像的位置;
可以同时导出多个镜像;为一个文件;
指定.tar.gz 可以导出并压缩。
[root@localhost ~]# scp images.tar 192.168.27.137:~
The authenticity of host '192.168.27.137 (192.168.27.137)' can't be established.
ED25519 key fingerprint is SHA256:wKCxBY5GtfgHv2MW283QFPMTGf6L4CqjVBroqsX6wso.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.27.137' (ED25519) to the list of known hosts.Authorized users only. All activities may be monitored and reported.
root@192.168.27.137's password:
images.tar 100% 64MB 254.7MB/s 00:00
导入
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]# ll
总用量 65420
-rw-------. 1 root root 706 1月 23 19:05 anaconda-ks.cfg
-rw------- 1 root root 66983424 3月 13 17:00 images.tar
[root@localhost ~]# docker load -i images.tar
01fd6df81c8e: Loading layer 1.459MB/1.459MB
Loaded image: busybox:latest
824bf068fd3d: Loading layer 65.51MB/65.51MB
Loaded image: myubuntu:18.04
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest beae173ccac6 2 years ago 1.24MB
myubuntu 18.04 5a214d77f5d7 2 years ago 63.1MB