2021Docker容器技术全解-docker搭建私有仓库(7)
1、在服务器m01上拉取仓库镜像registry。
[root@m01 ~]#docker pull registry
2、在服务器m01上运行docker私有仓库镜像,创建容器my-registry。
[root@m01 ~]#docker run -d -p 5000:5000 --restart=always --name my-registry \ -v /registry:/registry \ -e REGISTRY_STORAGE_DELETE_ENABLED=true \ -e REGISTRY_HTTP_ADDR=0.0.0.0:5000 \ registry:latest 05d5fb3d5e90446703022e86ea80aca762693b730764a3e755af4e210473d405 [root@m01 ~]#
说明 -d,后台运行容器 -p 5000:5000 ,映射容器5000端口至宿主机5000端口。
restart always,设置重新启动策略,在docker重新启动时自动重新启动容器my-registry。
-name my-registry,给容器命名。 -v /registry:/registry,把docker容器中/registry目录的数据加载到宿主机的/registry目录,宿主机的/registry目录如果不存在会自动创建。目的是为了防止docker私有仓库这个容器被删除时,仓库里的镜像也会被删除。宿主机查看到的私有仓库镜像就在这个目录中。 -e REGISTRY_STORAGE_DELETE_ENABLED,设置是否允许删除仓库存储的镜像。 -e REGISTRY_HTTP_ADDR=0.0.0.0:5000,设置镜像仓库地址。
3、查看私有仓库镜像,暂时没有镜像。
[root@m01 ~]#yum install -y tree [root@m01 ~]#tree /registry/ /registry/ 0 directories, 0 files [root@m01 ~]# [root@m01 ~]#curl http://10.0.0.61:5000/v2/_catalog {"repositories":[]}
三,docker客户端m01测试上传下载私有仓库镜像
#下面的这个镜像是使用Dockerfile打包Nginx+php镜像 [root@web01 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE mycentos8 nginx c757fb615c03 11 hours ago 383MB #给镜像打新标签(当标签的第一部分是主机名和端口时,上传镜像时docker会将其解释为镜像仓库的位置) [root@web01 ~]#docker tag mycentos8:nginx 10.0.0.61:5000/client-nginx-php [root@web01 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE 10.0.0.61:5000/client-nginx-php latest c757fb615c03 11 hours ago 383MB mycentos8 nginx c757fb615c03 11 hours ago 383MB #把打标签后的镜像推送到私有仓库
3docker客户端wb01上传创建新标签的镜像至私有仓库服务器m01。
[root@web01 ~]#docker push 10.0.0.61:5000/client-nginx-php Using default tag: latest The push refers to repository [10.0.0.61:5000/client-nginx-php] Get "https://10.0.0.61:5000/v2/": http: server gave HTTP response to HTTPS client [root@web01 ~]# 使用默认标记:最新 推送是指存储库[10.0.0.61:5000/客户端nginx-php] 得到“https://10.0.0.61:5000/v2/“:http:server向HTTPS客户端提供了http响应
4解决报错
在客户端配置私有仓库ip和端口并重启docker
[root@web01 ~]#cat /etc/docker/daemon.json { "insecure-registries": ["10.0.0.61:5000"] } [root@web01 ~]#systemctl restart docker
5再次执行上传(成功):
[root@web01 ~]#docker push 10.0.0.61:5000/client-nginx-php Using default tag: latest The push refers to repository [10.0.0.61:5000/client-nginx-php] 3ccd68eee53e: Pushed 0b386fa14218: Pushed 59674775b440: Pushed b6f8cb70f36a: Pushed 74ddd0ec08fa: Pushed latest: digest: sha256:3778f9d89b90871eecf64cbf82e74ec639e235f6a0709eee6e6682465bb36acd size: 1371 [root@web01 ~]#
6查看镜像仓库:
[root@web01 ~]#curl http://10.0.0.61:5000/v2/_catalog {"repositories":["client-nginx-php"]} [root@web01 ~]#
7打开web02机器,配置镜像仓库ip和端口
[root@web02 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 5d0da3dc9764 3 months ago 231MB [root@web02 ~]#vim /etc/docker/daemon.json [root@web02 ~]# [root@web02 ~]#cat /etc/docker/daemon.json { "insecure-registries": ["10.0.0.61:5000"] } [root@web02 ~]#systemctl restart docker [root@web02 ~]#
8在web02中下载m01镜像仓里面的镜像:
[root@web02 ~]#docker pull 10.0.0.61:5000/client-nginx-php Using default tag: latest latest: Pulling from client-nginx-php a1d0c7532777: Pull complete 905284dea9d2: Pull complete ac5b4cb41214: Pull complete bdcee17e4cff: Pull complete 067c9ab657be: Pull complete Digest: sha256:3778f9d89b90871eecf64cbf82e74ec639e235f6a0709eee6e6682465bb36acd Status: Downloaded newer image for 10.0.0.61:5000/client-nginx-php:latest 10.0.0.61:5000/client-nginx-php:latest [root@web02 ~]#docker images REPOSITORY TAG IMAGE ID CREATED SIZE 10.0.0.61:5000/client-nginx-php latest c757fb615c03 4 hours ago 383MB centos latest 5d0da3dc9764 3 months ago 231MB [root@web02 ~]#
四,使用web界面浏览删除docker私有仓库镜像
1、私有仓库服务器m01下载web界面管理工具docker-registry-web镜像。
[root@m01 ~]#docker pull hyper/docker-registry-web
2、设置私有仓库只读参数为false,创建启动容器registry-web。
docker run -itd -p 8080:8080 --restart=always --name registry_web --link my-registry \ -e REGISTRY_URL=http://10.0.0.61:5000/v2 \ -e REGISTRY_NAME=10.0.0.61:5000 \ -e REGISTRY_READONLY=false \ hyper/docker-registry-web:latest
3如果有防火墙请开放8080端口:
firewall-cmd --zone=public --add-port=8080/tcp --permanent firewall-cmd --reload firewall-cmd --zone=public --query-port=8080/tcp
4、docker私有仓库服务器200,编辑配置文件/etc/docker/daemon.json,增加内容“私有仓库IP:端口号”,保存配置文件,重启docker服务。
[root@m01 ~]#vi /etc/docker/daemon.json [root@m01 ~]#cat /etc/docker/daemon.json { "registry-mirrors": [ "http://hub-mirror.c.163.com" ], "insecure-registries": ["10.0.0.61:5000"] } [root@m01 ~]#systemctl restart docker [root@m01 ~]#