Shoomer

  • Docker
    DockerShow More
    Monolith to Microservices: A Docker + K8s Migration Story
    8 Min Read
    Docker Security Best Practices | Scout Trivy Scans
    8 Min Read
    CI/CD with Docker and Kubernetes with Examples YML
    10 Min Read
    Docker Networking Deep Dive | Bridge, Host, Overlay
    9 Min Read
    Docker Volumes and Bind Mounts Explained with Examples
    7 Min Read
  • Kubernetes
    KubernetesShow More
    Zero to Hero Kubernetes Crash Course – Minikube, kubectl, Helm Quickstart
    7 Min Read
    Spring Boot Web Crash Course 2025 – REST APIs, Controllers, Get/Post
    7 Min Read
    K8s Crash Course – Learn Containers to Clusters (Hands-On in 2025)
    7 Min Read
    Spring Data JPA Crash Course 2025 – Repository, Query Methods & Paging
    7 Min Read
    Spring Boot for Web Development – Crash Course with Thymeleaf & MVC
    7 Min Read
  • CICD Pipelines
    CICD PipelinesShow More
    What is GitOps with ArgoCD: Deep Dive into Architecture
    10 Min Read
    CI/CD with Docker and Kubernetes with Examples YML
    10 Min Read
  • Pages
    • About Us
    • Contact Us
    • Cookies Policy
    • Disclaimer
    • Privacy Policy
    • Terms of Use
Notification Show More
Font ResizerAa
Font ResizerAa

Shoomer

  • Learning & Education
  • Docker
  • Technology
  • Donate US
Search
  • Home
  • Categories
    • Learning & Education
    • Technology
    • Docker
  • More Foxiz
    • Donate US
    • Complaint
    • Sitemap
Follow US
Home » CI/CD with Docker and Kubernetes with Examples YML
CICD PipelinesDockerKubernetes

CI/CD with Docker and Kubernetes with Examples YML

shoomer
By shoomer
Last updated: June 11, 2025
Share

Continuous Integration and Continuous Deployment (CI/CD) pipelines are essential for delivering software faster, with better quality, and at scale. Integrating Docker and Kubernetes into your CI/CD workflows ensures consistent build environments, seamless containerization, and streamlined deployments to production.

Contents
Table of ContentsUsing Docker in a CI PipelineWhy Use Docker in CI?Workflow OverviewBuilding Docker Images with GitHub Actions or JenkinsUsing GitHub ActionsUsing JenkinsDeploying to KubernetesCreating a Kubernetes DeploymentDeploying the ApplicationAutomating Deployment in CI/CDAutomating Rolling UpdatesRolling Update ConfigurationAutomating Updates with Image TagsFinal Thoughts

This guide explores how to use Docker in a CI pipeline, steps to build Docker images with GitHub Actions or Jenkins, deploy to Kubernetes, and automate rolling updates for no-downtime deployments.

Table of Contents

  1. Using Docker in a CI Pipeline
  2. Building Docker Images with GitHub Actions or Jenkins
    • Using GitHub Actions
    • Using Jenkins
  3. Deploying to Kubernetes
  4. Automating Rolling Updates
  5. Final Thoughts

Using Docker in a CI Pipeline

Docker ensures that your CI builds are reproducible by isolating applications and their dependencies within containers.

Why Use Docker in CI?

  1. Environment Consistency: Builds run the same on local environments, build servers, and production.
  2. Reproducibility: The same Docker image version can be tested and deployed.
  3. Speed: Docker caches layers during builds, reducing build times for incremental changes.
  4. Portability: Docker images can run on any platform where Docker is installed.

Workflow Overview

  1. Developers push their code to a Git repository.
  2. CI tools like GitHub Actions or Jenkins build a Docker image directly from the source.
  3. The image is pushed to a container registry (e.g., Docker Hub, Amazon ECR, or Google Artifact Registry) for distribution.
  4. The image is deployed to a Kubernetes cluster.

Building Docker Images with GitHub Actions or Jenkins

Using GitHub Actions

GitHub Actions is a native CI/CD solution for automating workflows directly from your GitHub repository.

Sample GitHub Actions Workflow for Building Images:
Create a .github/workflows/docker-image.yml file:

name: Build and Push Docker Image

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Code
      uses: actions/checkout@v3

    - name: Log in to Docker Hub
      run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin

    - name: Build Docker Image
      run: docker build -t my-docker-image:${{ github.sha }} .

    - name: Push Docker Image
      run: docker push my-docker-image:${{ github.sha }}

Workflow Explanation:

  1. Trigger on Push: Runs the pipeline on pushes to the main branch.
  2. Docker Login: Uses GitHub Secrets to store Docker credentials securely.
  3. Build and Push Image: Builds and pushes the Docker image, tagging it with the Git commit SHA.

