如何部署 Docker Registry 服务

2025-02-26 09:52:31
推荐回答(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 中的镜像:

  $ curl -v -X GET http://localhost:5000/v2/hello-mine/tags/list* Hostname was NOT found in DNS cache
  * Trying 127.0.0.1...* Connected to localhost (127.0.0.1) port 5000 (#0)> GET /v2/hello-mine/tags/list HTTP/1.1> User-Agent: curl/7.35.0> Host: localhost:5000> Accept: */*
  >
  < HTTP/1.1 200 OK
  < Content-Type: application/json; charset=utf-8
  < Docker-Distribution-Api-Version: registry/2.0
  < Date: Sun, 12 Apr 2015 01:29:47 GMT
  < Content-Length: 40
  <
  {"name":"hello-mine","tags":["latest"]}
  * Connection #0 to host localhost left intact
  也可以通过在浏览器中访问以下地址来获取这些信息:http://localhost:5000/v2/hello-mine/tags/list

  9、从你的本地环境中移除所有未使用的镜像:

  $ docker rmi -f $(docker images -q -a )
  此命令仅用于说明目的;移除镜像强制 run 从 Registry 而不是从本地缓存拉取。如果在这之后运行docker images,在你的镜像列表中,应该看不到任何 hello-world 或 hello-mine 的实例。

  $ 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
  10、试运行 hello-mine。

  $ docker run hello-mine
  Unable to find image 'hello-mine:latest' locally
  Pulling repository hello-mine
  FATA[0001] Error: image library/hello-mine:latest not found
  命令 run 运行失败,因为你的新镜像在 Docker 公共 Registry 中是不存在的。
  11、现在,尝试指定镜像的 Registry 来运行镜像:

  $ docker run localhost:5000/hello-mine
  如果你在这之后运行 docker images, 你会发现里面多了一个 hello-mine 的实例。

  

  使 Docker 官方 Registry 镜像做好生产环境准备

  Docker 的官方镜像只为简单的测试或除错准备。其配置对多数生产环境来讲都不适用。例如,任何能访问服务器 IP 的客户端,都能推送及拉取镜像。参看下一节,获取使该镜像做好生产环境准备的信息。

  

  理解生产环境的部署

  当部署一个用于生产环境发布的 Registry 时,须考虑如下因素:

  BACKEND STORAGE 应在何处存储镜像?
  ACCESS AND/OR AUTHENTICATION 用户是否应拥有全部或受控的访问权限?这取决于你为公众提供镜像服务,还是只为公司内部提供。
  DEBUGGING 当问题或状况发生时,是否有解决这些它们的方法。日志由于可以看到问题动向,这使其很有用。
  CACHING 快速提取镜像可能至关重要,如果依赖镜像进行测试、构建,或有其他自动化系统的话。
  我们可以配置 Registry 功能特性,用以调整适配这些因素。可以在命令行里指定选项来干这个,或者更通常地,用一个 Registry 配置文件来完成此事。配置文件是 YAML 格式的。

  Docker 的官方 Repository 镜像用以下配置文件做了预置:

  version: 0.1
  log:
  level: debug
  fields:
  service: registry
  environment: development
  storage:
  cache:
  layerinfo: inmemory
  filesystem:
  rootdirectory: /tmp/registry-dev
  http:
  addr: :5000
  secret: asecretforlocaldevelopment
  debug:
  addr: localhost:5001
  redis:
  addr: localhost:6379
  pool:
  maxidle: 16
  maxactive: 64
  idletimeout: 300s
  dialtimeout: 10ms
  readtimeout: 10ms
  writetimeout: 10ms
  notifications:
  endpoints:
  - name: local-8082
  url: http://localhost:5003/callback
  headers:
  Authorization: [Bearer ]
  timeout: 1s
  threshold: 10
  backoff: 1s
  disabled: true
  - name: local-8083
  url: http://localhost:8083/callback
  timeout: 1s
  threshold: 10
  backoff: 1s
  disabled: true
  这个配置非常基本,可以看到这在生产环境下会有一些问题。例如,http 区块详述了运行 Registry 的主机的 HTTP 服务器配置,但服务器没有使用甚至是最低要求的传输层安全性(TLS)配置。接下来我们将配置这些东西。