Docker私有仓库Registry

在Docker生态圈中,Docker仓库用于存储Docker镜像。官方的Docker hub存储了大量镜像,但是对于实际生产环境中,考虑到pull/push速度和保密性等因素,我们往往需要建立自己的镜像仓库服务。官方(docker-registry github )提供了一个registry images,使用这个images就可以快速完成本地镜像仓库的建立。

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

Registry的部署

需要 Docker version 1.6.0 或更新。

启动registry,并将服务映射到主机的5000端口上:

#
docker run -d -p 5000:5000 --restart=always --name registry registry:2

从官方pull个镜像并打上本地registry的tag:

docker pull ubuntu && docker tag ubuntu localhost:5000/ubuntu

push到本地registry:

docker push localhost:5000/ubuntu

从本地registry重新pull下来:

#
docker pull localhost:5000/ubuntu

停止registry:


docker stop registry && docker rm -v registry

存储

默认情况下,registry数据通过主机文件系统的docker volume实现持久化。你也可以自己进行修改:

启动registry,这里使用本地目录作为存储,。


docker run -d -p 5000:5000 --restart=always --name registry
-v `pwd`/data:/var/lib/
registry:2

# mkdir /var/lib/docker/registry
# docker run -d -e --restart=always STORAGE_PATH=/registry -e SETTINGS_FLAVOR=local -p 5000:5000 -v /var/lib/docker/registry:/registry --name registry

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

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

# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25951e255ba7 registry "docker-registry" 3 minutes ago Up 3 minutes 0.0.0.0:5000->5000/tcp registry_instance

检测http服务状态:

# curl 127.0.0.1:5000
""docker-registry server""

从本地仓库上获取有哪些镜像

curl -X GET http://127.0.0.1:5000/v1/repositories/search
curl http://127.0.0.1:5000/v1/repositories/search
{"num_results": 1, "query": "", "results": [{"description": "", "name": "library/centos6"}]}

验证

现在我们通过将镜像push到registry来验证一下。我的机器上有个hello-world的镜像,我们要通过docker tag将该镜像标志为要推送到私有仓库,新image名称必须带有”主机IP:5000”。

#
docker tag hello-world 127.0.0.1:5000/hello-world
大专栏   Docker私有仓库Registryre>

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

#
docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry 2.1.1 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 B
127.0.0.1:5000/hello-world latest 975b84d108f1 2 weeks ago 960 B

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

#
docker push 127.0.0.1:5000/hello-world
The push refers to a repository [127.0.0.1:5000/hello-world] (len: 1)
Sending image list
Pushing repository 127.0.0.1:5000/hello-world (1 tags)
535020c3e8ad: Image successfully pushed
af340544ed62: Image successfully pushed
Pushing tag for rev [af340544ed62] on {http://127.0.0.1:5000/v1/repositories/hello-world/tags/latest}

查看registry挂载的目录,可以看到images已经被成功上传。


ls /var/lib/docker//images

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

#
docker rmi hello-world
docker rmi 127.0.0.1:5000/hello-world

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

#
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

# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
registry 2.1.1 b91f745cd233 5 days ago 220.1 MB
ubuntu 14.04 a5a467fddcb8 6 days ago 187.9 MB
127.0.0.1:5000/hello-world latest 0a6ba66e537a 2 weeks ago 960 B

同样,通过查看registry的日志,可以看到images通过PUT/GET来操作。


docker logs e1efcb6528a5

Running a domain registry

Get a certificate

可能问题

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

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

然后重启docker后台进程,再push即可。


service docker restart

猜你喜欢

转载自www.cnblogs.com/wangziqiang123/p/11690557.html