docker registry搭建私有仓库

公有镜像仓库

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

# 可以在https://hub.docker.com 免费注册一个Docker账号
docker login

docker search ubuntu
docker pull ubuntu:22.04

# 将自己镜像推送到docker hub
docker push <Local IMAGE ID> xstar/ubuntu:2204v1

搭建Harbor镜像仓库

Harbor: 由VMware公司开源的企业级的Docker Registry管理项目,它包括权限管理(RBAC)、LDAP、日志审核、管理界面、自我注册、镜像复制和中文支持等功能。

由于ghcr.io/aquasecurity/trivy-db:2下载一直失败,最后用oras下载才成功下载。

harbor-trivy镜像扫描工具安装部署(离线漏洞库):https://blog.csdn.net/weixin_47055136/article/details/132423901

搭建Registry镜像仓库

docker-registry是官方提供的工具,可以用于构建私有的镜像仓库。

可以通过获取官方registry镜像来运行。默认情况下,仓库会被创建在容器的/var/lib/registry目录下。可以通过-v参数来将镜像文件存放在本地的指定路径。

用docker运行registry

添加HTTPS自签证书

Country Name (2 letter code) [AU]:cn
State or Province Name (full name) [Some-State]:sc
Locality Name (eg, city) []:chengdu
Organization Name (eg, company) [Internet Widgits Pty Ltd]:home
Organizational Unit Name (eg, section) []:home
Common Name (e.g. server FQDN or YOUR name) []:192.168.31.40
Email Address []:xstar@qq.com

私有仓库操作

client设置

docker client不安装证书的话,进行pull/push操作,会出现x509: certificate signed by unknown authority的报错。

{
  "registry-mirrors": [
    "https://dockerproxy.com",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com",
    "https://ccr.ccs.tencentyun.com"
  ],
  "dns": [ "114.114.114.114","61.139.2.69" ],
  "insecure-registries": [ "192.168.31.40:5000","192.168.31.40" ]
}

docker login

不登录直接push镜像的话会失败,提示no basic auth credentials的错误。

docker login -u myuser -p mypassword 192.168.31.40:5000
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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

docker pull/push

查询私有仓库镜像

目前无法用docker search/images/image ls对私有仓库镜像进行查询,可以通过curl调用API列出仓库内所有的镜像。

cp list_private_images.py /usr/local/bin
chmod 755 /usr/local/bin/list_private_images.py

list_private_images.py https://192.168.31.40:5000 myuser mypassword
#https://192.168.31.40:5000/xstar/ubuntu:22.04v1

管理镜像

如果没有开启,执行删除镜像操作的时候,会返回如下两种错误:

{"errors":[{"code":"UNSUPPORTED","message":"The operation is unsupported."}]}
HTTP/1.1 405 Method Not Allowed
Content-Type: application/json; charset=utf-8
Docker-Distribution-Api-Version: registry/2.0
X-Content-Type-Options: nosniff
Date: Fri, 18 Mar 2022 04:12:22 GMT
Content-Length: 78

将Registry部署到Swarm

create service方式

如果使用分布式存储(如:Amazon S3),则可使用完全扩展的多节点Registry服务。

如果使用本地挂载的卷,只能使用单副本服务和Swarm节点约束,来确保只有一个工作节点副本写入。

compose方式

Compose模式不支持external secret,且只能运行在当前节点,建议使用Stack模式。

WARNING: The Docker Engine you're using is running in swarm mode.

Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node.

To deploy your application across the swarm, use `docker stack deploy`.

Creating registry_registry_1 ... done

stack方式

docker stack deploy -c Registry-docker-stack.yml Registry
Creating network Registry_default
Creating service Registry_registry

docker service ls
ID             NAME                MODE         REPLICAS   IMAGE        PORTS
inygepp4z78i   Registry_registry   replicated   1/1        registry:2   *:5000->5000/tcp

docker service ps Registry_registry
ID             NAME                  IMAGE        NODE            DESIRED STATE   CURRENT STATE                ERROR     PORTS
qsiqlrrd0n9j   Registry_registry.1   registry:2   swarm-manager   Running         Running about a minute ago

curl -k -u 'myuser:mypassword' -XGET https://192.168.31.40:5000/v2/xstar/ubuntu/tags/list
{"name":"xstar/ubuntu","tags":["22.04v1"]}

stack+NFS共享

由于依赖节点上的本地数据存储,Registry只能运行在一个指定的节点,无法故障切换、或者负载均衡。

考虑使用NFS作为基本的网络存储,实现Registry在swarm集群中多节点运行和切换。

准备NFS Volume服务

  1. 参考本文前面部份,完成/data/registry目录下的:config.yml、certs和auth文件的准备。
    /data/registry/
    ├── auth
    │   └── htpasswd
    ├── certs
    │   ├── registry.crt
    │   └── registry.key
    ├── config.yml
    └── docker # registry自动创建的数据目录
        └── registry
    
  2. 首先参看docker-swarm中的NFS Volume章节,并完成:
    1. NFS Server部署、配置,设置NFS registry目录共享。
    2. 在所有node安装nfs-common,确认在NFS客户端可以正常mount和读写。

registry stack配置

Stack部署Registry

docker stack启动Registry

docker stack deploy -c Registry-docker-stack_v2.yml Registry
service部署时,自动创建了network、config和volume。

docker stack deploy -c Registry-docker-stack_v2.yml Registry
Creating network Registry_default
Creating config Registry_registry_config_yml
Creating service Registry_registry

docker stack ls
NAME       SERVICES
Registry   1

docker stack ps Registry
ID             NAME                  IMAGE            NODE            DESIRED STATE   CURRENT STATE           ERROR     PORTS
swfcczvkl8zb   Registry_registry.1   registry:2.8.3   swarm-manager   Running         Running 3 minutes ago
ozxm1c2gqifs   Registry_registry.2   registry:2.8.3   swarm-node2     Running         Running 3 minutes ago

在运行的节点,用 docker volume ls和docker config ls可以查看到对应的卷和配置修改,用mount|grep regi可以看到对应NFS目录已挂载。

测试访问registry正常:
curl -k -u 'myuser:mypassword' -XGET https://192.168.31.42:5000/v2/xstar/ubuntu/tags/list
{"name":"xstar/ubuntu","tags":["22.04v1"]}