Docker Private Image Registry Configuration and Usage
This guide is applicable to setting up a private image repository based on Docker Registry, which supports both HTTP and HTTPS access and provides an authentication mechanism.
Basic Deployment
Create a storage directory on the host machine
bash
sudo mkdir -p /opt/myregistryStart a private repository (without authentication)
bash
docker run -d \
-p 5000:5000 \
--restart=always \
--name registry \
-v /opt/myregistry:/var/lib/registry \
registryVerify running status
bash
docker ps | grep registryTest whether the repository is available
bash
curl http://localhost:5000/v2/_catalog✅ The returned result should be:
json
{"repositories":[]}Indicates that the warehouse has been activated and is empty (normal).
Push the image to the repository
Labeling local images
bash
docker tag nginx:latest localhost:5000/nginx:latestPush to private repository
bash
docker push localhost:5000/nginx:latestVerify push results
bash
curl http://localhost:5000/v2/_catalog✅ The returned results should include:
json
{"repositories":["nginx"]}Other machines pulling images
Note: You need to configure
insecure-registriesto allow non-HTTPS access.
Configure Docker client
Edit /etc/docker/daemon.json:
json
{
"insecure-registries": ["192.168.1.100:5000"]
}Restart Docker:
bash
sudo systemctl restart dockerPulling images
bash
docker pull 192.168.1.100:5000/nginx:latestEnable authentication (HTpasswd)
Create authentication directory
bash
mkdir -p /opt/registry-authGenerate password file
bash
docker run --rm --entrypoint htpasswd httpd:2 -Bbn admin 123456 > /opt/registry-auth/htpasswdStart a repository with authentication
bash
docker run -d \
-p 5000:5000 \
--restart=always \
--name registry \
-v /opt/myregistry:/var/lib/registry \
-v /opt/registry-auth:/auth \
-e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" \
registryVerification and Debugging
View the list of images in the repository
bash
curl http://localhost:5000/v2/_catalogView the tag list of a specific image
bash
curl http://localhost:5000/v2/nginx/tags/listViewing container logs
bash
docker logs registryManagement Operations
Stop/start/restart the repository
bash
docker stop registry
docker start registry
docker restart registryCompletely delete the repository (retain the data volume)
bash
docker rm -f registryRecreate repository (without losing data)
bash
docker run -d -p 5000:5000 --restart=always --name registry \
-v /opt/myregistry:/var/lib/registry \
registryAdvanced Operations
Delete specific images (manual cleanup)
Enter container to delete
bash
docker exec -it registry /bin/sh
cd /var/lib/registry/docker/registry/v2/repositories
rm -rf centos7
exitPerform garbage collection
bash
registry garbage-collect /etc/distribution/config.ymlOne-click script (recommended)
bash
#!/bin/bash
IMAGE_NAME="centos7"
REGISTRY_IP="192.168.91.8:5000"
docker exec -it registry /bin/sh << EOF
cd /var/lib/registry/docker/registry/v2/repositories
rm -rf $IMAGE_NAME
registry garbage-collect /etc/distribution/config.yml
EOF
curl http://$REGISTRY_IP/v2/_catalogComparison of Authentication Methods
| Tool | Behavior | Description |
|---|---|---|
docker login | ✅ Successfully | Credentials saved to ~/.docker/config.json |
docker push/pull | ✅ Automatic credential usage | Docker client automatically reads config.json |
curl | ❌ Unauthenticated | Will not read Docker configuration files |
Using curl for Authentication
Directly pass username and password
bash
curl -u admin:123456 http://localhost:5000/v2/_catalogBase64 encoded authentication header
bash
echo -n "admin:123456" | base64
# Output: YWRtaW46MTIzNDU2
curl -H "Authorization: Basic YWRtaW46MTIzNDU2" http://localhost:5000/v2/_catalogExtracting credentials from Docker configuration (automatically)
bash
AUTH=$(cat ~/.docker/config.json | jq -r '.auths["localhost:5000"].auth')
curl -H "Authorization: Basic $AUTH" http://localhost:5000/v2/_catalogPath change description
| Old path (document/old version) | Actual path (your version) |
|---|---|
/etc/docker/registry/config.yml | /etc/distribution/config.yml |
⚠️ Note: Docker Registry v2+ uses the
distributionconfiguration file, rather than the olderdocker/registryversion.
