Docker Registries
A registry is a storage and distribution point for Docker images. Docker Hub is the default registry.
Registry Types
1. Docker Hub
# Log in
docker login
# Push an image
docker push username/myapp:latest
# Pull an image
docker pull nginx:latest
2. Private Registry
# Run a private registry
docker run -d -p 5000:5000 --name registry registry:2
# Tag the image
docker tag myapp:latest localhost:5000/myapp:latest
# Push to the registry
docker push localhost:5000/myapp:latest
3. Cloud Registry
# AWS ECR
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin 123456789.dkr.ecr.us-east-1.amazonaws.com
# Google Container Registry
gcloud auth configure-docker
# Azure Container Registry
az acr login --name myregistry
Use Case: Application Registry
# Run a private registry
docker run -d \
-p 5000:5000 \
-v registry-data:/var/lib/registry \
--name myapp-registry \
registry:2
# Tag the image for the myapp registry
docker tag myapp-api:1.0 localhost:5000/myapp-api:1.0
# Push to the myapp registry
docker push localhost:5000/myapp-api:1.0
# Pull from the myapp registry
docker pull localhost:5000/myapp-api:1.0
Registry Configuration
# docker-compose.yml
version: '3.8'
services:
registry:
image: registry:2
ports:
- "5000:5000"
environment:
REGISTRY_STORAGE_DELETE_ENABLED: "true"
volumes:
- registry-data:/var/lib/registry
registry-ui:
image: joxit/docker-registry-ui
ports:
- "8080:80"
environment:
REGISTRY_TITLE: "MyApp Registry"
REGISTRY_URL: http://registry:5000
depends_on:
- registry
volumes:
registry-data:
Everyday Use Case
Case: A DevOps team needs to manage internal images.
# Push a new image
docker build -t localhost:5000/myapp-app:2.0 .
docker push localhost:5000/myapp-app:2.0
# View all images in the registry
curl -s http://localhost:5000/v2/_catalog
# Delete an old image
curl -X DELETE http://localhost:5000/v2/myapp-app/manifests/<digest>
Best Practices
- Use clear tags:
app:v1.0instead ofapp:latest - Scan for vulnerabilities: Before pushing to the registry
- Back up the registry: Regular data backups
- Use authentication: For controlled access
- Monitor storage: Watch storage usage
Use Case: CI/CD Pipeline
# .github/workflows/docker.yml
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
tags: username/myapp-app:${{ github.sha }}
Troubleshooting
Registry cannot be accessed
# Check the registry status
docker ps | grep registry
# Check the registry logs
docker logs myapp-registry
# Check the port
curl http://localhost:5000/v2/
Push fails
# Check authentication
docker login localhost:5000
# Check the image tag
docker images | grep myapp
# Push with verbose output
docker push localhost:5000/myapp-app:1.0 2>&1
Conclusion
A Docker registry enables controlled image distribution. For applications, use a private registry to manage internal images securely.