Deploying with Docker in CD
Imagine a publishing house. Authors write drafts, editors review them, and once approved, the book is automatically printed and shipped to bookstores. In DevOps, CD pipelines work the same way: once code passes tests, Docker images are automatically deployed to environments. This ensures fast, reliable, and repeatable releases.
Deployment Foundations
1. What is Continuous Delivery (CD)?
- Continuous Delivery: Ensures code is always in a deployable state.
- Continuous Deployment: Extends CD by automatically deploying every successful build to production.
- CD pipelines automate the release process, reducing manual effort and errors.
2. Why Docker in CD?
- Consistency: Same image deployed across staging and production.
- Portability: Images run anywhere — Swarm, Kubernetes, cloud.
- Speed: Automated deployments reduce downtime.
- Rollback: Easy to revert to previous image versions.
3. Typical CD Workflow with Docker
- CI pipeline builds and tests Docker image.
- Image is pushed to a registry (Docker Hub, AWS ECR, GCP Artifact Registry).
- CD pipeline pulls image from registry.
- Deploys image to staging or production environment.
- Monitors deployment and rolls back if needed.
4. Deployment Targets
- Docker Swarm: Deploy services with
docker stack deploy. - Kubernetes: Deploy pods and services with
kubectl apply. - Cloud Services: AWS ECS, Azure Container Instances, Google Cloud Run.
Things to Remember
- CD pipelines automate deployments, ensuring speed and reliability.
- Docker images provide consistency across environments.
- Rollbacks safeguard against failed deployments.
Hands‑On Lab
Step 1: Push Image to Registry
docker tag myapp:latest mydockerhub/myapp:latest
docker push mydockerhub/myapp:latest
Step 2: Deploy to Swarm
docker service create --name myapp --replicas 3 mydockerhub/myapp:latest
Step 3: Deploy with Docker Stack
version: '3.7'
services:
web:
image: mydockerhub/myapp:latest
deploy:
replicas: 3
restart_policy:
condition: on-failure
ports:
- "8080:80"
docker stack deploy -c stack.yml mystack
Step 4: Deploy to Kubernetes
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: mydockerhub/myapp:latest
ports:
- containerPort: 80
kubectl apply -f deployment.yaml
Practice Exercise
- Push a Docker image of a sample app to Docker Hub.
- Deploy the app to a Swarm cluster using
docker stack deploy. - Deploy the same app to Kubernetes using
kubectl apply. - Simulate a failed deployment and perform a rollback.
- Reflect on how CD pipelines ensure fast, reliable releases.
Visual Learning Model
CD Pipeline with Docker
├── CI → builds & tests image
├── Registry → stores image
├── CD → pulls image
├── Deploy → Swarm/Kubernetes/Cloud
└── Rollback → revert if failure
The Hackers Notebook
Deploying with Docker in CD automates the release process, ensuring applications are delivered quickly and reliably. Images are pushed to registries, then deployed to environments like Swarm, Kubernetes, or cloud services. Rollbacks provide safety, making CD pipelines a cornerstone of modern DevOps.

Updated on Dec 26, 2025