Prerequisites
- Make sure your machine has public IP associate with itself
- Ensure you have
sudo
privileges on your system. - Update your system’s package list and upgrade existing packages.
Step 1: Install Docker and Docker Compose
- Update Your System:
sudo apt update && sudo apt upgrade -y
- Install Docker:
sudo apt install -y docker.io
sudo systemctl enable --now docker
- Add User to Docker Group:
sudo usermod -aG docker $USER
newgrp docker
- Verify Docker Installation:
docker --version
Step 2: Run a Local Docker Registry
- Run the Registry:
docker run -d -p 5000:5000 --name registry --restart always registry:2
- Verify the Registry is Running:
curl http://localhost:5000/v2/
- Check Available Registry Images:
curl http://localhost:5000/v2/_catalog
Step 3: Secure the Registry with Authentication
- Create Authentication Credentials:
sudo mkdir -p /etc/docker/registry
sudo chmod 777 /etc/docker/registry
- Install Apache Utilities (htpasswd):
sudo apt update
sudo apt install -y apache2-utils
- Generate Credentials:
htpasswd -Bbn > /etc/docker/registry/htpasswd
- Login to the Private Registry:
docker login localhost:5000
Step 4: Secure the Registry with SSL/TLS
- Install Certbot for SSL Certificates:
sudo apt install -y certbot
- Generate an SSL Certificate:
sudo certbot certonly --standalone -d-
- Run the Registry with SSL & Authentication:
At First Stop the running registry
docker stop registry && docker rm registry
Then run the registry again with
docker run -d -p 5000:5000 --name registry --restart always
-v /etc/docker/registry:/auth
-v /etc/letsencrypt:/certs
-e "REGISTRY_AUTH=htpasswd"
-e "REGISTRY_AUTH_HTPASSWD_REALM="
-e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd"
-e "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/live//fullchain.pem"
-e "REGISTRY_HTTP_TLS_KEY=/certs/live//privkey.pem"
registry:2
- Test Secure Connection:
curl -k -u :'' https://:5000/v2/
Troubleshooting
If you encounter any issues, run the following commands to adjust permissions:
sudo chmod -R 755 /etc/letsencrypt/
sudo chmod -R 755 /etc/letsencrypt/live/
sudo chmod -R 644 /etc/letsencrypt/live//*
sudo chmod -R 644 /etc/letsencrypt/archive//*
sudo chmod 640 /etc/docker/registry/htpasswd
sudo chown root:docker /etc/docker/registry/htpasswd