Docker搭建私有仓库

绝地灬酷狼 2021-10-14 23:35 592阅读 0赞

文章目录

    • Docker搭建私有仓库
      • 安装docker
      • 安装运行 docker-registry
      • 在私有仓库上传镜像
      • 私有仓库下载镜像
      • 其他主机上传镜像
      • 参考

Docker搭建私有仓库

安装docker

这里就不赘述了,我们可以直接参考:https://www.runoob.com/docker/docker-tutorial.html
本文采用的是Centos 7版本,docker version是19.03.1

安装运行 docker-registry

可以通过获取官方 registry 镜像来运行。

  1. docker run -d -p 5000:5000 --restart=always --name registry registry
  2. [root@cai ~]# docker run -d -p 5000:5000 --restart=always --name registry registry
  3. Unable to find image 'registry:latest' locally
  4. latest: Pulling from library/registry
  5. c87736221ed0: Pull complete
  6. 1cc8e0bb44df: Pull complete
  7. 54d33bcb37f5: Pull complete
  8. e8afc091c171: Pull complete
  9. b4541f6d3db6: Pull complete
  10. Digest: sha256:8004747f1e8cd820a148fb7499d71a76d45ff66bac6a29129bfdbfdc0154d146
  11. Status: Downloaded newer image for registry:latest
  12. c56e18bc9616128030b22047d787a84d078c2356d56755e9e97e84c1037b0676

然后我们通过docker image命令查看到我们下载的镜像

  1. [root@cai ~]# docker image ls
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. registry latest f32a97de94e1 5 months ago 25.8MB

然后我们通过docker pull命令下载一个镜像,我们这里下载最简单的一个hello-world镜像

  1. [root@cai ~]# docker pull hello-world

在私有仓库上传镜像

创建好私有仓库之后,就可以使用 docker tag 来标记一个镜像,然后推送它到仓库。例如私有仓库地址为 127.0.0.1:5000,然后我们就可以把刚刚下载的hello-world镜像打包一个自己的镜像上传上去。

使用 docker tag 将 hello-world:latest 这个镜像标记为 127.0.0.1:5000/my-hello-world:latest。

格式为 docker tag IMAGE[:TAG] [REGISTRY_HOST[:REGISTRY_PORT]/]REPOSITORY[:TAG]

  1. [root@cai ~]# docker tag hello-world:latest 127.0.0.1:5000/my-hello-world:lastest
  2. [root@cai ~]# docker image ls
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. registry latest f32a97de94e1 5 months ago 25.8MB
  5. 127.0.0.1:5000/my-hello-world lastest fce289e99eb9 7 months ago 1.84kB
  6. hello-world latest fce289e99eb9 7 months ago 1.84kB

使用 docker push 上传标记的镜像。

  1. [root@cai ~]# docker push 127.0.0.1:5000/my-hello-world
  2. The push refers to repository [127.0.0.1:5000/my-hello-world]
  3. af0b15c8625b: Pushed
  4. lastest: digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a size: 524

然后我们通过私有仓库接口查看一下镜像。

  1. [root@cai ~]# curl 127.0.0.1:5000/v2/_catalog
  2. { "repositories":["my-hello-world"]}

说明镜像已经上传上去。

私有仓库下载镜像

现在我们尝试从私有仓库下载这个镜像。

首先我们先移除掉本地的镜像。

  1. [root@cai ~]# docker image rm 127.0.0.1:5000/my-hello-world:lastest
  2. Untagged: 127.0.0.1:5000/my-hello-world:lastest
  3. Untagged: 127.0.0.1:5000/my-hello-world@sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
  4. [root@cai ~]# docker image ls
  5. REPOSITORY TAG IMAGE ID CREATED SIZE
  6. registry latest f32a97de94e1 5 months ago 25.8MB
  7. hello-world latest fce289e99eb9 7 months ago 1.84kB

然后我们通过docker pull来拉取我们的镜像。

  1. [root@cai ~]# docker pull 127.0.0.1:5000/my-hello-world:lastest
  2. lastest: Pulling from my-hello-world
  3. Digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a
  4. Status: Downloaded newer image for 127.0.0.1:5000/my-hello-world:lastest
  5. 127.0.0.1:5000/my-hello-world:lastest
  6. [root@cai ~]# docker image ls
  7. REPOSITORY TAG IMAGE ID CREATED SIZE
  8. registry latest f32a97de94e1 5 months ago 25.8MB
  9. hello-world latest fce289e99eb9 7 months ago 1.84kB
  10. 127.0.0.1:5000/my-hello-world lastest fce289e99eb9 7 months ago 1.84kB

