如何部署 Docker Registry 服务

2025-04-30 17:30:47
推荐回答(1个)
回答1:

  将创建一个 Container 来运行 Docker 的官方 Registry 镜像。你将推送(Push)一个镜像到这个 Registry 服务器,然后再从该 Registry 中拉取(Pull)同一个镜像。

  这是个很好的练习,有助于理解客户端与本地 Registry 的基本交互。

  1、安装 Docker。

  2、从 Docker 公共 Registry 中运行 hello-world 镜像。

  $ docker run hello-world
  run 命令自动从 Docker 的官方镜像库中将 hello-world 镜像 pull 下来。

  3、在 localhost 上启动 Registry 服务。

  $ docker run -p 5000:5000 registry:2.0
  这将在 DOCKER_HOST 上启动一个 Registry 服务,并在 5000 端口监听。
  4、列出镜像。

  $ docker images
  REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
  registry 2.0 bbf0b6ffe923 3 days ago 545.1 MB
  golang 1.4 121a93c90463 5 days ago 514.9 MB
  hello-world latest e45a5af57b00 3 months ago 910 B
  这个列表应当包括一个由先前运行而得来的 hello-world 镜像。

  5、为本地 repoistory 重新标记 hello-world 镜像。

  $ docker tag hello-world:latest localhost:5000/hello-mine:latest
  此命令使用 [REGISTRYHOST/]NAME[:TAG] 格式为 hello-world:latest 重新打标。REGISTRYHOST在此例中是 localhost。在 Mac OSX 环境中,得把 localhost 换成 $(boot2docker ip):5000。

  6、列出新镜像。

  $ docker images
  REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
  registry 2.0 bbf0b6ffe923 3 days ago 545.1 MB
  golang 1.4 121a93c90463 5 days ago 514.9 MB
  hello-world latest e45a5af57b00 3 months ago 910 B
  localhost:5000/hello-mine latest ef5a5gf57b01 3 months ago 910 B
  可以看到,新镜像已经出现在列表中。

  7、推送新镜像到本地 Registry 中。

  $ docker push localhost:5000/hello-mine:latest
  The push refers to a repository [localhost:5000/hello-mine] (len: 1)
  e45a5af57b00: Image already exists
  31cbccb51277: Image successfully pushed
  511136ea3c5a: Image already exists
  Digest: sha256:a1b13bc01783882434593119198938b9b9ef2bd32a0a246f16ac99b01383ef7a
  8、使用 curl 命令及 Docker Registry 服务 API v2 列出 Registry 中的镜像