0
点赞
收藏
分享

微信扫一扫

3、Docker镜像管理(下载、删除、打标签、导出导入)

镜像管理
docker镜像是由一层一层的文件系统累积起来的

容器的组成
最下面:bootfs,引导文件系统,在镜像中不存在,用的是宿主机的内核  
然后是:rootfs,根文件系统,如centos/ubuntu
接着:镜像(jdk)
接着:镜像(tomcat)
最后:可写容器(container)
除了可写层,其他都是只读的。

这种情况可以复用,如根文件系统,多个镜像,根文件系统是一样的,可以节约空间。

docker存储引擎

Docker官方推荐首选存储引擎为overlay

镜像查询

搜索镜像:带有OK字样的是官方,最好从官方下载
[root@ubuntu2004 ~]#docker search nginx
NAME       DESCRIPTION                 STARS     OFFICIAL   AUTOMATED
nginx     Official build of Nginx.      17448     [OK]

镜像下载

[root@ubuntu2004 ~]#docker pull ubuntu  不写版本,就是最新版
ubuntu系统建议安装的基础包

# apt update                       #安装软件前需要先更新索引
# apt install procps               #提供top, ps, free等命令
# apt install psmisc               #提供pstree, killall等命令
# apt install iputils-ping         #提供ping命令
# apt install net-tools            #提供netstat网络工具等
# apt install iproute2             #提供ip,ss网络工具等

删除镜像(镜像不能使用)

docker rm删除容器
docker rm删除镜像(模板)
跟名字删除标签,跟ID删除镜像

删除所有镜像
docker rmi `docker images -q`
docker rmi -f 强制删除 (除非占用镜像的容器全部删除,才能释放镜像所占用空间)
du -sh /var/libvirt/images/overlay2/

清理无名称镜像以及不再使用的镜像,建议定期清理(无名称镜像dang ling)
docker image prune (默认之清理dang ling的镜像)
docker image prune -f(加-f为强行清理)
docker image prune -a(加-a为不再使用的)
全部删除
docker system prune -f -a

镜像打标签,类似于起别名

标识镜像(给镜像打标签)
[root@ubuntu2004 ~]#docker tag nginx:latest mefbginx
[root@ubuntu2004 ~]#docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
mefbginx      latest    605c77e624dd   9 months ago    141MB
nginx         latest    605c77e624dd   9 months ago    141MB
alpine        latest    c059bfaa849c   10 months ago   5.59MB
ubuntu        latest    ba6acccedd29   12 months ago   72.8MB
hello-world   latest    feb5d9fea6a5   12 months ago   13.3kB

上传镜像规范,才可以上传到指定的仓库
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
仓库主机FQDN或IP[:端口]/项目名(或用户名)/images名字:版本

镜像导出(此种方式适用于临时用,建议建个内网镜像仓库)

导出单个镜像

导出镜像文件(可REPOSITORY:TAG或者IMAGE ID)
[root@ubuntu2004 ~]#docker save nginx:latest -o nginx.tar
[root@ubuntu2004 ~]#ll nginx.tar 
-rw------- 1 root root 145905152 10月 16 21:20 nginx.tar
对镜像文件进行压缩
[root@ubuntu2004 ~]#gzip nginx.tar
[root@ubuntu2004 ~]#ll -h nginx.tar.gz 
-rw------- 1 root root 53M 10月 16 21:20 nginx.tar.gz
把压缩后的镜像文件传到需要的主机
[root@ubuntu2004 ~]#scp nginx.tar.gz 10.0.0.8:
root@10.0.0.8's password: 
nginx.tar.gz 
把压缩镜像包生成镜像(自动解压缩)
[root@rocky8 ~]#docker load -i nginx.tar.gz
2edcec3590a4: Loading layer [==================================================>]  83.86MB/83.86MB
e379e8aedd4d: Loading layer [==================================================>]     62MB/62MB
b8d6e692a25e: Loading layer [==================================================>]  3.072kB/3.072kB
f1db227348d0: Loading layer [==================================================>]  4.096kB/4.096kB
32ce5f6a5106: Loading layer [==================================================>]  3.584kB/3.584kB
d874fd2bc83b: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest
[root@rocky8 ~]#docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
nginx        latest    605c77e624dd   9 months ago   141MB

导出多个镜像(可用ID但有缺陷,会丢失镜像名字)
[root@ubuntu2004 ~]#docker save 605c77e624dd c059bfaa849c -o nginx-alpine.tar
对镜像文件进行压缩
[root@ubuntu2004 ~]#gzip nginx-alpine.tar
把压缩后的镜像文件传到需要的主机
[root@ubuntu2004 ~]#scp nginx-alpine.tar.gz 10.0.0.8:
把压缩镜像包生成镜像(自动解压缩)
[root@rocky8 ~]#docker load -i nginx-alpine.tar.gz


导出所有镜像 写shell脚本
[root@ubuntu2004 ~]#docker save `docker images |awk 'NR>1{print $1":"$2}' |tr '\n' ' '` -o all.tar
对镜像文件进行压缩
[root@ubuntu2004 ~]#gzip all.tar
把压缩后的镜像文件传到需要的主机
[root@ubuntu2004 ~]#scp all.tar.gz 10.0.0.8:
把压缩镜像包生成镜像(自动解压缩)
[root@rocky8 ~]#docker load -i all.tar.gz

把全部镜像导出并生成单独文件
[root@ubuntu2004 ~]#for i in `docker image ls --format "{{.Repository}}:{{.Tag}}"`;do docker save $i -o `echo $i|cut -d: -f1`.tar ;done

把一个机器上的镜像全部打包到另一台电脑上

上面那个方法不专业,以下方法更专业
[root@ubuntu2004 ~]#docker image save `docker image ls --format "{{.Repository}}:{{.Tag}}"` -o all2.tar
对镜像文件进行压缩
[root@ubuntu2004 ~]#gzip all2.tar
把压缩后的镜像文件传到需要的主机
[root@ubuntu2004 ~]#scp all2.tar.gz 10.0.0.8:
把压缩镜像包生成镜像(自动解压缩)
[root@rocky8 ~]#docker load -i all2.tar.gz


举报

相关推荐

0 条评论