Docker私有仓库Registry如何搭建

50次阅读
没有评论

共计 2964 个字符,预计需要花费 8 分钟才能阅读完成。

这篇文章主要为大家展示了“Docker 私有仓库 Registry 如何搭建”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让丸趣 TV 小编带领大家一起研究并学习一下“Docker 私有仓库 Registry 如何搭建”这篇文章吧。

1.  关于 Registry

官方的 Docker hub 是一个用于管理公共镜像的好地方,我们可以在上面找到我们想要的镜像,也可以把我们自己的镜像推送上去。但是,有时候,我们的使用场景需要我们拥有一个私有的镜像仓库用于管理我们自己的镜像。这个可以通过开源软件 Registry 来达成目的。

 Registry 在 github 上有两份代码:老代码库和新代码库。老代码是采用 python 编写的,存在 pull 和 push 的性能问题,出到 0.9.1 版本之后就标志为 deprecated,不再继续开发。从 2.0 版本开始就到在新代码库进行开发,新代码库是采用 go 语言编写,修改了镜像 id 的生成算法、registry 上镜像的保存结构,大大优化了 pull 和 push 镜像的效率。

  官方在 Docker hub 上提供了 registry 的镜像(详情),我们可以直接使用该 registry 镜像来构建一个容器,搭建我们自己的私有仓库服务。Tag 为 latest 的 registry 镜像是 0.9.1 版本的,我们直接采用 2 版本。

2. Registry 的部署

运行下面命令获取 registry 镜像,

$ sudo docker pull registry:2

然后启动一个容器,

$ sudo docker run -d -v /wks/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:2

Registry 服务默认会将上传的镜像保存在容器的 /var/lib/registry,我们将主机的 /wks/registry 目录挂载到该目录,即可实现将镜像保存到主机的 /wks/registry 目录了。

  运行 docker ps 看一下容器情况,

$ sudo docker ps 
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3766397a458 registry:2  /bin/registry /etc/d  46 seconds ago Up 45 seconds 0.0.0.0:5000- 5000/tcp registry

说明我们已经启动了 registry 服务,打开浏览器输入 http://127.0.0.1:5000/v2,出现下面情况说明 registry 运行正常,

 Docker 私有仓库 Registry 如何搭建

3.  验证

现在我们通过将镜像 push 到 registry 来验证一下。

我的机器上有个 hello-world 的镜像,我们要通过 docker tag 将该镜像标志为要推送到私有仓库,

$ sudo docker tag hello-world 127.0.0.1:5000/hello-world

然后查看以下本地的镜像,

$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry 2 b91f745cd233 5 days ago 220.1 MB
ubuntu 14.04 a5a467fddcb8 6 days ago 187.9 MB
hello-world latest 975b84d108f1 2 weeks ago 960 B127.0.0.1:5000/hello-world latest 975b84d108f1 2 weeks ago 960 B

接下来,我们运行 docker push 将 hello-world 镜像 push 到我们的私有仓库中,

$ sudo docker push 127.0.0.1:5000/hello-world
The push refers to a repository [127.0.0.1:5000/hello-world] (len: 1)
975b84d108f1: Image successfully pushed 
3f12c794407e: Image successfully pushed 
latest: digest: sha256:1c7adb1ac65df0bebb40cd4a84533f787148b102684b74cb27a1982967008e4b size: 2744

现在我们可以查看我们本地 /wks/registry 目录下已经有了刚推送上来的 hello-world。我们也在浏览器中输入 http://127.0.0.1:5000/v2/_catalog,如下图所示,

 Docker 私有仓库 Registry 如何搭建

现在我们可以先将我们本地的 127.0.0.1:5000/hello-world 和 hello-world 先删除掉,

$ sudo docker rmi hello-world
$ sudo docker rmi 127.0.0.1:5000/hello-world

然后使用 docker pull 从我们的私有仓库中获取 hello-world 镜像,

$ sudo docker pull 127.0.0.1:5000/hello-world
Using default tag: latest
latest: Pulling from hello-world
b901d36b6f2f: Pull complete 
0a6ba66e537a: Pull complete 
Digest: sha256:1c7adb1ac65df0bebb40cd4a84533f787148b102684b74cb27a1982967008e4b
Status: Downloaded newer image for 127.0.0.1:5000/hello-world:latest
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry 2 b91f745cd233 5 days ago 220.1 MB
ubuntu 14.04 a5a467fddcb8 6 days ago 187.9 MB127.0.0.1:5000/hello-world latest 0a6ba66e537a 2 weeks ago 960 B

4.  可能问题

可能会出现无法 push 镜像到私有仓库的问题。这是因为我们启动的 registry 服务不是安全可信赖的。这是我们需要修改 docker 的配置文件 /etc/default/docker,添加下面的内容,

 DOCKER_OPTS= –insecure-registry xxx.xxx.xxx.xxx:5000

然后重启 docker 后台进程,

$ sudo service docker restart

这是再 push 即可。

以上是“Docker 私有仓库 Registry 如何搭建”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注丸趣 TV 行业资讯频道!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-25发表,共计2964字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)