Docker搭建私有仓库(三)
转载自https://blog.youkuaiyun.com/qq_32523587/article/details/82879015
一、环境
准备两台安装好docker的服务器:
服务端机器 (主机1名为docker私有仓库):docker私有仓库服务器,运行registry容器;
测试端机器 (主机2名为docker):普通的docker服务器,在这台服务器上下载一个测试镜像busybox,然后上传到registry服务器进行测试
二、主机1配置镜像仓库
-
下载镜像
[root@localhost ~]# docker pull registry Using default tag: latest latest: Pulling from library/registry c87736221ed0: Pull complete 1cc8e0bb44df: Pull complete 54d33bcb37f5: Pull complete e8afc091c171: Pull complete b4541f6d3db6: Pull complete Digest: sha256:8004747f1e8cd820a148fb7499d71a76d45ff66bac6a29129bfdbfdc0154d146 Status: Downloaded newer image for registry:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE registry latest f32a97de94e1 3 months ago 25.8MB
-
运行registry容器
在/opt/目录下创建registry目录
[root@localhost ~]# docker run -itd -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name test_registry registry:latest 014bc49e8e1e6a8f6e489ed4e80b3d16b94c320a4f75e449bda8651516ac144f [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 014bc49e8e1e registry:latest "/entrypoint.sh /etc…" 7 seconds ago Up 5 seconds 0.0.0.0:5000->5000/tcp test_registry
参数说明
-itd:在容器中打开一个伪终端进行交互操作,并在后台运行;
-v:把宿主机的/data/registry目录绑定 到 容器/var/lib/registry目录(这个目录是registry容器中存放镜像文件的目录),来实现数据的持久化;
-p:映射端口;访问宿主机的5000端口就访问到registry容器的服务了;
–restart=always:这是重启的策略,假如这个容器异常退出会自动重启容器;
–name test_registry:创建容器命名为test_registry,你可以随便命名;
registry:latest:这个是刚才pull下来的镜像 -
查看仓库中所有镜像
[root@localhost ~]# curl http://127.0.0.1:5000/v2/_catalog {"repositories":[]}
现在是空的,因为才刚运行,里面没有任何镜像内容。
三、使用主机2测试镜像仓库
-
下载busybox镜像
[root@localhost ~]# docker pull busybox Using default tag: latest latest: Pulling from library/busybox 8e674ad76dce: Pull complete Digest: sha256:c94cf1b87ccb80f2e6414ef913c748b105060debda482058d2b8d0fce39f11b9 Status: Downloaded newer image for busybox:latest [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE busybox latest e4db68de4ff2 2 weeks ago 1.22MB
-
为镜像打标签
[root@localhost ~]# docker tag busybox:latest 192.168.1.40:5000/busybox:v1 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.1.40:5000/busybox v1 e4db68de4ff2 2 weeks ago 1.22MB busybox latest e4db68de4ff2 2 weeks ago 1.22MB
格式说明:Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
busybox:lastest 这是源镜像,也是刚才pull下来的镜像文件;
192.168.1.40:5000/busybox:v1:这是目标镜像,也是registry私有镜像服务器的IP地址和端口; -
上传到镜像服务器
[root@localhost ~]# docker push 192.168.1.40:5000/busybox:v1 The push refers to repository [192.168.1.40:5000/busybox] Get https://192.168.1.40:5000/v2/: http: server gave HTTP response to HTTPS client
这里报错了,需要https的方法才能上传,我们可以修改下daemon.json来解决:
[root@node ~]# vi /etc/docker/daemon.json { "registry-mirrors": [ "https://registry.docker-cn.com"], "insecure-registries": [ "192.168.1.40:5000"] } systemctl restart docker
添加私有镜像服务器的地址,注意书写格式为json,有严格的书写要求。
再一次上传镜像文件:[root@localhost ~]# docker push 192.168.1.40:5000/busybox:v1 The push refers to repository [192.168.1.40:5000/busybox] 6194458b07fc: Layer already exists v1: digest: sha256:bf510723d2cd2d4e3f5ce7e93bf1e52c8fd76831995ac3bd3f90ecc866643aff size: 527
-
查看主机1镜像服务器仓库
[root@localhost ~]# curl http://127.0.0.1:5000/v2/_catalog {"repositories":["busybox"]}
四、测试从registry服务器私有仓库中下载镜像
- 先删除测试主机2上的所有镜像
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.1.40:5000/busybox v1 e4db68de4ff2 2 weeks ago 1.22MB busybox latest e4db68de4ff2 2 weeks ago 1.22MB [root@localhost ~]# docker rmi -f $(docker images -aq) Untagged: 192.168.1.40:5000/busybox:v1 Untagged: 192.168.1.40:5000/busybox@sha256:bf510723d2cd2d4e3f5ce7e93bf1e52c8fd76831995ac3bd3f90ecc866643aff Untagged: busybox:latest Untagged: busybox@sha256:c94cf1b87ccb80f2e6414ef913c748b105060debda482058d2b8d0fce39f11b9 Deleted: sha256:e4db68de4ff27c2adfea0c54bbb73a61a42f5b667c326de4d7d5b19ab71c6a3b Deleted: sha256:6194458b07fcf01f1483d96cd6c34302ffff7f382bb151a6d023c4e80ba3050a Error: No such image: e4db68de4ff2 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE
- 从主机2的registry服务器上下载busybox镜像
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@localhost ~]# docker pull 192.168.1.40:5000/busybox:v1 v1: Pulling from busybox 8e674ad76dce: Pull complete Digest: sha256:bf510723d2cd2d4e3f5ce7e93bf1e52c8fd76831995ac3bd3f90ecc866643aff Status: Downloaded newer image for 192.168.1.40:5000/busybox:v1 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 192.168.1.40:5000/busybox v1 e4db68de4ff2 2 weeks ago 1.22MB
五、检查主机1的仓库
-
列出私有仓库中所有镜像
[root@localhost ~]# curl http://192.168.1.40:5000/v2/_catalog {"repositories":["busybox"]}
-
列出busybox镜像有哪些tag
[root@localhost ~]# curl http://192.168.1.40:5000/v2/busybox/tags/list {"name":"busybox","tags":["v1"]}
-
可以在主机2的cd /opt/registry/docker/registry/v2/repositories目录下
找到主机1push的镜像
[root@localhost repositories]# pwd /opt/registry/docker/registry/v2/repositories [root@localhost repositories]# ll 总用量 0 drwxr-xr-x. 5 root root 55 6月 23 11:15 busybox