Docker的镜像基础管理
基础镜像拉取
docker pull centos
docker pull centos:6.3
docker pull centos:7.0
docker pull nginx
镜像的基础查看
查看下载的所有镜像
docker imager ls
查看下载的所有进行的ID号码
docker imager ls -p
查看镜像的详细信息
docker image inspect 镜像名
查看docker所有模块占用磁盘的空间
docker system df
查看指定镜像的所在位置下面的镜像
docker image ls -f before=nginx:1.15
[root@hwf ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.16 dfcfd8e9a5d3 13 days ago 127MB
nginx 1.17 602e111c06b6 13 days ago 127MB
nginx 1.15 53f3fd8007f7 12 months ago 109MB
[root@hwf ~]# docker image ls -f before=nginx:1.16
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.17 602e111c06b6 13 days ago 127MB
nginx 1.15 53f3fd8007f7 12 months ago 109MB
[root@hwf ~]# docker image ls -f before=nginx:1.17
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.15 53f3fd8007f7 12 months ago 109MB
查看指定镜像的所在位置上面的镜像
docker image ls -f since=hello-world:latest
[root@hwf ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.16 dfcfd8e9a5d3 13 days ago 127MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB
[root@hwf ~]# docker image ls -f since=hello-world:latest
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.16 dfcfd8e9a5d3 13 days ago 127MB
镜像的导入和导出
复制导入镜像到指定位置
docker imger save nginx >/opt/nginx.tar.gz
docker imager save 43458646b2 >/tmp/ubu.tar.gz [这里使用镜像名:版本,不要使用ID,不然为none]
导入镜像
docker image load -i /tmp/ubbu.tar.gz
shell 批量导出镜像包
# 批量导出
for x in $(docker images --format "{{.Repository}}:{{.Tag}}");do docker save ${x} > devops-images/${x##*/}.tgz ;done;
for x in $(docker images --format "{{.Repository}}:{{.Tag}}" | grep c7n/hzero-);do docker save ${x} > images/${x##*/}.tar ;done;
#批量删除
for x in $(docker images --format "{{.Repository}}:{{.Tag}}"|grep goharbor/);do docker image rm -f ${x} ;done;
举例
for x in $(docker images --format "{{.Repository}}:{{.Tag}}" | grep registry.c7n.gzinfo);do echo ${x##*/} ;done;
导出猪齿鱼官方镜像
for x in $(docker images --format "{{.Repository}}:{{.Tag}}" | grep registry.cn-shanghai.aliyuncs.com/c7n);do \ if [ ! -f "/data/images/${x##*/}.tar" ];then \ docker save ${x} > /data/images/${x##*/}.tar ; \ fi \ done;
##*/ - 最右边"/"开始 左边删除,保留右边 #*/ - 最左边"/"开始 左边删除,保留右边 %%/* - 最左边"/"开始 右边删除,保留左边 %/* - 最右边"/"开始 右边删除,保留左边
批量导入镜像包
for x in $(ls *.tar);do docker load -i ${x};done
镜像的删除(如果容器使用了,先删除容器在删除镜像)
删除镜像
docker image rm -f 43458646b2
删除全部镜像
docker image rm -f `docker image ls -q`
虚悬镜像
这类无标签镜像也被称为 虚悬镜像(dangling image) ,
上面的镜像列表中,还可以看到一个特殊的镜像,这个镜像既没有仓库名,也没有标签,均为 <none>
可以用下面的命令专门显示这类镜像:
查看虚悬镜像
[root@hwf ~]# docker image ls -f dangling=true
REPOSITORY TAG IMAGE ID CREATED SIZE
删除虚悬镜像
docker image prune
[root@hwf ~]# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
镜像添加标签tag
给没有标签的镜像打上标签
docker image tag 镜像ID号 xxx:xxx
[root@hwf ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.16 dfcfd8e9a5d3 13 days ago 127MB nginx latest 602e111c06b6 13 days ago 127MB hello-world latest bf756fb1ae65 4 months ago 13.3kB [root@hwf ~]# docker image tag 602e111c06b6 xxx:1.1 [root@hwf ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.16 dfcfd8e9a5d3 13 days ago 127MB nginx latest 602e111c06b6 13 days ago 127MB xxx 1.1 602e111c06b6 13 days ago 127MB hello-world latest bf756fb1ae65 4 months ago 13.3kB
如果要保留这个标签,可以将原先的标签名称删除

评论