Skip to main content

Command Palette

Search for a command to run...

Module 11: Docker in CI/CD

Updated
2 min read

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 run

  • To Kubernetes clusters with kubectl

  • To 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.


Mastering Docker: The Complete Guide

Part 11 of 14

This series takes you on a journey from Docker basics to advanced real-world applications. You’ll learn everything from running your first container, building images, and managing networks, to multi-container setups, CI/CD pipelines.

Up next

Module 12: Orchestration Introduction

Why this module? Until now, we’ve been running containers individually with Docker. But in real-world production, we often need: Multiple containers working together. Scaling containers up or down depending on demand. Automatic recovery if a conta...