hualinux 进阶 1.4: centos8 docker CE 入门及安装(三)建立docker仓库

目录

一、docker仓库

二、建立公共仓库

2.1 注册并创建仓库

2.2 使用docker命令登录

2.3 修改镜像的 repository 使之与 Docker Hub 账号匹配

2.4 上传镜像到hub.docker.com

2.5 docker hub中查看镜像

三、建立私有仓库

3.1 说明

3.2 安装及运行registry v2

3.2.1 下载registry

3.2.2 运行registry

3.2.3 修改docker配置

3.3 上传docker镜像到私有仓库registry中

3.3.1 添加新的docker镜像

3.3.2 上传docker镜像到私在仓库registry中

3.4 查看私有仓库registry

3.5 registry添加ssl证书变成https

附录一、docker registry v2 api


上一章《hualinux 进阶 1.3: centos8 docker CE 入门及安装(二) 构建docker镜像》讲了怎样制作自己的docker镜像,制作好的镜像肯定要上传到仓库保存,将是这章节的内容,将以公有仓库和自己搭建的私有仓库讲解

一、docker仓库

仓库(Repository)是集中存放镜像的地方。

一个容易混淆的概念是注册服务器(Registry)。实际上注册服务器是管理仓库的具体服务器,每个服务器上可以有多个仓库,而每个仓库下面有多个镜像。从这方面来说,仓库可以被认为是一个具体的项目或目录。例如对于仓库地址dl.dockerpool.com/ubuntu 来说, dl.dockerpool.com 是注册服务器地址,ubuntu 是仓库名。

大部分时候,并不需要严格区分这两者的概念。

目前Docker官方维护了一个公共仓库,链接为https://hub.docker.com/,大部分需求,都可以通过在 Docker Hub 中直接下载镜像来实现。

用户可以将自己的镜像保存到 Docker Hub 免费的 repository 中。如果不希望别人访问自己的镜像,也可以购买私有 repository。

用户无需登录即可通过 docker search 命令来查找官方仓库中的镜像,并利用docker pull 命令来将它下载到本地。就像上面的例子那样。

 

二、建立公共仓库

2.1 注册并创建仓库

建立公共仓库去https://hub.docker.com上注册一个账号,我的为hualinux,注册好还要创建一个仓库

2.2 使用docker命令登录

[root@vm82 ~]# docker login -u hualinux
Password:    #输入正确的密码
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded    #登录成功提示

2.3 修改镜像的 repository 使之与 Docker Hub 账号匹配

根据docker hub官方说明文档得知repo上传的格式为:

docker tag <existing-image> <hub-user>/<repo-name>[:<tag>]

所以你们需要用docker tag修改一下本地镜像名,我们通过 docker tag 命令重命名镜像。 

[root@vm82 ~]# docker tag --help

Usage:	docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
[root@vm82 ~]# 
[root@vm82 ~]# docker tag hua:nginx hualinux/test:nginx1.18
[root@vm82 ~]# docker images  hualinux/test
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hualinux/test       nginx1.18           7887ed045e97        20 hours ago        132MB
 
 

2.4 上传镜像到hub.docker.com

根据docker hub官方说明文档得知repo上传的格式为:

 通过 docker push 将镜像上传到 Docker Hub,国内访问很卡的话,需要尝试N次方可成功!!

[root@vm82 ~]# docker push hualinux/hua:nginx
The push refers to repository [docker.io/hualinux/hua]
f23e7e9ff603: Pushed 
89a535833e0a: Mounted from library/nginx 
8e8c644f0c81: Mounted from library/nginx 
a9a89498d0f0: Mounted from library/nginx 
13cb14c2acd3: Mounted from library/nginx 
nginx: digest: sha256:ccbd4967b6e621891ff8a1daef28304c0fd6232fc8750bfc7ed0116db6200729 size: 1362

2.5 docker hub中查看镜像

登录https://hub.docker.com,在Public Repository 中就可以看到上传的镜像。

 

三、建立私有仓库

3.1 说明

Docker Hub 虽然非常方便,但还是有些限制,比如:

  1. 需要 internet 连接,而且下载和上传速度慢。
  2. 上传到 Docker Hub 的镜像任何人都能够访问,虽然可以用私有 repository,但不是免费的。
  3. 安全原因很多组织不允许将镜像放到外网。

解决方案就是搭建本地的 Registry。

Docker 已经将 Registry 开源了,同时在 Docker Hub 上也有官方的镜像 registry。下面我们就在 Docker 中运行自己的 registry。

 

3.2 安装及运行registry v2

#docker-registry v2版本即是docker-distribution代替,为了简单方便起见,我们直接安装docker-registry,省去了配置的麻烦。

#建立存放镜像数据目录
mkdir -p /disk1/myregistry

3.2.1 下载registry

目前最新版本为2.7

docker pull registry

3.2.2 运行registry

根据 docker registry官网描述,得知如果你默认运行命令为:

上面的  --restart always 表示 如果docker重启,容器也会跟着docker重启而启动,我这里不使用它,为了安全考虑,我指定内网IP地址,这个IP地址是本服务器的IP地址,你们绑定的时候记得修改成自己本地服务器的IP!

#使用-v参数把本地的目录映射到docker中
docker run -d -p 192.168.3.82:5000:5000 --name hua-reg -v /disk1/myregistry:/var/lib/registry registry
#查看docker
docker ps
netstat -altnp|grep 5000

3.2.3 修改docker配置

registry默认使用的是https,我现在使用的是http,所以要在配置文件中指定http,如果不指定http的话,到后面上传会报下面错误:

Get https://192.168.3.82:5000/v2/: http: server gave HTTP response to HTTPS client

打开/etc/docker/daemon.json,添加

