第 3 章 镜像 - 020 - 搭建本地 Registry

Docker Hub 虽然非常方便,但还是有些限制,比如:

  1. 需要 internet 连接,而且下载和上传速度慢。
  2. 上传到 Docker Hub 的镜像任何人都能够访问,虽然可以用私有 repository,但不是免费的。
  3. 安全原因很多组织不允许将镜像放到外网。

解决方案就是搭建本地的 Registry。

在 Docker Hub 上也有官方的镜像 registry。我们可以在 Docker 中运行自己的 registry。

启动 registry 容器

 root@ubuntu:~# docker run -d -p : -v /myregistry:/var/lib/registry registry:
Unable to find image 'registry:2' locally
: Pulling from library/registry
cd784148e348: Pull complete
0ecb9b11388e: Pull complete
918b3ddb9613: Pull complete
5aa847785533: Pull complete
adee6f546269: Pull complete
Digest: sha256:979960c6c4e885f9c55f19c85ed33ebca2341a56998a82eecb3529ff0c7c3a7e
Status: Downloaded newer image for registry:
f27eb6348026862f5744a916f67c4079d36c54fd46091d27a6c5d916f00ae5bc
  • 使用的镜像是 registry:2。
  • -d 是后台启动容器。
  • -p 将容器的 5000 端口映射到 Host 的 5000 端口。5000 是 registry 服务端口。
  • -v 将容器 /var/lib/registry 目录映射到 Host 的 /myregistry,用于存放镜像数据。

通过 docker tag 重命名镜像,使之与 registry 匹配

 root@ubuntu:~# docker tag gsophy/docker.edu:v1 localhost:/gsophy/docker.edu:v1  #在镜像的前面加上了运行 registry 的主机名称和端口
root@ubuntu:~#
root@ubuntu:~# docker images localhost:/gsophy/docker.edu
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost:/gsophy/docker.edu v1 ef1dc54703e2 weeks ago 132MB
  • 镜像名称由 repository 和 tag 两部分组成。
  • 而 repository 的完整格式为:[registry-host]:[port]/[username]/xxx
  • 只有 Docker Hub 上的镜像可以省略 [registry-host]:[port]

通过 docker push 上传镜像

 root@ubuntu:~# docker push localhost:/gsophy/docker.edu:v1
The push refers to repository [localhost:/gsophy/docker.edu]
64446057e402: Pushed
13a694db88ed: Pushed
3fc0ec65884c: Pushed
30d0b099e805: Pushed
7b4e562e58dc: Pushed
v1: digest: sha256:246fed9aa9be7aaba1e04d9146be7a3776c9a40b5cfb3242d3427f79edee37db size:
root@ubuntu:~#

现在已经可通过 docker pull 从本地 registry 下载镜像了

 root@ubuntu:~# docker pull localhost:/gsophy/docker.edu:v1
v1: Pulling from gsophy/docker.edu
177e7ef0df69: Pull complete
65e7b631411e: Pull complete
3249ef8b9272: Pull complete
43ed8fedd0f6: Pull complete
02d3f33bda9d: Pull complete
Digest: sha256:246fed9aa9be7aaba1e04d9146be7a3776c9a40b5cfb3242d3427f79edee37db
Status: Downloaded newer image for localhost:/gsophy/docker.edu:v1
root@ubuntu:~#
root@ubuntu:~# docker images localhost:/gsophy/docker.edu:v1
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost:/gsophy/docker.edu v1 ef1dc54703e2 weeks ago 132MB

除了镜像的名称长一些(包含 registry host 和 port),使用方式完全一样。

以上是搭建本地 registry 的简要步骤。当然 registry 也支持认证,https 安全传输等特性,具体可以参考官方文档 https://docs.docker.com/registry/configuration/

----------------------引用来自-----------------------

https://mp.weixin.qq.com/s?__biz=MzIwMTM5MjUwMg==&mid=2653587627&idx=1&sn=b85416005be844a921c146883ac0e6b8&chksm=8d3080b2ba4709a42459bd4eb977e225e2847c7bc39888c3f6f3e2d903eaf54556518683fa57&scene=21#wechat_redirect

上一篇:Filter、Listener 学习总结


下一篇:PHP微信开发之模板消息回复