DevOps

How to Build a CI/CD Pipeline with GitHub Actions and Docker – Step-by-Step

How to Build a CI/CD Pipeline with GitHub Actions and Docker – Step-by-Step

Want to automate your application deployment process? Using GitHub Actions with Docker is one of the most efficient ways to build a complete CI/CD pipeline — directly from your repository.

In this guide, we’ll walk you through the exact steps to build a CI/CD pipeline that builds a Docker image and pushes it to Docker Hub whenever you push code to your repo. Let’s go! 🚀

🧱 What You’ll Need

  • A GitHub account
  • A Docker Hub account
  • A GitHub repo with your app code
  • Basic knowledge of Docker and YAML

Step 1: Create Your Dockerfile

Inside the root of your project, create a file named Dockerfile. Here’s a basic example for a Node.js app:

# Dockerfile
FROM node:18
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]

Step 2: Create a Workflow in GitHub Actions

Inside your repo, create a folder .github/workflows and add a YAML file, for example ci-cd.yml:

# .github/workflows/ci-cd.yml
name: CI/CD Pipeline

on:
  push:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Log in to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and Push Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: yourdockerhubusername/your-app:latest

💡 Important: Replace yourdockerhubusername/your-app with your actual Docker Hub repo name.

Step 3: Add DockerHub Secrets to GitHub

Go to your GitHub repo settings → Secrets → Actions → “New repository secret”.

  • DOCKER_USERNAME: your Docker Hub username
  • DOCKER_PASSWORD: your Docker Hub password or access token

Now your workflow can log in securely without hardcoding credentials.

Step 4: Test Your Pipeline

Push any change to the main branch, and go to the Actions tab on GitHub. You’ll see the workflow running — building your Docker image and pushing it to Docker Hub.

🎯 What This Pipeline Does

  • Watches for changes in the main branch
  • Builds a Docker image using your Dockerfile
  • Pushes the image to Docker Hub
  • Runs entirely on GitHub’s infrastructure

✅ Final Thoughts

GitHub Actions makes it super simple to create a full CI/CD workflow directly within your repo. When combined with Docker, it becomes a powerful way to build, test, and ship applications automatically.

In future posts, we’ll show how to extend this pipeline with:

  • Automated tests
  • Multi-environment deployments
  • Kubernetes integration

🙌 Was This Helpful?

  • 👍 Share it with your team
  • 💬 Leave a comment below
  • 📬 Subscribe to get more DevOps tips!

Related Articles

One Comment

Leave a Reply

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

Back to top button