(一)registry定义和功能
1、registry注册中心是镜像存储的位置。
2、功能:集中的存储、分发镜像的服务,docker registry就是这样的服务。
3、Docker Registry组成:由三个部分组成:index,registry,registry client。

  • Index:是负责登录、负责认证、负责存储镜像信息和负责对外显示的外部实现。
  • repository:是负责存储镜像的内部实现 。 每个仓库可以包含多个 标签(Tag);每个标签对应一个镜像。通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。 我们可以通过 <仓库名>:<标签> 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 latest 作为默认标签
  • Registry Client:是docker客户端。

(二)、docker registry包括公共的 docker registry 和私有的docker registry

2.1 Docker Registry 公开服务是开放给用户使用、允许用户管理镜像的 Registry 服务。一般这类公开服务允许用户免费上传、下载公开的镜像,并可能提供收费服务供用户管理私有镜像。 最常用的是官方的Docker Hub也是默认的Registry,并拥有高质量的官方镜像。国内的是阿里云

2.2、私有 Docker Registry。用户还可以在本地搭建私有 Docker Registry。私有仓库优势: 1、节省带宽。 2、更加安全。 3、内部镜像统一管理。

备注:拉取镜像仓库镜像的格式:
 docker pull [:]/[/]:
 registry:仓库服务器地址:不指定默认是docker hub
 port:端口;默认是443,因为是https协议
 namespace:名称空间,指是哪个用户的仓库,如果是顶层仓库,可省
 name:仓库名
 tag:标签名;默认是latest版本

(三)、搭建私有仓库
1、下载

