登录haproxy后面的私有docker注册表v2

登录haproxy后面的私有docker注册表v2,docker,haproxy,docker-registry,Docker,Haproxy,Docker Registry,我正在尝试与HAProxy建立一个新的Docker注册表(v2)。对于Docker注册表,我使用来自Docker hub的图像,并使用Docker run-d-p 5000:5000-v/path/to/Registry:/tmp/Registry Registry:2.0.1运行它。这是我的HAProxy配置的一个子集: global chroot /var/lib/haproxy user haproxy group haproxy daemon tu

我正在尝试与HAProxy建立一个新的Docker注册表(v2)。对于Docker注册表,我使用来自Docker hub的图像,并使用
Docker run-d-p 5000:5000-v/path/to/Registry:/tmp/Registry Registry:2.0.1
运行它。这是我的HAProxy配置的一个子集:

global
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
    daemon
    tune.ssl.default-dh-param 2048

userlist auth_list
    group docker_registry users root
    user root password ***PASSWORD***

backend docker-registry
    server 127.0.0.1:5000_localhost 127.0.0.1:5000 cookie 127.0.0.1:5000_localhost 

frontend shared-frontend
    mode http
    bind *:80
    bind *:443 ssl crt *** CERT FILES ***
    option accept-invalid-http-request

    acl domain_d.mydomain.com hdr(host) -i d.mydomain.com
    acl auth_docker_registry_root http_auth(auth_list) root
    redirect scheme https if !{ ssl_fc } domain_d.mydomain.com
    http-request auth realm Registry if !auth_docker_registry_root { ssl_fc } domain_d.mydomain.com
    use_backend docker-registry if domain_d.mydomain.com
需要注意的重要事项是,我使用HAProxy来执行SSL终止和HTTP身份验证,而不是注册表

当我尝试登录到新注册表时,会出现问题。如果我运行
docker登录https://d.mydomain.com/v2/
然后输入用户
root
和密码,我会收到以下错误消息:

Docker客户端:

FATA[0009] Error response from daemon: invalid registry endpoint https://d.mydomain.com/v2/: https://d.mydomain.com/v2/ does not appear to be a v2 registry endpoint. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry d.mydomain.com` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/d.mydomain.com/ca.crt
Docker守护程序:

ERRO[0057] Handler for POST /auth returned error: invalid registry endpoint https://d.mydomain.com/v2/: https://d.mydomain.com/v2/ does not appear to be a v2 registry endpoint. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry d.mydomain.com` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/d.mydomain.com/ca.crt
ERRO[0057] HTTP Error: statusCode=500 invalid registry endpoint https://d.mydomain.com/v2/: https://d.mydomain.com/v2/ does not appear to be a v2 registry endpoint. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry d.mydomain.com` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/d.mydomain.com/ca.crt
因此,我尝试将
--unsecure registry d.mydomain.com
添加到:

  • /etc/default/docker
    with
    docker\u OPTS=-Hunix:///var/run/docker.sock --不安全的注册表d.mydomain.com
  • 使用
    docker-d--unsecureregistry d.mydomain.com
无论是这些,还是我在网上找到的任何其他东西,都不起作用。每次重新启动docker并尝试再次登录后,都会显示相同的错误消息

我还尝试了其他一些方法:

  • 在浏览器中,转到
    d.mydomain.com
    会导致404
  • 在浏览器中,转到
    d.mydomain.com/v2/
    会导致:
    {}
  • 替换
    https://d.mydomain.com/v2/
    在login命令中使用所有这些都不会成功
    • http://d.mydomain.com/v2/
    • d.mydomain.com/v2/
    • http://d.mydomain.com/
    • d.mydomain.com/
HAProxy执行SSL终止和HTTP auth的此设置在过去使用第一个版本的注册表和较旧版本的docker进行过。Docker registry v2有什么变化吗?这个还能用吗?如果它没有改变,为什么
--unsecureregistry
标志不再起作用

而且,我已经努力让它工作了一段时间,所以我可能已经忘记了所有我尝试过的事情。如果有什么东西可以用,让我知道,我会试试看

谢谢, 贾梅斯泰维


编辑
此编辑已移动到下面的答案

