VOOZH about

URL: https://thenewstack.io/boost-your-ci-cd-pipeline-automate-docker-with-github-actions/

⇱ Boost Your CI/CD Pipeline: Automate Docker With GitHub Actions - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2025-02-27 10:00:37
Boost Your CI/CD Pipeline: Automate Docker With GitHub Actions
contributed,
CI/CD / Containers / DevOps

Boost Your CI/CD Pipeline: Automate Docker With GitHub Actions

Learn how to automate Docker workflows using GitHub Actions for faster deployment.
Feb 27th, 2025 10:00am by Advait Patel
👁 Featued image for: Boost Your CI/CD Pipeline: Automate Docker With GitHub Actions
In this guide, we will get deeper into automating Docker workflows with GitHub Actions — just clear steps that get you up and running. Automation is necessary with the rise of cloud-native development and the growing complexity of CI/CD pipelines. GitHub Actions provides a seamless way to integrate Docker into your workflows, reducing manual effort and improving deployment speed. Let’s get started!

How To Set Up GitHub Actions for Docker

👁 Image
Let’s get straight into setting up GitHub Actions. The first thing you need to do is create a workflow file. It’s a simple YAML file placed in .github/workflows/your repo. Step 1: Create the Workflow File
  1. Go to your repo.
  2. Create a folder called .github if it doesn’t already exist.
  3. Inside that, create a folder called workflows.
  4. Create a file called docker.yml (or anything you like) in .github/workflows/.
Here’s the basic structure of your docker.yml file:
name: Docker Workflow

on:
 push:
 branches:
 - main

jobs:
 build:
 runs-on: ubuntu-latest

 steps:
 - name: Check out code
 uses: actions/checkout@v2

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

 - name: Build Docker Image
 run: docker build -t myapp:${{ github.sha }} .
 
 - name: Push Docker Image
 run: |
 echo ${{ secrets.DOCKER_PASSWORD }} | docker login --username ${{ secrets.DOCKER_USERNAME }} --password-stdin
 docker push myapp:${{ github.sha }}

The above YAML file automates the build. Further, it pushes your Docker image whenever changes are moved to the main branch.

Self-Hosted vs. GitHub-Hosted Runners

👁 Image
There are two options available for executing your workflow: The GitHub-hosted runners are the default option. The setup is relatively maintenance-free and handy from your side. With self-hosted runners, the user has complete control over the workflow execution machines. While this method provides increased flexibility, it requires ongoing maintenance. GitHub-hosted runners will be the preferred solution for most users. Since they work best for Docker builds.

Automating Docker Image Builds

👁 Image
Let’s say you’ve pushed some new code. Now, you want to automate building your Docker image. Here’s how you can do that.

Step 2: Build the Docker Image Automatically

You’ll use the docker build command in the GitHub Actions workflow file to build your Docker image automatically. For example, inside your docker.yml file:
- name: Build Docker image
 run: docker build -t myapp:${{ github.sha }} .

The mentioned command will create a Docker image and tag it with the commit SHA (${{ github.sha }}). It makes sure each image is uniquely tagged with the commit ID.

Step 3: Tagging Docker Images Dynamically

You’ll probably want to tag your images in a meaningful way. For example, by branch name and maybe with a version tag. You can do this with GitHub Actions variables:
- name: Build and tag Docker image
 run: docker build -t myapp:${{ github.sha }} -t myapp:${{ github.ref }} .

In this example:
  • ${{ github.sha }} tags the image with a unique commit hash.
  • ${{ github.ref }} tags it with the branch name (e.g., refs/heads/main).
It makes your images easy to track and identify.

Pushing to Docker Hub or GHCR

Now that you’ve built the image, the next step is to push it to a container registry, such as Docker Hub or GitHub Container Registry (GHCR).

Step 4: Set Up Secure Authentication

First, you’ll need to authenticate Docker to push the image. Since you don’t want to expose your credentials directly in the YAML file, GitHub Secrets is your friend here. Go to your GitHub repo’s Settings > Secrets and add two secrets:
  • DOCKER_USERNAME
  • DOCKER_PASSWORD
Then, in your workflow file, you’ll use these secrets to log in to your Docker:
- name: Log in to Docker Hub
 run: |
 echo ${{ secrets.DOCKER_PASSWORD }} | docker login --username ${{ secrets.DOCKER_USERNAME }} --password-stdin

Step 5: Push the Image to Docker Hub or GHCR

Finally, after logging in, push your Docker image:
- name: Push Docker image to Docker Hub
 run: docker push myapp:${{ github.sha }}
The YAML code pushes your image to Docker Hub. Moreover, you can swap this out for GHCR if that’s your choice.

Multi-Arch Builds With QEMU and Buildx

Your existing workflow must support several machine architectures, such as ARM and x86. It allows prop-up hardware operations ranging from Raspberry Pi (ARM-based) devices to cloud-based servers (x86-based). At this phase, the combination of QEMU+Buildx inside GitHub Actions is handy.

Step 6: Set Up Multi-Arch Builds

First, you must set up QEMU and Buildx in your workflow file. Here’s what that looks like:
- name: Set up QEMU
 uses: docker/setup-qemu-action@v2

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

- name: Build multi-arch Docker image
 run: |
 docker buildx build --platform linux/amd64,linux/arm64 -t myapp:${{ github.sha }} .

This will build images for both amd64 (standard desktop/server architecture) and arm64 (used by Raspberry Pi and some cloud servers).

Security Improvements: Scanning Images for Vulnerabilities

Security is always top of mind. You don’t want to push images that have vulnerabilities.

Step 7: Scan Docker Images for Vulnerabilities

You can integrate security tools like Trivy, and Snyk into your GitHub Actions to scan your images during the build process. Here’s an example using Trivy:
- name: Scan Docker image for vulnerabilities
 run: |
 trivy image myapp:${{ github.sha }}
 if [ $? -ne 0 ]; then exit 1; fi
If Trivy detects vulnerabilities, the build will fail. This ensures only secure images get pushed.

Automating Deployments

You’ve built your Docker image and must now push it to the registry. Now, it’s time to deploy it.

Step 8: Deploy to Kubernetes

Using GitHub Actions, you can easily deploy your Docker image to a Kubernetes cluster. Here’s is how;
- name: Deploy to Kubernetes
 uses: appleboy/kubernetes-action@v0.1.0
 with:
 kubeconfig: ${{ secrets.KUBECONFIG }}
 context: ${{ secrets.K8S_CONTEXT }}
 command: kubectl set image deployment/myapp myapp=myapp:${{ github.sha }}

The action updates the Kubernetes deployment with the latest image tagged with ${{ github.sha }}.

Summary

Automating your Docker workflows with GitHub Actions can dramatically improve your development pipeline, reliability, and security. So you now have an automated pipeline with no manual intervention that builds Docker images, gets them pushed to a registry, scans them for known vulnerabilities, and deploys them to your environment. The best part? You can do it all directly from GitHub with just a few lines of YAML with just a few lines of YAML. So, whether you’re pushing code, testing images, or deploying to prod, GitHub Actions has you covered. So, are you ready to take the plunge? Begin automating your Docker workflows now! For a complete working demo, look at the GitHub repository.
TRENDING STORIES
Advait Patel is a senior site reliability engineer at Broadcom, where he plays a key role in managing, building, and securing multimillion dollar revenue-generating products. Advait is an advocate for professional growth and is eager to share his expertise with...
Read more from Advait Patel
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Docker.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.