You can create a private Docker registry to serve as your own “Docker Hub” for internal use, which can be cost-effective and give you full control over your images. Setting up a self-hosted Docker registry is straightforward and can be hosted on your own infrastructure, on-premises servers, or even your private cloud. Here’s how you can do it:
1. Use Docker’s Official Registry Image
Docker provides an official image to set up your private registry.
Steps:
-
Run the Registry Container:
docker run -d -p 5000:5000 --name registry --restart=always registry:2
This starts a registry on port 5000.
-
Test the Registry:
-
Persist Data:
By default, data is not persistent. Use a volume to store images:docker run -d -p 5000:5000 --name registry --restart=always -v /path/to/registry/data:/var/lib/registry registry:2
2. Secure the Registry with HTTPS
To use the registry in a production environment, secure it with HTTPS.
Steps:
-
Generate SSL Certificates:
Use a trusted certificate or generate a self-signed certificate:openssl req -newkey rsa:4096 -nodes -sha256 -keyout domain.key -x509 -days 365 -out domain.crt
-
Configure Docker Registry with HTTPS:
Mount the certificate and key to the container:docker run -d -p 443:5000 --name registry \--restart=always \-v /path/to/registry/data:/var/lib/registry \-v /path/to/domain.crt:/certs/domain.crt \-v /path/to/domain.key:/certs/domain.key \-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \-e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \registry:2
-
Access Your Secure Registry:
Use the registry with HTTPS:docker tag your-image your-domain.com/your-image docker push your-domain.com/your-image
-
Handle Self-Signed Certificates:
If using self-signed certificates, configure Docker clients to trust them:- Copy the
.crt
file to/etc/docker/certs.d/your-domain.com/ca.crt
.
- Copy the
3. Use Authentication for Access Control
Add authentication to secure the registry further.
Steps:
-
Create a Password File:
Usehtpasswd
to create a password file:docker run --rm --entrypoint htpasswd registry:2 -Bbn username password > /path/to/auth/htpasswd
-
Run the Registry with Authentication:
Mount the password file and configure the registry:docker run -d -p 443:5000 --name registry \--restart=always \-v /path/to/registry/data:/var/lib/registry \-v /path/to/domain.crt:/certs/domain.crt \-v /path/to/domain.key:/certs/domain.key \-v /path/to/auth:/auth \-e REGISTRY_AUTH=htpasswd \-e REGISTRY_AUTH_HTPASSWD_REALM="Registry Realm" \-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \-e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \registry:2
-
Access the Registry with Authentication:
Log in to the registry before pushing/pulling images:docker login your-domain.com
4. Add a Frontend (Optional)
For large enterprises, providing a user-friendly interface can be beneficial. Consider using a registry frontend like:
- Portus: An open-source Docker registry UI with user management.
- GitHub: https://github.com/SUSE/Portus
- Harbor: A cloud-native container registry with advanced features.
- Official Site: https://goharbor.io/
5. Scale and High Availability (Optional)
For large enterprises, ensure the registry is scalable and resilient.
Options:
-
Cluster Setup:
Use tools like Kubernetes or Docker Swarm to manage multiple registry instances. -
Object Storage Backend:
Configure the registry to use object storage (e.g., MinIO, AWS S3, Alibaba OSS) for scalability:- Update
config.yml
for the registry:storage:s3:accesskey: <your-access-key>secretkey: <your-secret-key>region: <region>bucket: <bucket-name>
- Update
By self-hosting a private Docker registry, you gain full control over your images, avoid cloud service fees, and comply with organizational policies. This approach is scalable and cost-effective for enterprises.