如何login到具有HTTP前缀设置的Dockerregistry?

我已经设置了自己的Docker Registry,但是我不希望它在根URL上,所以当我创build服务时,我使用了REGISTRY_HTTP_PREFIX环境variables并将其设置为/registry/ ,因此/registry/的URL是https://tools.example.com/registry 。 这是由Nginx代理,它有基本的身份validation设置。

我使用浏览器testing了对registry的访问,我可以通过http://tools.example.com/registry/v2/_catalog显示没有存储库。

在这里输入图像说明

这导致我认为这是工作。 但是,当我尝试使用Docker命令行login到registry时,我得到了基本身份validation挑战,但是由于URL不正确而无法login,例如

 docker login -u russells -p xxxxxxxx https://tools.example.com/registry/ Error response from daemon: login attempt to https://tools.example.com/v2/ failed with status: 404 Not Found 

从错误可以看出,前缀没有被正确添加。 所以我怎样才能login到registry,所以我可以推送图像。 有没有一个环境variables或我缺less的东西,使docker login工作正常?

Update – 2017-08-12 2253 BST

我一直玩的configuration了一下,但我还没有走得很远。

按照要求这里是我的configuration文件。

nginx.conf

 user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; keepalive_timeout 65; upstream docker-registry { server registry:5000; } map $upstream_http_docker_distribution_api_version $docker_distribution_api_version { '' 'registry/2.0'; } server { listen 15000; server_name tools.example.com; # disable any limits to avoid HTTP 413 for large image uploads client_max_body_size 0; # required to avoid HTTP 411 chunked_transfer_encoding on; location /registry/ { # Do not allow connections from docker 1.5. and earlier # docker pre-1.6.0 did not properly set the user agent on ping, catch "Go *" user agents if ($http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go ).*$") { return 404; } auth_basic "Docker Registry"; auth_basic_user_file /etc/nginx/.htpasswd; add_header 'Docker-Distribution-Api-Version' $docker_distribution_api_version always; proxy_pass http://docker-registry/registry/; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_read_timeout 900; } } } 

我的Dockerregistry服务部署为registry ,并运行在默认端口5000上。现在看这个,我想我已经弄糊涂了。 我不需要registry来回答前缀本身,只是Nginx。

例如,如果我离开的位置设置为/然后我可以login,但如果我改变这/registry/然后我无法。 我开始认为这两者是相互矛盾的。

注册处

除了一个环境variables – REGISTRY_HTTP_PREFIX ,我还没有为registry设置configuration,这可能在此设置中超过了要求。

更新 – 2017年08月15日1100 BST

为了testingregistry的prefix ,我使用下面的configuration文件创build了一个registry容器:

 version: 0.1 auth: htpasswd: realm: Docker Registry path: /auth/etc/htpasswd storage: filesystem: rootdirectory: /var/lib/registry maxthreads: 100 http: addr: 0.0.0.0:5000 prefix: /registry/ tls: certificate: /auth/ssl/certs/registry.cert key: /auth/ssl/private/registry.key 

由于这是使用自签名证书,所以我通过将证书放置在/etc/docker/certs.d/host-lin-01:5000更新了Docker引擎。

然后我用下面的命令创build了容器:

docker run -it --rm -p 5000:5000 --name registry_test -v ~/workspaces/docker/registry/etc/registry.yml:/etc/docker/registry/config.yml -v ~/workspaces/docker/registry:/auth registry:2

如果我尝试使用以下命令loginregistry:

docker login -u russells -p xxxxxx https://host-lin-01:5000/registry

我得到以下错误:

Error response from daemon: login attempt to https://host-lin-01:5000/v2/ failed with status: 404 Not Found

现在,如果我从registryyaml文件中删除perfix: /registry/ line并重新启动容器,然后login一切都很好:

 docker login -u russells -p xxxxxx https://turtle-host-03:5000/ Login Succeeded 

然而,奇怪的是,login工作的任何前缀我放在loginURL的末尾,例如

 docker login -u russells -p xxxxxx https://turtle-host-03:5000/registry/fred/34 Login Succeeded 

我不明白。 我必须误解prefix设置。

您的问题是您的基本身份validation的应用程序。 所以你有Nginx的基本身份validation是由一个普通的registry支持。

你可以validationurl,并看到_catalog空白json,这让你觉得它正在工作。 但是从技术上讲,发生的事情是,你的Nginx正在请求用户名/密码,然后传递给你的dockerregistry。 而这又没有authentication。

现在,当你使用docker login你期待一个authentication的registry,但你有一个authenticationnginx和未经过身份validation的registry。 所以你需要从你的nginxconfiguration中排除下面的代码行

 auth_basic "Docker Registry"; auth_basic_user_file /etc/nginx/.htpasswd; 

当启动你的registry时,你需要定义下面的环境variables

  REGISTRY_AUTH: htpasswd REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm 

请确保您从主机映射/auth/htpasswd到registry容器。 做到这一点,设置应该工作。 另外请确保在您的Docker客户端系统中设置服务器证书

可选更改

这个答案的下一部分是可选的。 既然你使用的是Nginx和Registry。 我build议你REGISTRY_HTTP_PREFIX你的registry的REGISTRY_HTTP_PREFIX并将proxy_pass更改为

 proxy_pass http://docker-registry/;