DEV Community

Cover image for From Commit to Registry: A Guide to Automate Docker Image Builds with Github Actions
Dhanush Reddy
Dhanush Reddy

Posted on • Updated on

From Commit to Registry: A Guide to Automate Docker Image Builds with Github Actions

Github Actions is a powerful tool that allows developers to automate their software development workflows. It allows triggering actions, such as building and deploying code, in response to events such as a code push or a pull request. You may save time and effort by automating your development process with Github Actions custom workflows.

Github Actions is integrated directly into Github, making it easy to set up and use. It can be used to automate a wide range of tasks. One of the most popular use cases for Github Actions is automating the build and deployment of Docker images. By using Github Actions, you can automate the process of building and pushing Docker images to a registry, such as Docker Hub. This can save you a significant amount of time and effort, as well as ensuring that your images are built and deployed consistently and correctly.

In this blog post, we'll go through how to use Github Actions to automate the creation and distribution of Docker images. We will be covering everything from setting up the necessary prerequisites to writing the code.

Why automate Docker image builds?

That's a good question. Automating the process of building and deploying Docker images can bring several benefits to your development process. Some of the main reasons to automate Docker image builds include:

  • Consistency: Automating the build process ensures that your images are built consistently and correctly every time. This can help to eliminate errors that may occur due to manual processes, such as typos or missed steps.

  • Speed: Automating the build process can save you a significant amount of time, as the build and deployment process can be done quickly and efficiently. Similarily, multi architecture images can be built in the same workflow.

  • Continuous Integration and Deployment: By automating the build process, you can easily integrate the process with the other steps of your development process, such as testing and deploying your code. This can help to ensure that your images are always up-to-date and ready to be deployed to production.

Prerequisites

Before diving into automating your Docker image builds with Github Actions, there are a few prerequisites that you will need to have in place. These include:

  • A Github account: In order to use Github Actions, you will need to have a Github account and be able to access the repository where you want to set up the automation.

  • A Docker Hub account: In order to push your images to Docker Hub, you will need to have an account and be logged in.

  • A Dockerfile: Your repository should contain a Dockerfile that describes the instructions to build your image.

Setting up Github Secrets

In order for your Github Actions workflow to push images to your Docker Hub account, you will need to set up secrets for your Docker Hub credentials in your Github repository. This will allow the workflow to securely access your account and perform the necessary actions.

To set up secrets:

  1. Go to the main page of your Github repository.
  2. Click on the "Settings" tab.
  3. Under the "Secrets and Variables" section, click on "Actions" and then on "New Repository Secret"
  4. Enter a name for the secret, as DOCKERHUB_USERNAME and enter your Docker Hub username as the value.
  5. Click "Add secret".
  6. You can generate an access token for your Docker Hub account on hub.docker.com/settings/security
  7. Repeat this process to add another secret for Dockerhub token. In the code that follows this section, I have kept the name for it as DOCKERHUB_TOKEN. So ensure you stick to the variable name that you define in your Github settings.

Once you've set up your secrets, you can reference them in your Github Actions workflow file using the syntax ${{ secrets.SECRET_NAME }}. This will allow the workflow to access your Docker Hub credentials without exposing them in plaintext in the file.

The secrets which we just created are encrypted and can only be accessed by GitHub Actions running on the same repository. By setting up secrets for your Docker Hub credentials, you can ensure that your automation process is secure and that your credentials are protected.

The Code

At the root of your Github Repo create a folder named as .github and inside it create another folder named as workflows. Now create a file named a build-and-push.yaml or any name as per your needs for writing the Github Action.

name: build-and-push

on:
  push:
    branches:
      - "main"

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3  # This step checkouts the code of the repository
      - name: Login to Docker Hub
        uses: docker/login-action@v2 # This step logs in to the Docker Hub using the secrets
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2 # This step sets up Buildx, a tool that allows building multi-arch images
      - name: Build and push
        uses: docker/build-push-action@v3 # This step performs the actual build and push to Docker Hub
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/go-server:latest
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          # Cache the image layers
          cache-from: type=gha
          cache-to: type=gha,mode=max

Enter fullscreen mode Exit fullscreen mode

The code starts by defining the name of the workflow and the on event, which is a push to the main branch.

Then, there is a jobs block, where we define a job named build. This job will run on an ubuntu-latest machine.

The steps block defines the steps that the job will perform. In this case, the steps are:

  • Check out the code from the repository.
  • Login to the Docker Hub using the secrets that we set up earlier.
  • Set up Docker Buildx, a tool that allows building multi-architecture images.
  • Build and push the image to Docker Hub, using the information from the Dockerfile.

Make sure to change the tags in the above code with your Docker Hub Repo that you want to create. In my case I have kept is as go-server

This code provides a simple and effective way to automate the process of building and pushing Docker images to a registry like Docker Hub. With this code, you can ensure that your images are always up-to-date and ready to be deployed.

The Dockerfile I have used for this Github Action was a simple Golang HTTP Server. You can checkout the entire code here

Conclusion

In conclusion, Github Actions is a powerful tool that allows you to automate many processes in your development workflow, including building and pushing Docker images. By automating the process of building and pushing Docker images, you can save a lot of time and effort, and ensure that your images are always up to date.

In case if you still have any questions regarding this post or want to discuss something with me feel free to connect on LinkedIn or Twitter.

If you run an organization and want me to write for you, please connect with me on my Socials 🙃

Top comments (0)