1. registry创建
- docker官方提供一个搭建私有仓库的镜像
registry
,将镜像pull下来,端口设置5000。
docker pull registry:2
- registry服务器默认会将上传的镜像保存在容器中的
/var/lib/registry
,我们将主机的/home/xxx/docker/opt/registry
目录挂载到该目录。
docker run -d -v /home/xxx/docker/opt/registry:/var/lib/registry -p 5000:5000 --name myregistry registry:2
- 浏览器访问
http://127.0.0.1:5000/v2
, 出现下面情况说明registry运行正常。
- 将本地的镜像push到registry中,采用
busybox
进行测试。
docker pull busybox
docker tag busybox localhost:5000/busybox:latest
docker push localhost:5000/busybox:latest
通过curl验证一下
curl -X GET http://127.0.0.1:5000/v2/_catalog
出现如下结果:
{"repositories":["busybox"]}
2. registry 中的镜像删除
- 进入registry
docker exec -it registry sh
- 查看config.yml, 在配置文件中
storage
配置中增加delete配置项。
vi /etc/docker/registry/config.yml
version: 0.1
log:
fields:
service: registry
storage:
delete:
enabled: true
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: /var/lib/registry
http:
addr: :5000
headers:
X-Content-Type-Options: [nosniff]
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3
- 查看当前仓库数据的大小
du -sch /var/lib/registry
892.0K /var/lib/registry
892.0K total
- 删除镜像的api如下:
DELETE /v2/<name>/manifests/<reference>
name: 镜像名称
reference: 镜像对应的sha256值
- 获取镜像信息, 如何不加
-I
digest的值会出错。
** 加-I
的情况如下:
curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I -X GET http://127.0.0.1:5000/v2/busybox/manifests/latest
HTTP/1.1 200 OK
Content-Length: 527
Content-Type: application/vnd.docker.distribution.manifest.v2+json
Docker-Content-Digest: sha256:ee44b399df993016003bf5466bd3eeb221305e9d0fa831606bc7902d149c775b
Docker-Distribution-Api-Version: registry/2.0
Etag: "sha256:ee44b399df993016003bf5466bd3eeb221305e9d0fa831606bc7902d149c775b"
X-Content-Type-Options: nosniff
Date: Tue, 24 Nov 2020 07:52:55 GMT
** 不加-I
情况如下:
curl --header "Accept:application/vnd.docker.distribution.manifest.v2+json" -X GET http://127.0.0.1:5000/v2/busybox/manifests/latest
{
"schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"config": {
"mediaType": "application/vnd.docker.container.image.v1+json",
"size": 1457,
"digest": "sha256:dc3bacd8b5ea796cea5d6070c8f145df9076f26a6bc1c8981fd5b176d37de843"
},
"layers": [
{
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 764624,
"digest": "sha256:5f5dd3e95e9fd44603cbb8968144999b730545267ce74d9b442f8f7f7827f776"
}
]
}
我们也可以在主机的/home/xxx/docker/opt/registry
中查看:
- 删除镜像
curl -I -X DELETE http://127.0.0.1:5000/v2/busybox/manifests/sha256:ee44b399df993016003bf5466bd3eeb221305e9d0fa831606bc7902d149c775b
出现Not Found
命令行出错导致的。
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Tue, 24 Nov 2020 07:08:45 GMT
Content-Length: 19
- 注意:配置完config.yml不要忘记重启一下registry
docker restart myregistry
HTTP/1.1 202 Accepted
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Tue, 24 Nov 2020 07:36:09 GMT
Content-Length: 0
- 如果误删了
sha256:dc3bacd8b5ea796cea5d6070c8f145df9076f26a6bc1c8981fd5b176d37de843
,查看数据大小。
888.0K /var/lib/registry
888.0K total
*删除sha256:ee44b399df993016003bf5466bd3eeb221305e9d0fa831606bc7902d149c775b
,查看数据大小。(只是删除了元数据)
/ # du -sch /var/lib/registry/
860.0K /var/lib/registry/
860.0K total
-
查看registry数据目录
垃圾回收
- 进行容器执行垃圾回收命令
/var/lib/registry # registry garbage-collect /etc/docker/registry/config.yml
busybox
0 blobs marked, 3 blobs and 0 manifests eligible for deletion
blob eligible for deletion: sha256:5f5dd3e95e9fd44603cbb8968144999b730545267ce74d9b442f8f7f7827f776
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/5f/5f5dd3e95e9fd44603cbb8968144999b730545267ce74d9b442f8f7f7827f776 go.version=go1.11.2 instance.id=d1fcb128-f544-452d-b2a6-caed701bd76f service=registry
blob eligible for deletion: sha256:dc3bacd8b5ea796cea5d6070c8f145df9076f26a6bc1c8981fd5b176d37de843
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/dc/dc3bacd8b5ea796cea5d6070c8f145df9076f26a6bc1c8981fd5b176d37de843 go.version=go1.11.2 instance.id=d1fcb128-f544-452d-b2a6-caed701bd76f service=registry
blob eligible for deletion: sha256:ee44b399df993016003bf5466bd3eeb221305e9d0fa831606bc7902d149c775b
INFO[0000] Deleting blob: /docker/registry/v2/blobs/sha256/ee/ee44b399df993016003bf5466bd3eeb221305e9d0fa831606bc7902d149c775b go.version=go1.11.2 instance.id=d1fcb128-f544-452d-b2a6-caed701bd76f service=registry
- 查看数据大小
92.0K /var/lib/registry/
92.0K total
-
查看registry数据目录