其他主机上传镜像

如果你不想使用 127.0.0.1:5000 作为仓库地址,比如想让本网段的其他主机也能把镜像推送到私有仓库。你就得把例如 192.168.174.136:5000 这样的内网地址作为私有仓库地址,这时你会发现无法成功推送镜像。

这是因为 Docker 默认不允许非 HTTPS 方式推送镜像。我们可以通过 Docker 的配置选项来取消这个限制。

我们现在有另一台主机,ip地址为192.168.174.137,然后我们尝试推送镜像到我们192.168.174.136的私有仓库中。
我们先下载个镜像。

  1. [root@cai ~]# docker pull hello-world
  2. Using default tag: latest
  3. latest: Pulling from library/hello-world
  4. 1b930d010525: Pull complete
  5. Digest: sha256:6540fc08ee6e6b7b63468dc3317e3303aae178cb8a45ed3123180328bcc1d20f
  6. Status: Downloaded newer image for hello-world:latest
  7. docker.io/library/hello-world:latest
  8. [root@cai ~]# docker images
  9. REPOSITORY TAG IMAGE ID CREATED SIZE
  10. hello-world latest fce289e99eb9 7 months ago 1.84kB
  11. [root@cai ~]#

然后打包

  1. [root@cai ~]# docker tag hello-world:latest 192.168.174.136:5000/my-docker2-hello-world:lastest
  2. [root@cai ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. 192.168.174.136:5000/my-docker2-hello-world lastest fce289e99eb9 7 months ago 1.84kB
  5. hello-world latest fce289e99eb9 7 months ago 1.84kB
  6. [root@cai ~]#

然后我们把镜像推到192.168.174.136这台机器的私有仓库中。
然后发现出现如下问题:

  1. [root@cai ~]# docker push 192.168.174.136:5000/my-docker2-hello-world
  2. The push refers to repository [192.168.174.136:5000/my-docker2-hello-world]
  3. Get https://192.168.174.136:5000/v2/: dial tcp 192.168.174.136:5000: connect: no route to host

这个时候可以尝试把两台机器的防火墙都关闭。

  1. [root@cai ~]# systemctl stop firewalld

然后我们继续

  1. [root@cai ~]# docker push 192.168.174.136:5000/my-docker2-hello-world
  2. The push refers to repository [192.168.174.136:5000/my-docker2-hello-world]
  3. Get https://192.168.174.136:5000/v2/: http: server gave HTTP response to HTTPS client
  4. [root@cai ~]#

然后我们需要在推送的那台机器,也就是在IP为192.168.174.137这台机器上配置一下/etc/docker/daemon.json(如果文件不存在请新建该文件)

  1. [root@cai ~]# vim /etc/docker/daemon.json
  2. {
  3. "registry-mirror": [
  4. "https://registry.docker-cn.com"
  5. ],
  6. "insecure-registries": [
  7. "192.168.174.136:5000"
  8. ]
  9. }

然后重启docker

  1. [root@cai ~]# systemctl restart docker

重新push

  1. [root@cai ~]# docker push 192.168.174.136:5000/my-docker2-hello-world
  2. The push refers to repository [192.168.174.136:5000/my-docker2-hello-world]
  3. af0b15c8625b: Pushed
  4. lastest: digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a size: 524

然后查看私有仓库的接口返回

  1. [root@cai ~]# curl 192.168.174.136:5000/v2/_catalog
  2. {"repositories":["my-docker2-hello-world","my-hello-world"]}

说明已经能成功上传镜像了。

参考

https://yeasy.gitbooks.io/docker_practice/repository/registry.html

发表评论

表情:
评论列表 (有 0 条评论,592人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Docker私有仓库

    仓库搭建 搭建私有仓库最简单的方法是在容器管理节点(物理机或者虚拟机)上搭建registry容器,并将其作为企业内部的私有仓库,存放所有需要部署的容器镜像。 首先,让我