镜像操作案例
查找镜像
docker search busybox
下载镜像
docker pull busybox:1.36.0
查看镜像及列表存储位置
root@LAPTOP-H2EI4I6A:~# docker images busybox
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 517b897a6a83 2 months ago 4.27MB
查看 overlay2/repositories.json 文件,该文件记录已拉取镜像文件的信息
查看本地镜像, 发现和 repositories.json 文件记录的信息相同
镜像过滤
○ 根据仓库名列出镜像
列出特定的某个镜像,也就是说指定仓库名和标签
root@LAPTOP-H2EI4I6A:~# docker images nginx
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx <none> 0f8498f13f3a 20 months ago 142MB
nginx 1.23.3 ac232364af84 20 months ago 142MB
root@LAPTOP-H2EI4I6A:~# docker images nginx:1.23.3
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.23.3 ac232364af84 20 months ago 142MB
docker image ls 还支持强大的过滤器参数 --filter,或者简写 -f。
之前我们已经看到了使用过滤器来列出虚悬镜像的用法,它还有更多的用法。
查看某个位置之前的镜像 before,查看某个位置之后的镜像 since
root@LAPTOP-H2EI4I6A:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
crpi-xp37q81m2et6ppvz.cn-hangzhou.personal.cr.aliyuncs.com/hacha/hacha v0.1 517b897a6a83 2 months ago 4.27MB
busybox latest 517b897a6a83 2 months ago 4.27MB
crpi-xp37q81m2et6ppvz.cn-hangzhou.personal.cr.aliyuncs.com/hacha/hacha latest d2c94e258dcb 19 months ago 13.3kB
nginx <none> 0f8498f13f3a 20 months ago 142MB
nginx 1.23.3 ac232364af84 20 months ago 142MB
root@LAPTOP-H2EI4I6A:~# docker image ls -f since=crpi-xp37q81m2et6ppvz.cn-hangzhou.personal.cr.aliyuncs.com/hacha/hacha
REPOSITORY TAG IMAGE ID CREATED SIZE
crpi-xp37q81m2et6ppvz.cn-hangzhou.personal.cr.aliyuncs.com/hacha/hacha v0.1 517b897a6a83 2 months ago 4.27MB
busybox latest 517b897a6a83 2 months ago 4.27MB
root@LAPTOP-H2EI4I6A:~# docker image ls -f before=crpi-xp37q81m2et6ppvz.cn-hangzhou.personal.cr.aliyuncs.com/hacha/hacha
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx <none> 0f8498f13f3a 20 months ago 142MB
nginx 1.23.3 ac232364af84 20 months ago 142MB
查看镜像详情
root@LAPTOP-H2EI4I6A:~# docker inspect busybox:latest
查看镜像分层
Docker history 可以查看分层
root@LAPTOP-H2EI4I6A:~# docker history nginx:1.23.3
IMAGE CREATED CREATED BY SIZE COMMENT
ac232364af84 20 months ago /bin/sh -c #(nop) CMD ["nginx" "-g" "daemon… 0B
<missing> 20 months ago /bin/sh -c #(nop) STOPSIGNAL SIGQUIT 0B
<missing> 20 months ago /bin/sh -c #(nop) EXPOSE 80 0B
<missing> 20 months ago /bin/sh -c #(nop) ENTRYPOINT ["/docker-entr… 0B
<missing> 20 months ago /bin/sh -c #(nop) COPY file:e57eef017a414ca7… 4.62kB
<missing> 20 months ago /bin/sh -c #(nop) COPY file:abbcbf84dc17ee44… 1.27kB
<missing> 20 months ago /bin/sh -c #(nop) COPY file:5c18272734349488… 2.12kB
<missing> 20 months ago /bin/sh -c #(nop) COPY file:7b307b62e82255f0… 1.62kB
<missing> 20 months ago /bin/sh -c set -x && addgroup --system -… 61.3MB
<missing> 20 months ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bullseye 0B
<missing> 20 months ago /bin/sh -c #(nop) ENV NJS_VERSION=0.7.9 0B
<missing> 20 months ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.23.3 0B
<missing> 20 months ago /bin/sh -c #(nop) LABEL maintainer=NGINX Do… 0B
<missing> 20 months ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 20 months ago /bin/sh -c #(nop) ADD file:60911afdacfdc216e… 80.5MB
docker image inspect 可以查看详细的分层
一些镜像下载的时候也可以看到是一层一层下载的
打标签
docker tag busybox:1.36.0 ccr.ccs.tencentyun.com/maxhou6/busyboxbymaxhou:v3.0
推送镜像
docker pull ccr.ccs.tencentyun.com/maxhou6/busyboxbymaxhou:v3.0
运行容器
root@139-159-150-152:~# docker run -it --rm ccr.ccs.tencentyun.com/maxhou6/busyboxbymaxhou:v3.0 sh
/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:08
inet addr:172.17.0.8 Bcast:172.17.255.255
Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:737 (737.0 B) TX bytes:0 (0.0 B)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
/ # hostname
619291da55ec
/ # exit
镜像删除
镜像的删除:可以根据 id 和名字删除,对于使用的镜像需要先清理容器再删除镜像
root@139-159-150-152:~# docker rmi busybox:1.36.0
Untagged: busybox:1.36.0
镜像综合实战
实战一、离线迁移镜像
- 服务器 1 上镜像保存为 tar 文件,如操作系统为 ubuntu
root@139-159-150-152:/data/myworkdir# docker save -o busybox.tar busybox
root@139-159-150-152:/data/myworkdir# ll
total 147740
drwxr-xr-x 2 root root 4096 Mar 13 16:28 ./
drwx--x--x 5 root root 4096 Mar 13 15:03 ../
-rw------- 1 root root 5101568 Mar 13 16:28 busybox.tar
-rw------- 1 root root 146172416 Mar 13 15:05 mynginx_1.22.1.tar
- scp 或者只用使用可视化工具拷贝镜像到第二台服务器上
root@139-159-150-152:/data/maxhou# scp busybox.tar root@43.138.218.166:/data/maxhou
The authenticity of host '43.138.218.166 (43.138.218.166)' can't
be established.
ECDSA key fingerprint is
SHA256:ai8KwGyM5W/iocUP8O00OMX7hriCD3ajd3wHVvdazTs.
Are you sure you want to continue connecting
(yes/no/[fingerprint])? yes
Warning: Permanently added '43.138.218.166' (ECDSA) to the list of
known hosts.
root@43.138.218.166's password:
busybox.tar
- 第二台机器从 tar 中恢复镜像
[root@VM-8-12-centos maxhou]# docker load -i busybox.tar baacf561cfff: Loading layer
[==================================================>]
5.092MB/5.092MB
Loaded image: busybox:1.36.0
可以看到busybox镜像恢复出来了
[root@VM-8-12-centos maxhou]# docker images
REPOSITORY TAG IMAGE ID CREATED
SIZE
busybox 1.36.0 7cfbbec8963d 3 weeks ago
4.86MB
rabbitmq 3.9-management ef80153df89f 2 months ago
269MB
ubuntu 1.0 955bd67a1123 3 months ago
344MB
myubuntu 1.0 1de21ccbd1a1 3 months ago
344MB
busybox latest 827365c7baf1 3 months ago
4.86MB
redis 7.0 0256c63af7db 3 months ago
- 执行 docker run 检查镜像是否可以正常运行
[root@VM-8-12-centos maxhou]# docker run -it --rm busybox:1.36.0 sh
/ # ls /
bin dev etc home lib lib64 proc root sys tmp
usr var
/ # exit
实战二、镜像存储的压缩与共享
- 拉取 nginx 镜像,如果本地没有,镜像是从仓库拉取,如果有会提示镜像已经存在,并且是最新的。
root@139-159-150-152:~# docker pull nginx:1.21.1
1.21.1: Pulling from library/nginx
Digest:
sha256:a05b0cdd4fc1be3b224ba9662ebdf98fe44c09c0c9215b45f84344c1286
7002e
Status: Image is up to date for nginx:1.21.1
docker.io/library/nginx:1.21.1
- Docker images 查看本地镜像,可以看到该镜像为 142Mb
- 从上面看到 nginx 为 1.21.1 的大小是 140 多 MB,但是我们从 docker hub 上查看可
以看到镜像是 50 多 MB,说明发生了压缩,因为仓库到本地需要走网络,所以文件越
小越好
- 把一个镜像打多个 tag,然后同时推送仓库,可以看到是说层已经存在,不会重复存储
root@139-159-150-152:/data/maxhou# docker tag nginx:1.22.1
maxhou/mybitnginx:1.22.1
root@139-159-150-152:/data/maxhou# docker tag nginx:1.22.1
maxhou/mybitnginx:1.22.2
root@139-159-150-152:/data/maxhou# docker tag nginx:1.22.1
maxhou/mybitnginx:1.22.3
root@139-159-150-152:/data/maxhou# docker tag nginx:1.22.1
maxhou/mybitnginx:1.22.4
root@139-159-150-152:/data/maxhou# docker push maxhou/mybitnginx -a
The push refers to repository [docker.io/maxhou/mybitnginx]
9543dec06aa8: Layer already exists
ccf4f419ba49: Layer already exists
21f8452ebfb1: Layer already exists
25bbf4633bb3: Layer already exists
a4f34e6fb432: Layer already exists
3af14c9a24c9: Layer already exists
1.22.1: digest:
sha256:9081064712674ffcff7b7bdf874c75bcb8e5fb933b65527026090dacda3
6ea8b size: 1570
9543dec06aa8: Layer already exists
ccf4f419ba49: Layer already exists
21f8452ebfb1: Layer already exists
25bbf4633bb3: Layer already exists
a4f34e6fb432: Layer already exists
3af14c9a24c9: Layer already exists
1.22.2: digest:
sha256:9081064712674ffcff7b7bdf874c75bcb8e5fb933b65527026090dacda3
6ea8b size: 1570
9543dec06aa8: Layer already exists
ccf4f419ba49: Layer already exists
21f8452ebfb1: Layer already exists
25bbf4633bb3: Layer already exists
a4f34e6fb432: Layer already exists
3af14c9a24c9: Layer already exists
- 添加一个新的镜像到我们的仓库的时候,如果 docker hub 发现已经有了是 mount的,不是从本地推上去的就会直接挂载
root@139-159-150-152:/data/maxhou# docker tag nginx:1.16.1
maxhou/mybitnginx:1.16.1
root@139-159-150-152:/data/maxhou# docker push
maxhou/mybitnginx:1.16.1
The push refers to repository [docker.io/maxhou/mybitnginx]
c23548ea0b99: Mounted from library/nginx
82068c842707: Mounted from library/nginx
c2adabaecedb: Mounted from library/nginx
1.16.1: digest:
sha256:2963fc49cc50883ba9af25f977a9997ff9af06b45c12d968b7985dc1e92
54e4b size: 948
你尝试将标记后的镜像推送到私有仓库(在这个例子中是docker.io/maxhou/mybitnginx)时,Docker Hub 会检查这个镜像的各个层。
如果发现这些层和公共仓库(library/nginx)中的镜像层是相同的,就会直接挂载(引用)这些已经存在于公共仓库的层,而不是再次传输相同的数据来重新构建这些层。这样可以显著提高效率,避免重复的网络传输和存储开销。