Automating Docker Builds with GitHub Actions

Costa Paigin

Head of DevOps

April 28, 2023

Share your Social Media

Docker has revolutionized the software development landscape by simplifying the process of building, shipping, and running applications through containerization. This powerful technology enables developers to encapsulate their apps, making dependency management easier and ensuring consistency across different environments. However, managing Docker images and containers can become overwhelming, especially when dealing with multiple applications and teams. Luckily, GitHub Actions offers a solution. By leveraging GitHub Actions, a versatile workflow automation tool, you can automate your software development workflows, including building and deploying Docker images. 

In this blog, you'll learn how to configure Docker for GitHub Actions and set up GitHub Actions for Docker builds before moving on to discuss the best practices and advanced usage scenarios for using GitHub Actions with Docker.

Setting up Docker for GitHub Actions

Before we dive into GitHub Actions, let's make sure we have Docker set up correctly. You'll need to have it installed on your local machine and have a Dockerfile for your application. If you're not familiar with Docker, check out the official Docker documentation for a detailed guide on getting started. 

Here are the steps for setting up Docker for GitHub Actions going forward.

  1. Create a Dockerfile that specifies the software dependencies, configuration settings, and other requirements for your application.
  2. Build the Docker image using the command: ‘docker build -t <image-name>’
  3. Push it to a registry such as Docker Hub using the command: ‘docker push <username>/<image-name>:<tag>’
  4. Set up GitHub Actions in your repository by creating a ‘.github/workflows/<workflow-name>.yml’ file. Add the following code in it, and replace ‘<workflow-name>’, ‘<username>’, ‘<image-name>’, and ‘<tag>’ with the appropriate values for your project:

name: <workflow-name>
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build the Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: <username>/<image-name>:<tag>
- name: Deploy to production
uses: docker/metadata-action@v3
with:
images: <username>/<image-name>:<tag>

  1. Run the GitHub Actions workflow by pushing a new commit to your repository.

Configuring GitHub Actions for Docker builds

To configure GitHub Actions for Docker builds, you need to create a Dockerfile that specifies the software dependencies and other requirements for your application. Then, you can configure GitHub Actions to use the Dockerfile and build the Docker image. This is done by creating a GitHub Actions workflow that defines the steps for building the Docker image and pushing it to a container registry such as Docker Hub. You must also set up secrets in your GitHub repository to authenticate with the container registry.

Source: GitHub

To that end, create a new file named ‘.github/workflows/docker-build.yml’ in the root directory of your repository. This file defines the workflow for building Docker images. Here's a simple example:

name: Docker Build
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker Image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/my-node-app:${{ github.sha }}

This runs on every push event to the repository. The job runs on an Ubuntu latest runner and has two steps: checkout the repository code and build and push the Docker image. The tags parameter specifies the name and version of the Docker image and the env parameter specifies the Docker registry credentials, which are stored in secrets in the repository settings.

Save the workflow file and commit it to your repository. GitHub Actions will automatically run the workflow whenever you push changes to the repository.

Best practices for using GitHub Actions for Docker build

For Docker builds in GitHub Actions, there are some recommended practices to follow in order to optimize your workflows and ensure that your Docker images are built efficiently:

  • Keep Dockerfiles up-to-date:  Updating your Dockerfiles regularly will help ensure that your images are built using the latest software and security updates. Utilize the most recent base image available and refrain from using specific software versions that may quickly become obsolete.
  • Use multi-stage builds: To optimize the size of your Docker images, try implementing multi-stage builds. This separates the build environment from the production environment, allowing you to keep only the essential artifacts in the final image. The result is more streamlined and effective images.
  • Cache build steps: To quicken Docker builds, caching can be used to reuse previously built layers. This can be especially advantageous for larger projects with numerous dependencies. In GitHub Actions, caching can be activated using the --cache-from  flag in the Docker build command.

Advanced usage of GitHub Actions for Docker builds

In addition to basic Docker builds, GitHub Actions can also support more advanced use cases with Docker. Some of these use cases are mentioned below. 

  1. Docker Compose with GitHub Actions: With Docker Compose, it's possible to define and run multi-container Docker applications. GitHub Actions provides a way to build and test Docker Compose applications using the docker-compose command.
  2. Automated tests in Docker images: Docker images can be leveraged to execute automated tests on your codebase, verifying that it's working as intended prior to production deployment. GitHub Actions enables building and testing Docker images with popular testing frameworks such as pytest and JUnit.
  3. Kubernetes deployment with Docker images: Kubernetes clusters can accept Docker images as deployment artifacts through tools like kubectl or Helm charts. GitHub Actions can facilitate building and deploying Docker images to Kubernetes clusters, leading to a streamlined deployment process.

Incorporating these advanced use cases into your GitHub Actions workflows can help you streamline DevOps process and increase productivity.

Conclusion

In conclusion, using GitHub Actions to automate Docker builds can result in substantial time and effort savings for developers and enhance the quality and consistency of their code. By adhering to the instructions in this article, you can quickly establish and customize GitHub Actions to automate the building and deployment of Docker containers for your projects.

To maximize the benefits of GitHub Actions for Docker builds, it's important to apply best practices such as caching, image tagging, and secure storage of secrets. Advanced techniques like building multi-architecture images and publishing to multiple container registries can further streamline your workflow and increase efficiency.

Are you interested in learning more about automation and streamlining your development workflow? With Kubiya, you can automate repetitive tasks and workflows, freeing up more time for you to focus on the things that matter most. 

Join our public sandbox and start taking your development process to the next level.

Devops
Automation

What’s Interesting ?