2 Hours

17. Container Registries: Sharing and Storing Images

Distribution

Where Images Live

Once you build an image, you need to store it somewhere that your production servers can pull from. Container registries are HTTP APIs for storing and retrieving images. This module covers Docker Hub, cloud registries, and running your own private registry.

Docker Hub and Authentication

Docker Hub is the default public registry. It hosts official images (nginx, postgres, node, python) and millions of community images. For production, you should use a private registry or private repositories on Docker Hub. Authentication is required for pushing and for pulling from private repositories. Use `docker login` to authenticate. Store credentials securely — never hardcode `docker login` passwords in CI scripts. Use registry tokens or machine users with limited permissions.

Bash
# Login to Docker Hub
docker login

# Login with specific registry
docker login ghcr.io

# Login with username and password (insecure — use with caution)
echo $MY_PASSWORD | docker login --username myuser --password-stdin

# Tag image for Docker Hub
docker tag myapp:latest myusername/myapp:latest
docker push myusername/myapp:latest

# Pull from private repository
docker pull myusername/private-app:latest

Cloud Container Registries

Major cloud providers offer container registries with deep integration into their ecosystems. AWS ECR (Elastic Container Registry) integrates with IAM for fine-grained permissions and supports image scanning. Azure ACR (Container Registry) supports geo-replication and AKS integration. Google GAR (Artifact Registry) is Google's next-gen registry with vulnerability scanning and CI/CD integration. GitHub Container Registry (ghcr.io) integrates seamlessly with GitHub Actions and supports fine-grained permissions.

Bash
# AWS ECR
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com
docker tag myapp $ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/myapp:latest
docker push $ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/myapp:latest

# Google Artifact Registry
gcloud auth configure-docker us-docker.pkg.dev
docker tag myapp us-docker.pkg.dev/myproject/myapp/myapp:latest
docker push us-docker.pkg.dev/myproject/myapp/myapp:latest

# GitHub Container Registry
echo $GITHUB_TOKEN | docker login ghcr.io -u username --password-stdin
docker tag myapp ghcr.io/owner/repo/myapp:latest
docker push ghcr.io/owner/repo/myapp:latest

Running a Private Registry

For air-gapped environments or when you need full control, run your own registry using the official `registry:2` image. The registry stores images on disk (or S3, GCS, Azure Blob) and supports HTTPS, authentication, and garbage collection. For production, always run the registry behind a reverse proxy with TLS, configure HTTP secret for security, and set up regular garbage collection to remove unused blobs.

Bash
# Run private registry
docker run -d -p 5000:5000 --name registry registry:2

# Tag and push to private registry
docker tag myapp localhost:5000/myapp:latest
docker push localhost:5000/myapp:latest

# Run registry with TLS and authentication (production)
docker run -d -p 443:5000 \
  --name registry \
  -v $(pwd)/certs:/certs \
  -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt \
  -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key \
  -e REGISTRY_AUTH=htpasswd \
  -e REGISTRY_AUTH_HTPASSWD_REALM=Registry \
  -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
  registry:2

# Garbage collect unused blobs
docker exec registry bin/registry garbage-collect /etc/docker/registry/config.yml

Knowledge Check

Ready to test your understanding of 17. Container Registries: Sharing and Storing Images?

Back