Ubuntu18搭建Docker Registry服务

下载镜像
首先我们将该仓库pull下来:

wxs@ubuntu:~$ docker pull registry

配置配置文件
默认情况下的registry不支持删除镜像,我们需要自己写配置文件,在容器启动时候映射进去,在/data目录下编写config.yml文件:

注意:该文件必须使用空格分隔,不能使用tab符。

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

配置完成后就可以启动容器,注意映射config.yml文件的位置:

docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry  -v /data/config.yml:/etc/docker/registry/config.yml  registry

执行命令curl -X GET localhost:5000/v2/_catalog就能查看registry中的所有镜像:

wxs@ubuntu:~$ curl -X GET localhost:5000/v2/_catalog

配置宿主机
我安装了Docker Registry服务的主机IP为192.168.100.183,我现在要在宿主机中push镜像。

(1)默认情况下只支持HTTPS协议下的Registry连接,因此首先我们要让宿主机信任Registry服务器。

编辑宿主机中的/etc/docker/daemon.json文件,如果没有就新建:

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

注意在第三行中指定了Registry服务器的地址,和Registry暴露的端口号。

(2)修改/lib/systemd/system/docker.service,载入配置文件

在配置文件中添加一行:EnvironmentFile=-/etc/docker/daemon.json

(3)重启Docker服务

上传镜像
我们以上传hello-world镜像为例:

[wxs@bogon ~]$ docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
nginx                 latest              5699ececb21c        7 days ago          109MB
portainer/portainer   latest              7afb7abcfe5f        8 days ago          57MB
rancher/server        stable              85b3b338d0be        4 weeks ago         1.08GB
hello-world           latest              e38bc07ac18e        2 months ago        1.85kB
rancher/agent         v1.2.10             6023e1a77132        3 months ago        237MB

(1)首先我们修改要上传的镜像,名称格式为:Registry服务器地址/镜像名:tag的形式。

即将 library/hello-world:latest修改为192.168.100.183/hello-world:latest:

[wxs@bogon ~]$ docker tag library/hello-world:latest 192.168.100.183:5000/hello-world:latest
[wxs@bogon ~]$ docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
nginx                              latest              5699ececb21c        7 days ago          109MB
portainer/portainer                latest              7afb7abcfe5f        8 days ago          57MB
rancher/server                     stable              85b3b338d0be        4 weeks ago         1.08GB
192.168.100.183:5000/hello-world   latest              e38bc07ac18e        2 months ago        1.85kB
hello-world                        latest              e38bc07ac18e        2 months ago        1.85kB
rancher/agent                      v1.2.10             6023e1a77132        3 months ago        237MB

(2)然后push该镜像:

[wxs@bogon ~]$ docker push 192.168.100.183:5000/hello-world
The push refers to a repository [192.168.100.183:5000/hello-world]
2b8cbd0846c5: Pushed 
latest: digest: sha256:d5c74e6f8efc7bdf42a5e22bd764400692cf82360d86b8c587a7584b03f51520 size: 524

(3)回到Registry服务器,重新执行查看镜像的命令:

wxs@ubuntu:~$ curl -X GET localhost:5000/v2/_catalog
{"repositories":["hello-world"]}

可以看到刚刚宿主机上传的镜像已经拿到了。

查看命令
刚刚我们使用了一个API,查看Registry服务器上的所有镜像,即/v2/_catalog。还有一些其他查看命令,如下:

列出某个镜像所有tag: 
/v2/镜像名/tags/list,如:/v2/hello-world/tags/list

列出某个tag的详细信息 
/v2/镜像名/manifests/tag号,如:/v2/hello-world/manifests/latest

获取某个tag的digest 
curl --header "Accept: application/vnd.docker.distribution.manifest.v2+json" -I -X HEAD 192.168.100.183:5000/v2/hello-world/manifests/latest

删除镜像
删除镜像需要指定镜像名和digest(上传时得到的),使用命令如下:

DELETE /v2/镜像名/manifests/镜像digest

wxs@ubuntu:~$ curl -I -X DELETE localhost:5000/v2/hello-world/manifests/sha256:d5c74e6f8efc7bdf42a5e22bd764400692cf82360d86b8c587a7584b03f51520
HTTP/1.1 202 Accepted
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Wed, 04 Jul 2018 06:49:17 GMT
Content-Length: 0
Content-Type: text/plain; charset=utf-8

HTTP 202代表成功了,重新查看镜像和TAG,已经显示删除了:

wxs@ubuntu:~$ curl -X GET localhost:5000/v2/_catalog
{"repositories":["hello-world"]}
wxs@ubuntu:~$ curl -X GET localhost:5000/v2/hello-world/tags/list
{"name":"hello-world","tags":null}

但是实际上只是逻辑删除,文件依然存在,如果要彻底删除,需要进行垃圾回收。

进入到Registry服务内部:

docker exec -it 9d4d2055fb3a sh

执行命令:

registry garbage-collect /etc/docker/registry/config.yml


原文:https://blog.csdn.net/yuanlaijike/article/details/80912801 
 

猜你喜欢

转载自blog.csdn.net/Big_Baby_Z/article/details/90109825