我已使其正常工作。这是我的新配置:

haproxy.cfg

global
    chroot /var/lib/haproxy
    user haproxy
    group haproxy
    daemon
    tune.ssl.default-dh-param 2048

userlist auth_list
    group docker_registry users root
    user root password ***PASSWORD***

backend docker-registry
    server 127.0.0.1:5000_localhost 127.0.0.1:5000 cookie 127.0.0.1:5000_localhost 

backend docker-registry-auth
    errorfile 503 /path/to/registry_auth.http

frontend shared-frontend
    mode http
    bind *:80
    bind *:443 ssl crt *** CERT FILES ***
    option accept-invalid-http-request

    acl domain_d.mydomain.com hdr(host) -i d.mydomain.com
    redirect scheme https if !{ ssl_fc } domain_d.mydomain.com
    acl auth_docker_registry_root http_auth(auth_list) root
    use_backend docker-registry-auth if !auth_docker_registry_root { ssl_fc } domain_d.mydomain.com
    rsprep ^Location:\ http://(.*) Location:\ https://\1
    use_backend docker-registry if domain_d.mydomain.com
registry\u auth.http

HTTP/1.0 401 Unauthorized
Cache-Control: no-cache
Connection: close
Content-Type: text/html
Docker-Distribution-Api-Version: registry/2.0
WWW-Authenticate: Basic realm="Registry"

<html><body><h1>401 Unauthorized</h1>
You need a valid user and password to access this content.
</body></html>
HTTP/1.0
缓存控制:没有缓存
连接:关闭
内容类型:text/html
Docker分发Api版本:registry/2.0
WWW-Authenticate:Basic-realm=“注册表”
401未经授权
您需要有效的用户和密码才能访问此内容。
区别在于
http请求auth
行已被
use\u backend docker registry auth
替换。后端
docker注册表auth
没有服务器,它将始终给出
503
错误。但是503错误文件已更改为
registry\u auth.http
。在
registry\u auth.http
中,错误代码被覆盖为
401
,标题
WWW-Authenticate
被设置为
Basic realm=“registry”
,提供了基本的HAProxy 401错误页面,最重要的是,标题
Docker分发Api版本
被设置为
registry/2.0

因此,除了现在设置了自定义头
Docker分发Api版本
之外,此黑客破解设置的工作原理与旧的
http请求验证
行完全相同。这允许该设置通过从的第236行开始的测试

现在,当我运行
docker login d.mydomain.com
时,登录成功,我的凭证被添加到
.docker/config.json

第二个问题是,即使登录了新存储库,我也无法将其推送到新存储库。这是通过在
前端
中添加
rsprep
行来解决的。此行所做的是修改
位置
标题(如果存在),将所有
http://
改为
https://


我还发现了一些问题,以供将来参考

作为对前面答案的一个小澄清:我不得不改变这一行:

WWW-Authenticate: Basic realm="Registry"
为此:

WWW-Authenticate: Basic realm="Registry realm"
然后一切都正常了


顺便说一句,可以使用mkpasswd(whoisdeb包的一部分)对过程进行哈希处理。

您的后端配置是什么样子的,我假设您使用nginx拆分/v2?问题中的配置就是它。服务器指向docker注册表容器所在的
127.0.0.1:5000
。除非nginx内置于docker注册表映像中,否则根本就没有nginx(其想法是使用HAProxy,因为我在其他所有方面都使用HAProxy)。我不太清楚您所说的“拆分/v2 out”是什么意思,但如果您是指添加
reqrep^([^\]*\/)v2[/]?(*)\1\2
到后端以打开
http://d.mydomain.com/v2/
http://d.mydomain.com/
那么这似乎不起作用,因为我尝试登录时遇到了相同的错误。您在这方面有什么进展吗?目前还没有。正如我在文章中提到的,我已经在这方面工作了一段时间,目前没有想法可以测试。我目前最大的问题是,新的docker注册表是用go编写的,而不是用python编写的,所以我很难理解为什么它不工作。这方面有什么新闻吗。我也有同样的问题,但似乎没有任何效果。如果我禁用基本身份验证,则仅使用HAProxy作为https终止符可以正常工作。