上一章节,讲解了利用
Dockerfile
和commit
进行自定义镜像的构建。大部分时候,公司运维或者实施部门在构建了符合公司业务的镜像环境后,通常上不会上传到公共资源库的。这就须要本身搭建一个私有仓库,来存放本身的镜像数据,同时也能够存储其余镜像,方便拉取,比较国内的网络环境链接仍是比较慢的。因此,本章节介绍下如何构建私有仓库。linux
Docker
官方提供了Registry
镜像,方便构建本身的私有仓库。因此,接下来说解下私有仓库服务的安装。nginx
docker pull registry
因为Registry
是一个镜像,运行后若咱们删除了容器,里面的资源就都丢失了,因此咱们在运行时,指定一个资源的挂载目录,映射到宿主的一个目录下,这样资源就不会丢失了。git
docker run -d -v /opt/docker/registry:/var/lib/registry -p 1888:5000 --restart=always --name registry registry:latest
提示:restart=always
是指docker
重启时此容器自动启动。 启动后,就能经过:http://宿主IP:1888/v2 ,访问了。github
咱们在本地建立一个镜像,同时推送到仓库上。spring
# 利用tag 标记一个新镜像 docker tag hello-world 127.0.0.1:1888/demo/hello-world:latest # 推送镜像 docker push 127.0.0.1:1888/demo/hello-world:latest
此时,访问私有仓库地址:http://宿主IP:1888/v2/_catalog, 便可看见推送的镜像信息了。docker
直接使用127.0.0.1
或者local
时,是没有进行安全检验的。当咱们使用外部的ip地址推送时,Registr
y为了安全性考虑,默认是须要https
证书支持的。json
错误提示:api
[root@izbp16chpwsnff41nrjtfhz ~]# docker push 47.98.238.247:1888/demo/hello-world:v1 The push refers to repository [47.98.238.247:1888/demo/hello-world] Get https://47.98.238.247:1888/v2/: http: server gave HTTP response to HTTPS client
搜索相关资料后,有两种方案:一种是经过daemon.json
配置一个insecure-registries
属性;另外一种就直接配置一个https
的证书了。https
的相关配置不在本章节讲解范围了,这里就不示例了,最稳妥的作法能够是:利用nginx
进行https
证书支持,而后代理到Registry
服务。有兴趣的能够本身动手实现下。安全
针对第一种作法,这里示例下:springboot
{ "insecure-registries": ["实际的ip:端口"] }
以后重启Docker
,
systemctl restart docker
以后再进行推送就能推送成功了
docker push 47.98.238.247:1888/demo/hello-world:v1
查看下镜像的版本列表就能知道是否上传成功。
仓库很大,要是没有一套管理规则,也是很头疼的事情。因此接下来,讲解下如何管理Registry。
如下转至:https://cloud.tencent.com/developer/article/1116799 。详细可点击查看。
方法 | 路径 | 分类 | 描述 |
---|---|---|---|
GET | /v2/ | Base | 检查是否支持2.0接口 |
GET | /v2/${image}/tags/list | Tags | 获取镜像标签列表 |
GET | /v2/${image}/manifests/<referevce> | Manifest | 获取镜像主要信息 |
PUT | /v2/${image}/manifests/<referevce> | Manifest | 修改镜像主要信息 |
DELETE | /v2/${image}/manifests/<reference> | Manifest | 删除镜像的主要信息 |
GET | /v2/${image}/blobs/<digest> | Blob | 得到镜像层 |
DELETE | /v2/${image}/blobs/<digest> | Blob | 删除镜像层 |
POST | /v2/${image}/blobs/uploads/ | Initiate Blob Upload | 开始分块上传 |
GET | /v2/${image}/blobs/uploads/<uuid> | Blob Upload | 得到分块上传的速度 |
PATCH | /v2/${image}/blobs/uploads/<uuid> | Blob Upload | 分块上传数据 |
PUT | /v2/${image}/blobs/uploads/<uuid> | Blob Upload | 完成上传 |
DELETE | /v2/${image}/blobs/uploads/<uuid> | Blob Upload | 取消上传 |
GET | /v2/_catalog | Catalog | 得到镜像列表 |
基本上的管理都是经过以上的api接口方式了,你们可自行尝试下。
这里就简单示例下比较经常使用的也比较麻烦的删除镜像
方法。这里直接使用curl
也可以使用POSTMAN
等api尝试工具进行.
注意:因为默认是不容许删除镜像的(提示方法不支持),因此须要进行配置,开启删除功能。因此咱们在启动时,直接设置参数(REGISTRY_STORAGE_DELETE_ENABLED=true
):
docker run -d -v /opt/docker/registry:/var/lib/registry -p 1888:5000 --restart=always --name registry -e "REGISTRY_STORAGE_DELETE_ENABLED=true" registry:latest
或者可设置/etc/docker/registry/config.yml
,加入storage.delete.enabled
设置为true
(yaml格式)
version: 0.1 log: fields: service: registry storage: cache: blobdescriptor: inmemory filesystem: rootdirectory: /var/lib/registry delete: enable: true http: addr: :5000 headers: X-Content-Type-Options: [nosniff] health: storagedriver: enabled: true interval: 10s threshold: 3
以后重启(restart)容器便可。
提示:对于config.yml
配置文件,里面能够设置不少的属性信息,具体属性可查看官网说明:https://docs.docker.com/registry/configuration/
删除镜像:
curl -I -X --header "Accept: application/vnd.docker.distribution.manifest.v2+json" DELETE http://127.0.0.1:1888/v2/demo/hello-world/manifests/sha256:aca41a608e5eb015f1ec6755f490f3be26b48010b178e78c00eac21ffbe246f1
或者利用get获取对应镜像信息:
curl -X GET \ http://47.98.238.247:1888/v2/demo/hello-world/manifests/v1 \ -H 'Cache-Control: no-cache'
再或者能够直接根据存储目录查看(因为挂载宿主目录,因此咱们直接在宿主机查看):
/opt/docker/registry/docker/registry/v2/repositories/demo/hello-world/_manifests/tags/v2/index/sha256/link
利用cat命令 或者 直接打开文件查看:
题外话:关于Postman
用法,可点击查看
删除前:
资源库大小:
执行删除后,发现tag
已经为null了:
再看看大小,是没有变化的:
由于只删除了tag
,镜像文件没有删除。因此咱们进入容器执行下回收命令:
/ # registry garbage-collect /etc/docker/registry/config.yml demo/hello-world test/hello-world test/hello-world: marking manifest sha256:aca41a608e5eb015f1ec6755f490f3be26b48010b178e78c00eac21ffbe246f1 test/hello-world: marking blob sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34 test/hello-world: marking blob sha256:9db2ca6ccae029dd195e331f4bede3d2ea2e67e0de29d6a0f8c1572e70f32fa7 3 blobs marked, 1 blobs eligible for deletion blob eligible for deletion: sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4 INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/a3/a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4 go.version=go1.7.6 instance.id=de550dcd-7e54-4279-a9e4-d3609c83b11b
这个时候就已经回收了。目前貌似没有自动回收机制。
能够看见大小发生了变化了,说明镜像已经被清理了。也能够去刚刚查看sha256目录
查看,里面的信息已经被删除了
最后想说,利用命令去删除之类的太麻烦了,如今有不少开源的工具能够管理仓库。你们能够自行搜索下。这里能够看看这个开源的删除资源的工具类:https://github.com/burnettk/delete-docker-registry-image,具体使用方法有具体说明。
官方的文档仍是很齐全的,建议你们仍是直接去官网查看相关资料。
本章节主要是介绍如何利用
Registry
进行私有仓库的建立及管理。对于单纯的api
调用而言,有点麻烦,能够上网找写批量的脚本,简化步骤下。因为对linux命令不是很熟悉,你们仍是自行谷歌吧。正常而言,仍是应该有个管理的UI界面会更方便点。在后面讲解Docker可视化界面管理
时再来详细说明下,由于本人也没有搭建过,后面会为了这个章节找些资料,而后分享给你们。敬请期待!
若文中有错误或者遗漏之处,还望指出,共同进步!
499452441
lqdevOps
我的博客:http://blog.lqdev.cn