"insecure-registries": ["http://192.168.3.82:5000"]
因为之前有一个华为云了,所以有几个json的情况下,要用逗号分隔

#重启docker和registry

systemctl restart docker
docker start hua-reg

 

3.3 上传docker镜像到私有仓库registry中

在上传之前修改一个镜像的格式,如下:

docker tag <existing-image> <registry address:port>/<repo-name>[:<tag>]

3.3.1 添加新的docker镜像

[root@vm82 ~]# docker tag hua:nginx 192.168.3.82:5000/nginx:1.18
[root@vm82 ~]# docker images 192.168.3.82:5000/nginx
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
192.168.3.82:5000/nginx   1.18                7887ed045e97        23 hours ago        132MB

PS:原来的hua:nginx 镜像并没有被修改,只是添加多了一个镜像而已

3.3.2 上传docker镜像到私在仓库registry中

[root@vm82 ~]# docker push 192.168.3.82:5000/nginx:1.18 
The push refers to repository [192.168.3.82:5000/nginx]
f23e7e9ff603: Pushed 
89a535833e0a: Pushed 
8e8c644f0c81: Pushed 
a9a89498d0f0: Pushed 
13cb14c2acd3: Pushed 
1.18: digest: sha256:ccbd4967b6e621891ff8a1daef28304c0fd6232fc8750bfc7ed0116db6200729 size: 13

 

3.4 查看私有仓库registry

#根据docker registry v2 api接口列表,请看“附录一”得知查看镜像方式如下:

#v1版本为curl 192.168.3.76:5000/v1/search,现在v2版本用_catalog

http://192.168.3.82:5000/v2/_catalog

我查看一下这个仓库的镜像列表看一下

http://192.168.3.82:5000/v2/nginx/tags/list

没毛病吧,那么我想下载它呢,先把本地的删除看一下

docker rmi 192.168.3.82:5000/nginx:1.18 

按照语法

 <registry address:port>/<repo-name>[:<tag>]

得192.168.3.82:5000/nginx/1.18,再在前面加一个docker pull命令,最终得

[root@vm82 ~]# docker pull 192.168.3.82:5000/nginx:1.18 
1.18: Pulling from nginx
Digest: sha256:ccbd4967b6e621891ff8a1daef28304c0fd6232fc8750bfc7ed0116db6200729
Status: Downloaded newer image for 192.168.3.82:5000/nginx:1.18
192.168.3.82:5000/nginx:1.18

 

3.5 registry添加ssl证书变成https

前面的registry私在仓库使用的是http,如果要使用https就要添加证书

ssl证书一般是去专业的机构购买如:

沃通(https://www.wosign.com/)、Symantec(www.symantec.com/zh/cn/ssl-certificates)、

GlobalSigncn.globalsign.com)、entrustwww.entrust.com.cn)等,当然也可以手工生成不过会提示不安全,因为证书机构不承认的

如果为了测试证书生成可以使用如下命令:

openssl req -newkey rsa:2048 -x509 -nodes -days 3560 -out hualinux.crt -keyout hualinux.key

生成证书之后,还需要重启运行一个registry容器,需要添加下面几个参数

-u root \
-v /etc/docker/certs:/certs \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/hualinux.crt \
-e REGISTRY_HTTP_TLS_KEY=/certs/hualinux.key \

比如上面例子可以为:

docker run -d -p 192.168.3.82:5000:5000 --name hua-reg \
-u root \
-v /disk1/myregistry:/var/lib/registry registry
-v /etc/docker/certs:/certs \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/hualinux.crt \
-e REGISTRY_HTTP_TLS_KEY=/certs/hualinux.key \
registry

 

附录一、docker registry v2 api

docker registry v2 api描述和使用docker-registry v2

method

path

Entity

Description

GET

/v2/

Base

Check that the endpoint implements Docker Registry API V2.

GET

/v2/\<image>/tags/list

Tags

Fetch the tags under the repository identified by name.

GET

/v2/\<image>/manifests/\<referevce>

Manifest

Fetch the manifest identified by nameand referencewhere referencecan be a tag or digest. A HEADrequest can also be issued to this endpoint to obtain resource information without receiving all data.

put

/v2/\<image>/manifests/\<referevce>

Manifest

Put the manifest identified by nameand referencewhere referencecan be a tag or digest.

delete

/v2/\<image>/manifests/\<reference>

Manifest

Delete the manifest identified by nameand reference. Note that a manifest can only be deleted by digest.

GET

/v2/\<image>/blobs/\<digest>

Blob

Retrieve the blob from the registry identified bydigest. A HEADrequest can also be issued to this endpoint to obtain resource information without receiving all data.

DELETE

/v2/\<image>/blobs/\<digest>

Blob

Delete the blob identified by nameand digest

POST

/v2/\<image>/blobs/uploads/

Initiate

Blob

Upload

Initiate a resumable blob upload. If successful, an upload location will be provided to complete the upload. Optionally, if thedigest parameter is present, the request body will be used to complete the upload in a single request.

GET

/v2/\<image>/blobs/uploads/\<uuid>

Blob

Upload

Retrieve status of upload identified byuuid. The primary purpose of this endpoint is to resolve the current status of a resumable upload.

PATCH

/v2/\<image>/blobs/uploads/\<uuid>

Blob

Upload

Upload a chunk of data for the specified upload.

PUT

/v2/\<image>/blobs/uploads/\<uuid>

Blob

Upload

Complete the upload specified by uuid, optionally appending the body as the final chunk.

DELETE

/v2/\<image>/blobs/uploads/\<uuid>

Blob

Upload

Cancel outstanding upload processes, releasing associated resources. If this is not called, the unfinished uploads will eventually timeout.

GET

         /v2/_catalog       Catalog

Catalog

Retrieve a sorted, json list of repositories available in the registry.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值