This tutorial aims to guide you through the process of securing your private container registries with authentication. We will be focusing on Docker registries, and you will learn how to restrict access to your Docker images to authorized users only.
By the end of this tutorial, you will be able to:
- Understand the importance of securing your private registries
- Set up basic authentication for your private Docker registry
- Verify the setup
Securing your private Docker registry involves creating a password file for authentication and running your registry with additional parameters to use this password file.
First, you'll need to create a password file using htpasswd
. You can install it with apt-get
if you're on a Debian-based system:
sudo apt-get install apache2-utils
Then, create a password for a user. For example, to create a user named testuser
:
htpasswd -Bc .htpasswd testuser
You'll be prompted to enter and confirm your password.
Next, start your registry with additional parameters to use the .htpasswd
file:
docker run -d -p 5000:5000 --restart=always --name registry -v `pwd`/.htpasswd:/auth/.htpasswd -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/.htpasswd registry:2
This command tells Docker to run the registry and use the htpasswd file for authentication.
Finally, try to pull an image from your registry. If your setup is correct, it should prompt you for the username and password:
docker pull localhost:5000/my-image
Let's put everything together:
# Install htpasswd
sudo apt-get install apache2-utils
# Create a password file
htpasswd -Bc .htpasswd testuser
# Run Docker registry with authentication
docker run -d -p 5000:5000 --restart=always --name registry -v `pwd`/.htpasswd:/auth/.htpasswd -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/.htpasswd registry:2
# Try to pull an image
docker pull localhost:5000/my-image
In this tutorial, we've learned how to secure a private Docker registry with basic authentication. We created a password file with htpasswd
, ran our Docker registry with additional parameters to use this password file, and verified our setup.
To continue your learning, consider exploring more advanced forms of authentication, like token-based authentication.
Exercise 1: Set up a private Docker registry without authentication and try to pull an image from it. What happens?
Exercise 2: Now secure your registry with authentication. Try to pull an image without providing a username and password. What happens?
Exercise 3: Try to pull an image after providing the correct username and password. What happens?