r/docker•
Stuffinator

docker registry basic authentication -> 401 Unauthorized

Hello everyone,

I'm trying to run my own registry in my docker swarm, but I seem to fail to secure it in the most basic ways.

Error response from daemon: login attempt to http://localhost:5000/v2/ failed with status: 401 Unauthorized

I got a docker swarm with one manager node and two worker nodes. For persistent storage I use GlusterFS. The clustered storage is mounted in every machine under /mnt/

Here are the steps I went through (obviously with simplified password for testing purposes)

# create password file with htpasswd
htpasswd /mnt/auth/registry.htpasswd paul
New password: aaa
Re-type new password: aaa

# add the registry to my swarm
docker service create --name registry \
  --publish published=5000,target=5000 \
  --env "REGISTRY_AUTH=htpasswd" \
  --env "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
  --env REGISTRY_AUTH_HTPASSWD_PATH=/auth/registry.htpasswd \
  --mount type=bind,source=/mnt/auth,destination=/auth \
  registry:2

# check if service is running
docker service ps registry

ID             NAME         IMAGE        NODE      DESIRED STATE   CURRENT STATE            ERROR     PORTS
3nbb827gq6yg   registry.1   registry:2   my_node   Running         Running 14 seconds ago

# log in to registry with paul:aaa
docker login localhost:5000
Username: paul
Password: aaa
Error response from daemon: login attempt to http://localhost:5000/v2/ failed with status: 401 Unauthorized

# curl up on the floor and cry

I opened a shell to the running container, to see if everything is in order.

/ # echo $REGISTRY_AUTH_HTPASSWD_PATH
/auth/registry.htpasswd

/ # cat /auth/registry.htpasswd
paul:$apr1$jYX/MTw5$yOL0tELnfkj.RA348vIVF0

I also tried it outside the swarm

docker run -d \
  -p 5001:5000 \
  --restart=always \
  --name registry \
  -v /mnt/auth:/auth \
  -e "REGISTRY_AUTH=htpasswd" \
  -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
  -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/registry.htpasswd \
  registry:2

but get the same error :(

Any help would be really appreciated.

Posted Nov 29, 2021, 8:28 AM UTC · edited

16
StuffinatorOP•

Ok I solved it 🥳

The problem was that I created the password with default encryption, which is MD5 and apparently not supported by the official docker registry image.

When running htpasswd -B /mnt/auth/registry.htpasswd paul it creates the password with bcrypt encryption, which is much more secure and also supported by the official image.

With that, everything works fine.

Topic can be closed.

2
Certain_Combination•

Thanks for sharing the solution. I had the same problem because of other tutorials that suggested the -c parameter.

1
TiDuNguyen•

Thank you, I had the exact same problem after following some online tutorials for creating the htpasswd file with the basic htpasswd -c <file> <user command, it supposed to be -B not -c in order to create a bcrypt password.

1
StuffinatorOP•

I'm glad I could help.

1
bufandatl•

Did you check Docker logs. What is the registry saying when it starts and what when you try to connect.

1
StuffinatorOP•

After container start

time="2021-11-29T11:15:21.830872715Z" level=warning msg="No HTTP secret provided - generated random secret. This may cause problems with uploads if multiple registries are behind a load-balancer. To provide a shared secret, fill in http.secret in the configuration file or set the REGISTRY_HTTP_SECRET environment variable." go.version=go1.11.2 instance.id=63d38128-ea62-49af-b48d-7ae932e29266 service=registry version=v2.7.1
time="2021-11-29T11:15:21.831430586Z" level=info msg="redis not configured" go.version=go1.11.2 instance.id=63d38128-ea62-49af-b48d-7ae932e29266 service=registry version=v2.7.1
time="2021-11-29T11:15:21.832603941Z" level=info msg="Starting upload purge in 36m0s" go.version=go1.11.2 instance.id=63d38128-ea62-49af-b48d-7ae932e29266 service=registry version=v2.7.1
time="2021-11-29T11:15:21.843082726Z" level=info msg="using inmemory blob descriptor cache" go.version=go1.11.2 instance.id=63d38128-ea62-49af-b48d-7ae932e29266 service=registry version=v2.7.1
time="2021-11-29T11:15:21.876695906Z" level=info msg="listening on [::]:5000" go.version=go1.11.2 instance.id=63d38128-ea62-49af-b48d-7ae932e29266 service=registry version=v2.7.1

after login attempt

172.17.0.1 - - [29/Nov/2021:11:16:08 +0000] "GET /v2/ HTTP/1.1" 401 87 "" "docker/20.10.11 go/go1.16.9 git-commit/847da18 kernel/5.10.0-8-amd64 os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.11 \\(linux\\))"time="2021-11-29T11:16:08.242849567Z" level=warning msg="error authorizing context: basic authentication challenge for realm "Registry Realm": invalid authorization credential" go.version=go1.11.2 http.request.host="localhost:5001" http.request.id=fad3bcc8-5d4e-4fcb-a353-ae1165c608d6 http.request.method=GET http.request.remoteaddr="172.17.0.1:58386" http.request.uri="/v2/" http.request.useragent="docker/20.10.11 go/go1.16.9 git-commit/847da18 kernel/5.10.0-8-amd64 os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.11 \(linux\))"
time="2021-11-29T11:16:08.356933897Z" level=error msg="error authenticating user "paul": authentication failure" go.version=go1.11.2 http.request.host="localhost:5001" http.request.id=259abbcf-94bb-4347-8f38-22324ed5ba72 http.request.method=GET http.request.remoteaddr="172.17.0.1:58390" http.request.uri="/v2/" http.request.useragent="docker/20.10.11 go/go1.16.9 git-commit/847da18 kernel/5.10.0-8-amd64 os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.11 \(linux\))"
172.17.0.1 - - [29/Nov/2021:11:16:08 +0000] "GET /v2/ HTTP/1.1" 401 87 "" "docker/20.10.11 go/go1.16.9 git-commit/847da18 kernel/5.10.0-8-amd64 os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.11 \\(linux\\))"
time="2021-11-29T11:16:08.382638382Z" level=warning msg="error authorizing context: basic authentication challenge for realm "Registry Realm": authentication failure" go.version=go1.11.2 http.request.host="localhost:5001" http.request.id=259abbcf-94bb-4347-8f38-22324ed5ba72 http.request.method=GET http.request.remoteaddr="172.17.0.1:58390" http.request.uri="/v2/" http.request.useragent="docker/20.10.11 go/go1.16.9 git-commit/847da18 kernel/5.10.0-8-amd64 os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.11 \(linux\))"
1