本篇内容介绍了“Docker如何管理和删除本地仓库中的镜像”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为大荔企业提供专业的网站设计、成都网站建设,大荔网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
一:docker私有仓库安装
1、 下载镜像是有镜像仓库:
[root@localhost ~]# systemctl start docker #如果已经有镜像了,强制删除原来的镜像的方式如下: [root@xxx-pub /]# docker rmi -f docker.io/registry untagged: docker.io/registry:latest untagged: docker.io/registry@sha256:51bb55f23ef7e25ac9b8313b139a8dd45baa832943c8ad8f7da2ddad6355b3c8 [root@xxx-pub /]# #开始下载最新的镜像。 [root@localhost ~]# docker pull registry using default tag: latest latest: pulling from library/registry 4064ffdc82fe: pull complete c12c92d1c5a2: pull complete 4fbc9b6835cc: pull complete 765973b0f65f: pull complete 3968771a7c3a: pull complete digest: sha256:20bbbc0f6384cf7dc6e292ccbe75935b73c92ec776543c970904bc60feceb129 status: downloaded newer image for registry:latest [root@localhost ~]#
2、 启动并且挂载镜像仓库到本地磁盘:
[root@xxx-pub /]# docker run -d -v /registry:/home/docker-registry -p 5000:5000 --restart=always --privileged=true --name registry registry:latest unable to find image 'registry:latest' locally trying to pull repository docker.io/library/registry ... latest: pulling from docker.io/library/registry digest: sha256:51bb55f23ef7e25ac9b8313b139a8dd45baa832943c8ad8f7da2ddad6355b3c8 status: downloaded newer image for docker.io/registry:latest b7bd2b14ed488936afe798be95f3cd56f604fb092d45cf6f4a58359bcad0d34c [root@xxx-pub /]#
-v /registry:/home/docker-registry:默认情况下,会将仓库存放于容器内的/home/docker-registry目录下,指定本地目录挂载到容器。
-p 5000:5000 :端口映射。即本地5000端口,映射到registry中的5000端口。
–restart=always1:在容器退出时总是重启容器,主要应用在生产环境。
–privileged=true:在centos7中的安全模块selinux把权限禁掉了,参数给容器加特权,不加上传镜像会报类似权限错误。oserror: [errno 13] permission denied: ‘/tmp/registry/repositories/liibrary')或者(received unexpected http status: 500 internal server error)
–name registry:指定容器的名称。
为了持久化数据,将volume挂载到/home/docker-registry
3、 我们给一个本地镜像打个标签然后上传:
查看本地有哪些镜像:
[root@xxx-pub /]# docker images repository tag image id created size docker.io/centos centos7.5.1804 fdf13fa91c6e 4 weeks ago 200 mb docker.io/registry latest b2b03e9146e1 2 months ago 33.3 mb [root@xxx-pub /]#
以docker.io/centos为案例。
[root@xxx-pub /]# docker tag fdf13fa91c6e localhost:5000/xxx-centos7.5.1804:1.0 ##localhost:5000表示仓库地址,fdf13fa91c6e表示images id,xxx-centos7.5.1804表示镜像名称,1.0表示版本号。 或: [root@xxx-pub /]# docker tag docker.io/centos:centos7.5.1804 localhost:5000/xxx-centos7.5.1804:1.0
4、 直接上传:
[root@xxx-pub /]# docker push localhost:5000/xxx-centos7.5.1804:1.0 the push refers to a repository [localhost:5000/xxx-centos7.5.1804] bcc97fbfc9e1: pushed 1.0: digest: sha256:7c14180942615fef85cb5c8b1388e028be1a8f79694a5fa30a4025173e42ad61 size: 529 [root@xxx-pub /]#
在push的过程中可能报类似如下的错误(以下是网络上的错误提示):
[root@localhost ~]# docker push 192.168.174.128:5000/hello:latest the push refers to a repository [192.168.174.128:5000/hello] unable to ping registry endpoint https://192.168.174.128:5000/v0/ v2 ping attempt failed with error: get https://192.168.174.128:5000/v2/: http: server gave http response to https client v1 ping attempt failed with error: get https://192.168.174.128:5000/v1/_ping: http: server gave http response to https client [root@localhost ~]#
解决办法:
使用的是https,修改/etc/sysconfig/docker(这里是centos7下的docker)文件,加上add_registry='–add-registry 192.168.18.162:5000',insecure_registry='–insecure-registry 192.168.18.162:5000' (上面是历史的docker版本的配置),如下图:
但是在新的docker容器仓库的配置中,自己的docker容器中的配置采用(这里在docker registry和另外pull镜像的机器上都指定如下的配置):
指定完成之后,要重启docker,重启的配置如下:
[root@youx-pub volumes]# systemctl restart docker
另外:查看一下docker.service,查看一下配置文件:
[root@youx-pub registry]find / -name docker.service /sys/fs/cgroup/memory/system.slice/docker.service /sys/fs/cgroup/devices/system.slice/docker.service /sys/fs/cgroup/blkio/system.slice/docker.service /sys/fs/cgroup/cpu,cpuacct/system.slice/docker.service /sys/fs/cgroup/pids/system.slice/docker.service /sys/fs/cgroup/systemd/system.slice/docker.service /usr/lib/systemd/system/docker.service [root@youx-pub registry]# vim /usr/lib/systemd/system/docker.service
然后执行一下:
#因为可以用于systemctl启动的docker.service文件变化了,所以得执行以下配置。然后执行docker的重启工作 systemctl daemon-reload systemctl restart docker
如果还是报如下错误:
[root@youx-pub registry]# docker push 192.168.18.162:5000/nginx:1.2 the push refers to a repository [192.168.18.162:5000/nginx] get https://192.168.18.162:5000/v1/_ping: http: server gave http response to https client [root@youx-pub registry]#
解决办法是:
在/etc/docker下创建daemon.json,内容是:
{"insecure-registries":["192.168.18.162:5000"]}
然后再重启docker容器:
[root@youx-pub volumes]# systemctl restart docker
5、 上传完成,我们可以查看一下挂载目录是否有镜像:
[root@xxx-pub docker-registry]# pwd /home/docker-registry [root@xxx-pub docker-registry] # ls /registry/ docker
6、 关于本地镜像的查看:
[root@xxx-pub docker-registry]# curl http://192.168.18.162:5000/v2/_catalog {"repositories":["xxx-centos7.5.1804","xxx-centos7.5.1804-v1.0"]} [root@xxx-pub docker-registry]#
7、 看到有两个镜像,我们要获取他的标签信息以便下载呢:
[root@xxx-pub docker-registry]# curl http://192.168.18.162:5000/v2/xxx-centos7.5.1804/tags/list {"name":"xxx-centos7.5.1804","tags":["1.0"]} [root@xxx-pub docker-registry]#
8、 然后我们直接下载镜像(到另电脑上):
[root@bigdata2 ~]# docker pull 192.168.18.162:5000/youx-centos7.5.1804:1.0 using default tag: latest trying to pull repository 192.168.18.162/youx-centos7.5.1804 ... get https://192.168.18.162/v1/_ping: dial tcp 192.168.18.162:443: getsockopt: no route to host [root@bigdata2 ~]#
如果出现上面的情况,解决办法是:
编译:
[root@youx-pub volumes]# vim /etc/containers/registries.conf 这个文件。 旧的是编辑:/etc/sysconfig/docker 这个文件
然后重启docker
[root@youx-pub volumes]# systemctl restart docker
然后发现可以pull了
#要注意的是,这里的地址是:仓库地址:仓库端口号/repository:tag [root@bigdata2 ~]# docker pull 192.168.18.162:5000/youx-centos7.5.1804:1.0
如果还不行,需要修改/etc/docker/daemon.json,内容如下:
[root@bigdata2 docker]# cat daemon.json {"insecure-registries":["192.168.18.162:5000"]}
二、从另外一台机器上上传镜像到docker镜像仓库:
[root@bigdata2 ~]# docker tag centos7-jdk8-nginx:1.0 192.168.18.162:5000/centos7-jdk8-nginx:1.0 [root@bigdata2 ~]# docker images repository tag image id created size 192.168.18.162:5000/centos7-jdk8-nginx 1.0 bcacd65e2a2e 2 minutes ago 2.18 gb [root@bigdata2 ~]# docker push 192.168.18.162:5000/centos7-jdk8-nginx:1.0 the push refers to a repository [192.168.18.162:5000/centos7-jdk8-nginx] f018e9c38a66: pushed 2a47dcd341ef: pushed c5dea3bc729a: pushed bcc97fbfc9e1: pushed 1.0: digest: sha256:d907ff2f8eb590209700c01ce85c78d0bc778a4238539d747e4667d68cb52102 size: 1163 [root@bigdata2 ~]#
然后进入docker镜像仓库所在位置:
[root@youx-pub registry]# curl http://192.168.18.162:5000/v2/_catalog {"repositories":["centos7-jdk8-nginx","nginx"]} [root@youx-pub registry]# curl http://192.168.18.162:5000/v2/centos7-jdk8-nginx/tags/list {"name":"centos7-jdk8-nginx","tags":["1.0"]} [root@youx-pub registry]#
三、本地镜像如何删除,正常是不提供删除功能的,所以使用第三方的插件来进行删除
插件github上的位置:
1、下载资源
[root@master registry]# curl https://raw.githubusercontent.com/burnettk/delete-docker-registry-image/master/delete_docker_registry_image.py | sudo tee /usr/local/bin/delete_docker_registry_image >/dev/null sudo chmod a+x /usr/local/bin/delete_docker_registry_image
2、 设置相关环节变量:
查找registry_data_dir,命令: [root@youx-pub registry]# find / -name registry [root@youx-pub registry]# find / -name registry /var/lib/docker/overlay2/d72320cd67b42f7ae66342cc6dedab7abe5e89106de8c4919ec8c5a6e5940b09/diff/var/lib/ucf/registry /var/lib/docker/overlay2/92211417089f7be8239def550e1e89ce3f0e50ac57f2c36ba723ca312ea06ae3/diff/bin/registry /var/lib/docker/overlay2/c1716aea0b380eb94ead9aa02552769acd4c3dac8e6dab735997f1709ce79a33/diff/etc/docker/registry /var/lib/docker/overlay2/46f719f01255c431b4343e78607341d6d66ea482bc6d03c3b4e05e7413f46be6/diff/var/lib/registry /var/lib/docker/overlay2/46f719f01255c431b4343e78607341d6d66ea482bc6d03c3b4e05e7413f46be6/merged/bin/registry /var/lib/docker/overlay2/46f719f01255c431b4343e78607341d6d66ea482bc6d03c3b4e05e7413f46be6/merged/etc/docker/registry /var/lib/docker/overlay2/46f719f01255c431b4343e78607341d6d66ea482bc6d03c3b4e05e7413f46be6/merged/var/lib/registry /var/lib/docker/volumes/1522d54c954c755250cb71686cacd9714cd3d0f0c706ec18c1e490c7881fe713/_data/docker/registry /opt/data/registry /registry [root@youx-pub registry]# 发现 [root@youx-pub repositories]# pwd /var/lib/docker/volumes/1522d54c954c755250cb71686cacd9714cd3d0f0c706ec18c1e490c7881fe713/_data/docker/registry/v2/repositories [root@youx-pub repositories]# ls centos7-jdk8-nginx nginx [root@youx-pub repositories]# 所以设置镜像仓库的位置为: [root@master registry]# export registry_data_dir=/var/lib/docker/volumes/1522d54c954c755250cb71686cacd9714cd3d0f0c706ec18c1e490c7881fe713/_data/docker/registry/v2
地址就是咱们挂载地址:
看一下相关标签:
[root@youx-pub registry]# curl http://192.168.18.162:5000/v2/_catalog {"repositories":["youx-centos7.5.1804","youx-centos7.5.1804-v1.0"]} [root@youx-pub registry]# curl http://192.168.18.162:5000/v2/youx-centos7.5.1804-v1.0/tags/list {"name":"youx-centos7.5.1804-v1.0","tags":["latest"]} [root@youx-pub registry]#
可以之后我们直接删除:
[root@master registry]# delete_docker_registry_image --image youx-centos7.5.1804-v1.0:latest
咱们再看一下本地仓库的镜像有几个:
[root@master registry]# curl http://192.168.18.162:5000/v2/_catalog {"repositories":["youx-centos7.5.1804"]}
“Docker如何管理和删除本地仓库中的镜像”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
本文名称:Docker如何管理和删除本地仓库中的镜像
本文来源:https://www.cdcxhl.com/article0/jhseoo.html
成都网站建设公司_创新互联,为您提供用户体验、小程序开发、网站排名、微信公众号、商城网站、网页设计公司
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联