环境说明:
操作系统:centos7
内核版本:
[[email protected] ~]# uname -r
3.10.0-693.21.1.el7.x86_64
摘要:
仓库(Repository)是集中存放镜像的地方,与之关联的是注册服务器。
那注册服务器和仓库有什么关系呢?
比如仓库地址为:https://dl.dockerpool.com/ubuntu
那么dl.dockerpool.com就是注册服务器,ubuntu是仓库名
也就是说,注册服务器上可以存放多个镜像仓库,而每个镜像仓库里面存放多个镜像
Docker Hub 虽然非常方便,但还是有些限制,比如:
需要 internet 连接,而且下载和上传速度慢。
上传到 Docker Hub 的镜像任何人都能够访问,虽然可以用私有 repository,但不是免费的。
安全原因很多组织不允许将镜像放到外网。
一、公共仓库Registry使用
Docker Hub 是 Docker 公司维护的公共 Registry。用户可以将自己的镜像保存到 Docker Hub 免费的 repository 中。如果不希望别人访问自己的镜像,也可以购买私有 repository。
除了 Docker Hub,quay.io 是另一个公共 Registry,提供与 Docker Hub 类似的服务。
- 在https://hub.docker.com/?next=https%3A%2F%2Fhub.docker.com%2F
注册一个Docker Hub账号
2.在Dokcer host上验证登录
[email protected] ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don‘t have a Docker ID, head over to https://hub.docker.com to create one.
Username: liulei123
Password:
Login Succeeded
3.Docker Hub 为了区分不同用户的同名镜像,镜像的 registry 中要包含用户名,完整格式为:[username]/xxx:tag
通过docker tag 命令重命名镜像名字
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[[email protected] ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
469cfcc7a4b3: Pull complete
Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16
Status: Downloaded newer image for docker.io/centos:latest
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest e934aafc2206 13 days ago 199 MB
[[email protected] ~]# docker tag docker.io/centos liu/centos ##把docker.io重命名为liu/centos
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest e934aafc2206 13 days ago 199 MB
liu/centos latest e934aafc2206 13 days ago 199 MB
[[email protected] ~]#
4.通过docker push 命令把本地镜像liu/centos推送到公共仓库Docker Hup
[[email protected] ~]# docker push liu/centos
The push refers to a repository [docker.io/liu/centos]
43e653f84b79: Preparing
denied: requested access to the resource is denied
[[email protected] ~]#
上传镜像报错分析:
上面的信息显示是拒接访问,因为tag的名字斜线前面部分liu不是我在Docker Hub上注册的用户名liulei123,因此使用docker push liu/centos是无法正常上传本地镜像的,需要把本地镜像liu/centos修改为liulei123/centos才能把本地镜像上传到Dokcer Hub上
以下是解决过程
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest e934aafc2206 13 days ago 199 MB
liu/centos latest e934aafc2206 13 days ago 199 MB
[[email protected] ~]# docker tag liu/centos liulei123/centos
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
liu/centos latest e934aafc2206 13 days ago 199 MB
liulei123/centos latest e934aafc2206 13 days ago 199 MB
docker.io/centos latest e934aafc2206 13 days ago 199 MB
[[email protected] ~]# docker push liulei123/centos
The push refers to a repository [docker.io/liulei123/centos]
43e653f84b79: Pushed
latest: digest: sha256:191c883e479a7da2362b2d54c0840b2e8981e5ab62e11ab925abf8808d3d5d44 size: 529
5.登录 https://hub.docker.com,在Public Repository 中就可以看到本地上传的镜像
二、在Docker上搭建本地的 Registry。
说明:
Docker Hub 虽然非常方便,但还是有些限制,比如:
- 需要 internet 连接,而且下载和上传速度慢。
- 上传到 Docker Hub 的镜像任何人都能够访问,虽然可以用私有 repository,但不是免费的。
- 安全原因很多组织不允许将镜像放到外网。
- 搭建本地的 Registry是解决以上问题的一种选择
- 下载并启动一个Registry容器,创建docker本地私有仓库
[[email protected] ~]# docker run -d -p 5000:5000 -v /mygistry:/var/lib/registry registry:2 Unable to find image ‘registry:2‘ locally Trying to pull repository docker.io/library/registry ... 2: Pulling from docker.io/library/registry 81033e7c1d6a: Pull complete b235084c2315: Pull complete c692f3a6894b: Pull complete ba2177f3a70e: Pull complete a8d793620947: Pull complete Digest: sha256:672d519d7fd7bbc7a448d17956ebeefe225d5eb27509d8dc5ce67ecb4a0bce54 Status: Downloaded newer image for docker.io/registry:2 0ec05f0a53342d78dc0b6e1697328b5588f6f6744c781e1741cff038fbc04f02
参数说明:
-d 表示后台启动容器
-p 将容器的5000端口映射到宿主机5000端口,5000是注册服务器服务端口
-v 将 registry容器中/var/lib/registry的目录映射到宿主机的 /mygistry
2.使用docker tag 将liulei123/centos镜像标记为10.71.11.11:5000/liulei123/registry
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest e934aafc2206 13 days ago 199 MB
liu/centos latest e934aafc2206 13 days ago 199 MB
liulei123/centos latest e934aafc2206 13 days ago 199 MB
docker.io/registry 2 d1fd7d86a825 3 months ago 33.3 MB
[[email protected] ~]# docker tag liulei123/centos 10.71.11.11:5000/liulei123/registry
3.通过docker push 上传镜像
[[email protected] ~]# docker push 10.71.11.11:5000/liulei123/registry
The push refers to a repository [10.71.11.11:5000/liulei123/registry]
Get https://10.71.11.11:5000/v1/_ping: http: server gave HTTP response to HTTPS client
报错1:Get https://10.71.11.11:5000/v1/_ping: http: server gave HTTP response to HTTPS client
原因分析:
这个问题可能是由于客户端采用https,docker registry未采用https服务所致。
解决方法:
编辑/etc/docker/daemon.json文件,写入下面配置
{"insecure-registries":["10.71.11.11:5000"] }
重启docker服务
[[email protected] docker]# systemctl restart docker
报错2:
[[email protected] docker]# docker push 10.71.11.11:5000/liulei123/registry
The push refers to a repository [10.71.11.11:5000/liulei123/registry]
Put http://10.71.11.11:5000/v1/repositories/liulei123/registry/: dial tcp 10.71.11.11:5000: getsockopt: connection refused
说明:从报错信息Put http://10.71.11.11:5000/v1/repositories/liulei123/registry/: dial tcp 10.71.11.11:5000: getsockopt: connection refused来看,估计大家会先去网上查找相关报错的信息,经过查阅大量的文档,说是需要编辑/etc/sysconfig/docker,加入如下配置
但是加入INSECURE_REGISTRY=‘--insecure-registry=10.71.11.11:5000‘
配置后,重启docker服务报错
[[email protected] docker]# vi /etc/sysconfig/docker
[[email protected] docker]# systemctl restart docker
Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
把添加的配置注释掉后,重启docker成功
后来想了想,看看我的registry容器服务启动没有
[[email protected] docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
突然才恍然大悟,registry容器都没启动,上传本地镜像,肯定要报错,问题解决过程如下
拿到registry容器ID
[[email protected] docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ec05f0a5334 registry:2 "/entrypoint.sh /e..." 36 minutes ago Exited (2) 19 minutes ago friendly_mahavira
然后用容器ID启动容器
[[email protected] docker]# docker start 0ec05f0a5334
0ec05f0a5334
[[email protected] docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0ec05f0a5334 registry:2 "/entrypoint.sh /e..." 37 minutes ago Up 5 seconds 0.0.0.0:5000->5000/tcp friendly_mahavira
再次向镜像仓库上传本地镜像
[[email protected] docker]# docker push 10.71.11.11:5000/liulei123/registry
The push refers to a repository [10.71.11.11:5000/liulei123/registry]
43e653f84b79: Pushed
latest: digest: sha256:191c883e479a7da2362b2d54c0840b2e8981e5ab62e11ab925abf8808d3d5d44 size: 529
4.现在就可以从本地私有仓库下载镜像了
[[email protected] docker]# docker pull 10.71.11.11:5000/liulei123/registry
Using default tag: latest
Trying to pull repository 10.71.11.11:5000/liulei123/registry ...
latest: Pulling from 10.71.11.11:5000/liulei123/registry
Digest: sha256:191c883e479a7da2362b2d54c0840b2e8981e5ab62e11ab925abf8808d3d5d44
Status: Image is up to date for 10.71.11.11:5000/liulei123/registry:latest
以上是搭建本地 registry 步骤和排错过程。当然 registry 也支持认证,https 安全传输等特性,具体可以参考官方文档 https://docs.docker.com/registry/configuration/
原文地址:http://blog.51cto.com/liuleis/2105604