Using Jenkins

Jenkins is a robust and flexible CI/CD tool with excellent Docker integration.

Sample Jenkins Pipeline for Building Docker Images:
Create a Jenkinsfile:

pipeline {
    agent {
        docker {
            image 'docker:dind'
        }
    }

    environment {
        DOCKER_CREDENTIALS_ID = 'docker-hub-credentials'
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }

        stage('Build Docker Image') {
            steps {
                script {
                    sh 'docker build -t my-docker-image:${env.BUILD_ID} .'
                }
            }
        }

        stage('Push to Docker Registry') {
            steps {
                withCredentials([usernamePassword(credentialsId: "${DOCKER_CREDENTIALS_ID}", usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
                    script {
                        sh 'echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin'
                        sh 'docker push my-docker-image:${env.BUILD_ID}'
                    }
                }
            }
        }
    }
}

Workflow Explanation:

  1. Docker-in-Docker (DinD): Runs Docker within the Jenkins agent.
  2. Credentials Management: Uses a Jenkins credential ID (DOCKER_CREDENTIALS_ID) to protect secrets.
  3. Build and Push: Builds a Docker image and pushes it to Docker Hub.

Deploying to Kubernetes

Once the Docker image is built and pushed to a registry, it needs to be deployed to a Kubernetes cluster.

Creating a Kubernetes Deployment

Below is an example Deployment YAML that pulls the Docker image and deploys it to the cluster.

deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: app-container
        image: my-docker-image:<TAG>
        ports:
        - containerPort: 8080

Deploying the Application

To deploy the application to Kubernetes, use kubectl:

kubectl apply -f deployment.yaml

Monitor the deployment:

kubectl get pods
kubectl describe deployment my-app

Automating Deployment in CI/CD

To deploy automatically after image builds, integrate a kubectl step into your CI pipeline:

kubectl rollout restart deployment my-app

Diagram of Workflow:
Below is a simplified representation of the CI/CD flow:

CI/CD Pipeline Diagram

Automating Rolling Updates

Rolling updates allow Kubernetes to incrementally update application deployments without causing downtime.

Rolling Update Configuration

Configure the Deployment strategy to use RollingUpdate:

spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1

This ensures that during an update:

  • MaxSurge: A maximum of one extra Pod is allowed temporarily.
  • MaxUnavailable: At most, one Pod can be unavailable during the update.

Automating Updates with Image Tags

To trigger an automatic rolling update, update the image version in the Deployment YAML:

spec:
  containers:
  - name: app-container
    image: my-docker-image:new-tag

Use kubectl apply to initiate the update:

kubectl apply -f deployment.yaml

To check the rollout status:

kubectl rollout status deployment my-app

Rollback to a previous revision in case of failure:

kubectl rollout undo deployment my-app

Final Thoughts

Integrating Docker and Kubernetes into a CI/CD pipeline simplifies the building, testing, and deployment of containerized applications. By automating every stage, from image builds using GitHub Actions or Jenkins to Kubernetes rolling updates, teams can ensure efficient, reproducible, and downtime-free deployments.

Use this guide to start implementing robust CI/CD pipelines tailored to your containerized workloads, and streamline application delivery with Docker and Kubernetes. Bookmark this guide for future reference and deep-dive into automation!

Your detailed article on “CI/CD with Docker and Kubernetes” is ready, covering Docker in CI pipelines, building images with GitHub Actions or Jenkins, deploying to Kubernetes, and automating rolling updates, along with a helpful diagram. Let me know if there’s anything else you’d like to refine or add!

Share This Article
Facebook Email Copy Link Print
Previous Article Docker Networking Deep Dive | Bridge, Host, Overlay
Next Article Kubernetes Helm Charts: A Complete Tutorial
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Empowering Tomorrow's Leaders through Understanding Child Development and Learning

Learning to thrive

Daily Feed

Zero to Hero Kubernetes Crash Course – Minikube, kubectl, Helm Quickstart
June 23, 2025
Spring Boot Web Crash Course 2025 – REST APIs, Controllers, Get/Post
June 23, 2025
K8s Crash Course – Learn Containers to Clusters (Hands-On in 2025)
June 23, 2025
Spring Data JPA Crash Course 2025 – Repository, Query Methods & Paging
June 23, 2025

You Might Also Like

Docker

Docker Compose for Local Microservices | Multi-Container Orchestration

June 11, 2025
Kubernetes

Running Stateful Applications in Kubernetes (at Scale)

June 11, 2025
Kubernetes

Zero-Downtime Deployments with Kubernetes with Examples

June 11, 2025
Kubernetes

Top 20 Learning Resources for DevOps + Spring Boot Developers (2025)

June 23, 2025
@Copyright 2025
  • Docker
  • Technology
  • Learning & Education
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?