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
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 paulit 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.
Thanks for sharing the solution. I had the same problem because of other tutorials that suggested the
-cparameter.Thank you, I had the exact same problem after following some online tutorials for creating the
htpasswdfile with the basichtpasswd -c <file> <usercommand, it supposed to be -B not -c in order to create a bcrypt password.I'm glad I could help.