[root@otrs004097 ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
c87736221ed0: Pulling fs layer 
1cc8e0bb44df: Pulling fs layer 
54d33bcb37f5: Pulling fs layer 
e8afc091c171: Waiting 
b4541f6d3db6: Waiting 
latest: Pulling from library/registry
c87736221ed0: Pull complete 
1cc8e0bb44df: Pull complete 
54d33bcb37f5: Pull complete 
e8afc091c171: Pull complete 
b4541f6d3db6: Pull complete 
Digest: sha256:8004747f1e8cd820a148fb7499d71a76d45ff66bac6a29129bfdbfdc0154d146
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest

2、通过registry镜像启动一个容器

[root@otrs004097 ~]# docker run -d -v /opt/docker-registry:/var/lib/registry -p 5000:5000 --name registry registry
6c01265a36274493fd362ab76819f262e344f8259b9a7b3ae056140ec11d1ec8

-itd:在容器中打开一个伪终端进行交互操作,并在后台运行; 
-v:把宿主机的/data/registry目录绑定 到 容器/var/lib/registry目录(这个目录是registry容器中存放镜像文件的目录),来实现数据的持久化;
-p:映射端口;访问宿主机的5000端口就访问到registry容器的服务了;
--restart=always:这是重启的策略,假如这个容器异常退出会自动重启容器; 
--name registry:创建容器命名为registry,你可以随便命名; 
registry:latest:这个是刚才pull下来的镜像; 

3、查看运行状况

root@otrs004097 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
6c01265a3627        registry            "/entrypoint.sh /etc…"   4 seconds ago       Up 2 seconds        0.0.0.0:5000->5000/tcp   registry

4、在浏览器输入改地址:http://192.168.4.97:5000/v2/,说明正常
{}

5、上传出现报错的时候 Get https://192.168.10.102:5000/v1/_ping: http: server gave HTTP response to HTTPS client
解决方法有两种:
方法一:修改注册中心文件/etc/docker/daemon.json,并重启docker

[root@otrs004097 ~]# vim /etc/docker/daemon.json

{ "registry-mirrors": ["http://hub-mirror.c.163.com"], "insecure-registries": ["192.168.4.97:5000"] }

[root@otrs004097 ~]# systemctl restart docker 
注释:         insecure-registries----->开放注册https协议         registry-mirrors----->仓库源

方法二、通过创建证书自带的TLS认证

5.1、生成自签名证书

[root@otrs004097 ~]# mkdir -p /opt/docker/registry/certs
[root@otrs004097 ~]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout /opt/docker/registry/certs/domain.key -x509 -days 365 -out /opt/docker/registry/certs/domain.crt
Generating a 4096 bit RSA private key
.....................++
.....++
writing new private key to '/opt/docker/registry/certs/domain.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:shanghai
Locality Name (eg, city) [Default City]:shanghai
Organization Name (eg, company) [Default Company Ltd]:kj
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:
Email Address []:
[root@otrs004097 ~]# ll /opt/docker/registry/certs/
total 8
-rw-r--r-- 1 root root 1944 Aug 20 11:04 domain.crt
-rw-r--r-- 1 root root 3272 Aug 20 11:04 domain.key
5.2.创建带有TLS认证的registry容器
[root@otrs004097 ~]#  docker run -d --name registry2 -p 5000:5000  -v /opt/docker-registry/:/var/lib/registry -v /opt/docker/registry/certs:/certs -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key registry:2 
0ae56ecfbcd015e59503f01ec8e3f52143753d1137aab530a823d1461b989a01

5.3、通过浏览器输入进行访问
6.[root@otrs004097 certs.d]# curl -X GET https://192.168.4.97:5000/v2/ -k
{}

Docker入门(三)Registry私有仓库搭建并认证_第1张图片

5.4、在其他服务器进行上传镜像。
[root@DEV004019 ~]# docker push 192.168.4.97:5000/lqb_nginx:v1 
The push refers to repository [192.168.4.97:5000/lqb_nginx]
fe6a7a3b3f27: Pushed 
d0673244f7d4: Pushed 
d8a33133e477: Pushed 
v1: digest: sha256:dc85890ba9763fe38b178b337d4ccc802874afe3c02e6c98c304f65b08af958f size: 948
5.5、查看上传的镜像
[root@otrs004097 certs.d]# curl -X GET https://192.168.4.97:5000/v2/_catalog -k
{"repositories":["lqb_nginx"]}

Docker入门(三)Registry私有仓库搭建并认证_第2张图片

6、把需要上传的镜像,修改tag,为本地镜像,然后push。

[root@otrs004097 ~]# docker tag lqb2:v2.0 localhost:5000/lqb2v1:v1
[root@otrs004097 ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
localhost:5000/yz         v1.0                0011d86948f4        26 hours ago        346MB
localhost:5000/yzv1       v1                  0011d86948f4        26 hours ago        346MB
lqb2                      v2.0                62226ff8a5bc        27 hours ago        346MB
localhost:5000/lqb2v1     v1                  62226ff8a5bc        27 hours ago        346MB
t1                        latest              ee097386456f        27 hours ago        380MB
[root@otrs004097 ~]# docker push localhost:5000/lqb2v1:v1 
The push refers to repository [localhost:5000/lqb2v1]
fd214f756b32: Mounted from yzv1 
v1: digest: sha256:dc7415d74223057a91d6525473e0aa7e1a8edd89ea63e9ec2166b2deeccb4fe2 size: 529

7、打开浏览器输入:以下,会显示上传的镜像列表

http://192.168.4.97:5000/v2/_catalog
{"repositories":["lqb2v1","myubuntu","yzv1"]}

[root@otrs004097 ~]# curl -XGET HTTP://192.168.4.97:5000/v2/_catalog 
{"repositories":["lqb2v1","myubuntu","yzv1"]}

8、测试下载镜像,首先删除镜像,然后在下载

[root@otrs004097 ~]# docker rmi localhost:5000/lqb2v1:v1 
Untagged: localhost:5000/lqb2v1:v1
Untagged: localhost:5000/lqb2v1@sha256:dc7415d74223057a91d6525473e0aa7e1a8edd89ea63e9ec2166b2deeccb4fe2
[root@otrs004097 ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
localhost:5000/yzv1       v1                  0011d86948f4        27 hours ago        346MB
localhost:5000/yz         v1.0                0011d86948f4        27 hours ago        346MB
lqb2                      v2.0                62226ff8a5bc        27 hours ago        346MB
t1                        latest              ee097386456f        27 hours ago        380MB
lqb1                      v1.0                add4aac9e719        27 hours ago        369MB

[root@otrs004097 ~]# docker pull localhost:5000/lqb2v1:v1 
v1: Pulling from lqb2v1
Digest: sha256:dc7415d74223057a91d6525473e0aa7e1a8edd89ea63e9ec2166b2deeccb4fe2
Status: Downloaded newer image for localhost:5000/lqb2v1:v1
localhost:5000/lqb2v1:v1
[root@otrs004097 ~]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
localhost:5000/yz         v1.0                0011d86948f4        27 hours ago        346MB
localhost:5000/yzv1       v1                  0011d86948f4        27 hours ago        346MB
lqb2                      v2.0                62226ff8a5bc        27 hours ago        346MB
localhost:5000/lqb2v1     v1                  62226ff8a5bc        27 hours ago        346MB

pull和push都正常上传下载了
备注:
docker push :5000/:;上传镜像至私有仓库
docker pull :5000/:;从私有仓库pull镜像

docker run -d --name registry2 -p 5000:5000 -v /opt/docker-registry/:/var/lib/registry -v /opt/dcerts/:/certs -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key registry:2