For most teams, the right answer is “use Docker Hub, GHCR, ECR, or a managed registry.” But there are real reasons to self-host: air-gapped networks, regulatory requirements that bar third-party image storage, egress costs, or simply wanting the cache layer for a slow internet link. This article covers the two relevant options — the bare-bones Distribution (CNCF reference implementation) and Harbor (a full-featured registry with RBAC, scanning, and replication) — and when each fits.
How to verify
If a registry is allegedly running, probe it:
# Distribution / Docker Registry v2 API
curl -s https://registry.example.com/v2/_catalog | jq
curl -s -u user:pass https://registry.example.com/v2/_catalog | jq
curl -s https://registry.example.com/v2/myapp/tags/list | jq
# What does the daemon think it's talking to?
docker pull registry.example.com/myapp:latest
docker info | grep -A5 Registry
# TLS chain
openssl s_client -connect registry.example.com:443 -servername registry.example.com -showcerts < /dev/null | head -20
If /v2/_catalog returns 401, auth is in place. If it returns 404, the path doesn’t exist (probably a misrouted reverse proxy). If it returns JSON with repository names, you’re talking to a real registry.
What’s happening
A Docker registry is a content-addressable blob store with a thin HTTP API on top — the OCI Distribution spec. Each image is a manifest (JSON listing layers and the config) plus the layers themselves (gzipped tarballs). Pulls fetch the manifest then the layers. Pushes upload layers (deduplicated by digest) then the manifest. Garbage collection removes layers no manifest references anymore.
Distribution is the reference implementation. It’s a Go binary, configured by one YAML file, ~25 MB of container image, no built-in UI, no built-in auth (you bolt on htpasswd or token auth), no scanning, no replication. Good for one-team workflows where the registry is just a place to put images.
Harbor is built on Distribution but wraps it with a Postgres database, a UI, RBAC (projects, robot accounts, groups), Trivy-based scanning, content signing (Cosign integration), retention policies, replication between registries, and audit logs. Harbor is a real piece of infrastructure — six or seven containers, a Postgres instance, a Redis instance.
The procedure
For a Distribution-only setup behind Nginx with TLS and basic auth:
- Generate an htpasswd file. Bcrypt is the only safe choice — Distribution rejects MD5:
sudo apt-get install -y apache2-utils
mkdir -p /etc/registry/auth
htpasswd -Bc /etc/registry/auth/htpasswd ops
htpasswd -B /etc/registry/auth/htpasswd build-bot
chmod 0640 /etc/registry/auth/htpasswd
- Run the registry container with TLS and auth disabled (Nginx will handle both):
# compose.yaml
services:
registry:
image: registry:2.8
restart: unless-stopped
environment:
REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY: /var/lib/registry
REGISTRY_HTTP_ADDR: 0.0.0.0:5000
REGISTRY_STORAGE_DELETE_ENABLED: "true"
REGISTRY_HTTP_HEADERS_X-Content-Type-Options: "[nosniff]"
volumes:
- registry-data:/var/lib/registry
networks:
- internal
nginx:
image: nginx:1.27-alpine
restart: unless-stopped
ports:
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/registry.conf:ro
- /etc/registry/auth/htpasswd:/etc/nginx/htpasswd:ro
- /etc/letsencrypt/live/registry.example.com:/etc/ssl/certs:ro
networks:
- internal
depends_on:
- registry
volumes:
registry-data:
networks:
internal:
- Nginx config (
nginx.conf):
server {
listen 443 ssl http2;
server_name registry.example.com;
ssl_certificate /etc/ssl/certs/fullchain.pem;
ssl_certificate_key /etc/ssl/certs/privkey.pem;
# Docker requires large bodies for layer uploads
client_max_body_size 0;
chunked_transfer_encoding on;
auth_basic "Registry";
auth_basic_user_file /etc/nginx/htpasswd;
location /v2/ {
# /v2/ has its own probe that should not require auth
if ($http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go ).*$") {
return 404;
}
proxy_pass http://registry:5000;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 900;
}
}
- Bring it up and verify:
docker compose up -d
docker login registry.example.com
docker pull alpine:3.19
docker tag alpine:3.19 registry.example.com/test/alpine:3.19
docker push registry.example.com/test/alpine:3.19
curl -s -u ops:... https://registry.example.com/v2/_catalog | jq
- Schedule garbage collection — Distribution doesn’t reclaim space until you run it explicitly:
# Weekly cron on the host
0 3 * * 0 docker compose exec -T registry registry garbage-collect /etc/docker/registry/config.yml
registry garbage-collect walks the manifests, finds layers no manifest references, deletes them. The container must be running but ideally not serving writes during GC.
For Harbor, the upstream install is a tarball that lays out a Compose project for you:
wget https://github.com/goharbor/harbor/releases/download/v2.10.2/harbor-offline-installer-v2.10.2.tgz
tar xzf harbor-offline-installer-v2.10.2.tgz && cd harbor
cp harbor.yml.tmpl harbor.yml
# Edit harbor.yml: hostname, https.certificate, https.private_key,
# harbor_admin_password, database.password
sudo ./install.sh --with-trivy --with-notary
Harbor sets up Postgres, Redis, the registry, the UI, the scanner, and the signer. It exposes the registry on the hostname configured in harbor.yml. Trivy runs as a separate container that Harbor calls into for image scans on push.
- Plug Harbor’s API into CI for vulnerability checks and image signing:
# Push, scan, gate on results
docker push harbor.example.com/myapp/web:1.42.0
curl -s -u $HARBOR_USER:$HARBOR_PASS \
-X POST https://harbor.example.com/api/v2.0/projects/myapp/repositories/web/artifacts/sha256:.../scan
curl -s -u $HARBOR_USER:$HARBOR_PASS \
"https://harbor.example.com/api/v2.0/projects/myapp/repositories/web/artifacts/sha256:.../additions/vulnerabilities"
Common pitfalls
- Running Distribution on plain HTTP and adding
"insecure-registries": ["registry.example.com"]to every docker daemon — convenient and a security disaster. Always TLS. - Forgetting
client_max_body_size 0in Nginx — uploads of large layers fail at 1 MB. - Skipping garbage collection — Distribution storage grows monotonically. A six-month-old registry with no GC is often 10x larger than it needs to be.
- Storing the registry’s blob store on the local Docker volume of a single-node host with no backup. The registry IS your image lineage; back it up.
- Running Harbor with the default admin password (
Harbor12345) and the default Postgres password fromharbor.yml.tmpl. Both must be rotated before the install. - Letting Harbor’s Trivy scanner run with stale vulnerability DB — Harbor pulls updates via the Trivy adapter; verify the adapter container is healthy.
For most clients, we recommend a hosted registry (ECR, GHCR, or the platform-native one) — the operational overhead of self-hosting rarely pays off. When self-hosting is the right call, we run the Distribution or Harbor deployment as part of managed operations with backup, GC, and scan-feed monitoring on the standard schedule.