本文阐释了怎样部署私有的 Docker Registry 服务 —— 或为公司私用,或公开给其他用户使用。例如,你公司可能需要私人的 Registry 来支持持续集成(CI)。又或,你的公司可能有大量镜像方式的产品或服务,你想以公司品牌的方式来整体提供和呈现。
Docker 公共的 Registry 里维护有一个默认的
registry
镜像可以用以协助该部署过程。该 Registry 镜像对本地测试足矣,但不能用于生产环境。对于生产环境,应以docker/distribution
为基础,自行配置并构建自定义 Registry 镜像。注意:本文中的例子在 Ubuntu 14.04 下编写及测试。如果你在不同的操作系统中运行
Docker,你或许需要“翻译”这些命令,以适合你运行环境的需要。官方镜像下的简单示例
本节中,将创建一个 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:latestThe push refers to a repository [localhost:5000/hello-mine] (len: 1)e45a5af57b00: Image already exists31cbccb51277: Image successfully pushed511136ea3c5a: Image already existsDigest: sha256:a1b13bc01783882434593119198938b9b9ef2bd32a0a246f16ac99b01383ef7a8、使用
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 MB10、试运行
hello-mine
。$ docker run hello-mineUnable to find image 'hello-mine:latest' locallyPulling 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 的客户端,都能推送及拉取镜像。参看下一节,获取使该镜像做好生产环境准备的信息。
原文:https://github.com/docker/distribution/blob/master/docs/deploying.md
译文:http://livedig.com/686
查看原文:http://www.zoues.com/2016/10/20/%e5%a6%82%e4%bd%95%e9%83%a8%e7%bd%b2-docker-registry-%e6%9c%8d%e5%8a%a1/
如何部署 Docker Registry 服务
最新推荐文章于 2024-08-21 11:22:01 发布