Module 11: Docker in CI/CD
Docker plays a vital role in modern CI/CD pipelines, enabling consistent builds, fast deployments, and portable workflows. In this module, we’ll see how Docker integrates with DevOps pipelines to build, test, and deploy applications automatically.
🔹 1. Why Use Docker in DevOps Pipelines?
✅ Consistency – The same image works in dev, staging, and production.
✅ Portability – Works across any cloud or on-premise system.
✅ Speed – Build once, deploy anywhere.
✅ Scalability – Easily run in Kubernetes, ECS, or Swarm.
🔹 2. Building and Pushing Images in CI/CD
The most common CI/CD step with Docker is building images and pushing them to a registry (Docker Hub, AWS ECR, GCP Artifact Registry, etc.).
👉 Example with GitHub Actions:
name: Build and Push Docker Image
on: [push]
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Log in to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push
uses: docker/build-push-action@v4
with:
push: true
tags: mydockerhubuser/myapp:latest
✅ Every commit automatically builds and pushes a fresh Docker image.
🔹 3. Deploying Dockerized Apps Automatically
Once an image is built, CI/CD pipelines can deploy it automatically:
To servers using SSH +
docker runTo Kubernetes clusters with
kubectlTo cloud services (AWS ECS, Google Cloud Run, Azure App Service)
This ensures code changes reach production faster.
🔹 4. Example Pipeline with GitHub Actions
Here’s a simple pipeline for building and deploying a Node.js app using Docker:
name: CI/CD with Docker
on:
push:
branches: [ "main" ]
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build Docker image
run: docker build -t myapp .
- name: Run tests
run: docker run myapp npm test
- name: Push image to Docker Hub
run: |
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker tag myapp mydockerhubuser/myapp:latest
docker push mydockerhubuser/myapp:latest
✅ Builds → tests → pushes → ready for deployment.
🎯 Wrap Up
In this module, you learned how Docker fits into CI/CD pipelines by:
Building and pushing images
Automating deployments
Running real-world pipelines with GitHub Actions
This makes Docker a cornerstone of modern DevOps workflows.
