看啥

导航

四、docker 镜像(二)

  • 常用镜像仓库
  官方仓库:hub.docker.com
  自己的私有仓库:Harbor
  阿里云私有仓库:registry.cn-hangzhou.aliyuncs.com
  • 登录镜像仓库
# 格式
  docker login
  注: 默认情况下,docker login登录的是官方仓库,如果登录其他镜像仓库则需要指定镜像仓库的URL连接。
# 镜像仓库申请地址:
https://cr.console.aliyun.com/cn-shanghai/instances/repositories
# 实例
[root@Centos7 ~]# docker login registry.cn-hangzhou.aliyuncs.com
          Username: xxxxxxx
          Password:
          WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
          Configure a credential helper to remove this warning. See
          https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  
          Login Succeeded
#阿里云配置说明地址
https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

#配置镜像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://ojn6v8ag.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
  # 参数
  --username|-u : 指定用户名
  --password|-p : 指定密码

image

  • 镜像上传
# 格式
  	docker push [镜像标签]
# 注:要想上传镜像,首先得登录镜像仓库,其次设置对应镜像仓库的tag
[root@Centos7 ~]# docker push registry.cn-hangzhou.aliyuncs.com/alvinos/redis:v2
  The push refers to repository [registry.cn-hangzhou.aliyuncs.com/alvinos/redis]
  3480f9cdd491: Pushed 
  a24a292d0184: Pushed 
  f927192cc30c: Pushed 
  1450b8f0019c: Pushed 
  8e14cb7841fa: Pushed 
  cb42413394c4: Pushed 
  v2: digest: sha256:7ef832c720188ac7898dbd8d1e237b0738e94f94fc7e981cb7b8efe84555e892 size: 1572
  • 删除和清理镜像
    在 docker 中,删除镜像主要使用 rmi 子命令,清理镜像主要使用 prune 子命令。
# 格式
  	docker rmi [镜像名称或者镜像ID]
# 实例
[root@Centos7 ~]# docker rmi nginx
# 参数
  	-f  : 强制删除
[root@Centos7 ~]# docker rmi -f nginx
      Untagged: nginx:latest
      Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
# 注:当有容器正在使用镜像时,强制删除镜像,只能删除镜像的所有tag, 不会删除镜像。
  • 清空镜像
    使用一段时间之后,docker 会产生很多临时文件,以及一些没有被使用的镜像,我们可以通过 docker image prune 命令来进行清理。
  # 格式
  	docker image prune
# 实例
[root@Centos7 ~]# docker image prune
    WARNING! This will remove all dangling images.
    Are you sure you want to continue? [y/N] y
    Total reclaimed space: 0B
# 参数
   -a :删除所有无用的镜像,不光是临时镜像
[root@Centos7 ~]# docker image prune -a
WARNING! This will remove all images without at least one container associated to them.
 Are you sure you want to continue? [y/N] y
 Deleted Images:
 untagged: redis:latest
 untagged: redis@sha256:0f97c1c9daf5b69b93390ccbe8d3e2971617ec4801fd0882c72bf7cad3a13494
  untagged: registry.cn-hangzhou.aliyuncs.com/alvinos/redis:v2
  untagged: registry.cn-hangzhou.aliyuncs.com/alvinos/redis@sha256:7ef832c720188ac7898dbd8d1e237b0738e94f94fc7e981cb7b8efe84555e892
  deleted: sha256:621ceef7494adfcbe0e523593639f6625795cc0dc91a750629367a8c7b3ccebb
  deleted: sha256:de66cfbf4712b8ba9ef292e08ef7487be26d9d21b350548e400ae351405d820e
  deleted: sha256:79b2381e35429e8fc04d31b3445f069c22d288bf5c4cba7b7c10004ff78ae201
  deleted: sha256:1d047d19be363b00139990d4d7f392dabdb0809dbc9d0fbe67c1f15b8caed27a
  deleted: sha256:8c41f4e708c37059df28ae1cabc200a6db2fee45bd3a2cadcf70f2765bb68730
  deleted: sha256:b51317bef36fe1900be48402c8a41fcd9cdb6b8950c10209f764473cb8323371
  Total reclaimed space: 35.04MB
#-f :强制删除镜像,而不进行提示。
docker image prune -a -f
  • 查看镜像历史(镜像的构建历史)

    # 格式
    	docker history [镜像ID或镜像名称]
    # 实例
    [root@Centos7 ~]# docker history alpine
    IMAGE          CREATED        CREATED BY                                      SIZE      COMMENT
    7731472c3f2a   2 months ago   /bin/sh -c #(nop)  CMD ["/bin/sh"]              0B        
    <missing>      2 months ago   /bin/sh -c #(nop) ADD file:edbe213ae0c825a5b…   5.61MB    
    

注意:有些构建信息过长,可以使用--no-trunc 选项来输出完整信息。

posted on 2021-08-17 16:57  看啥  阅读(74)  评论(0编辑  收藏  举报