DALL-E 3

Using GitHub Actions to automatically build Docker images

Szabolcs Toth
4 min readMar 27, 2024

--

When we use cloud resources we usually face the challenge that performance is enough to run a containerised application but it is not enough to build it from scratch, especially if during the build, we need to create binaries as well.

Github Actions can help us a lot, connecting it to dockerhub will make our process totally effortless.

The steps:
1. Create a repository on GitHub
2. Create a repository on dockerhub
3. Create an Access Token on dockerhub
4. Add the Access Token to the GitHub repository
5. Create the “Action”
6. Upload the Dockerfile

1. Create new public/private repository on GitHub

2. Create new public/private repository on dockerhub

  • Set a name and description to your repo

3. Create an Access Token on dockerhub

  • Click on your avatar on the top right corner, then “My account”
  • In the ‘Security” menu, click on “New Access Token”
  • Set a description to your new token , then save the token as it is displayed only once

4. Add the Access Token to your GitHub repository

  • Navigate to “Settings”
  • We need to set credentials for our GitHub Actions under “Secrets and Variables”
  • Click on “New repository secret”
  • Create “DOCKERHUB_TOKEN” and paste the Access Token you got on dockerhub
  • Create “DOCKERHUB_USERNAME” and add your username

6. Create a new Action

  • Add the following script:
name: Build and push Docker image to dockerhub

on:
push:
branches: [ "main" ]

jobs:

build:

runs-on: ubuntu-latest

steps:
- name: Check out the repo
uses: actions/checkout@v4

- name: Login to dockerhub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push to dockerhub
uses: docker/build-push-action@v5
with:
context:
push: true
tags: kicsipixel/githubaction:latest

6. Upload the Dockerfile

  • Clone the repository to your machine:

git clone https://github.com/kicsipixel/GitHubAction.git

  • Add the your Dockerfile. Here I use a simple one:
#This is a very simple, sample Image 
FROM ubuntu

RUN apt-get update
CMD [“echo”,”Image created”]
  • Add and commit it:

git add .

git commit -m "Dockerfile was added

  • Push it to GitHub:

git push origin main

  • You can see that the Action automatically runs, when you push to ‘main’ branch.
  • Once the build is successful, you have the image on dockerhub

As you can see it is very easy to create some automation with GitHub, we use anyway for version controlling. This way you can save time and resources in your deployment environment by usiong pre-built image in your docker-compose.yml file like this:

services:
app:
image: kicsipixel/githubaction:latest

Note: Consider use a different branch than ‘main’ in your Action script to avoid rebuild the image every time when you change something in your repo.

Update: In case you would like to pull the new image and restart the server you can add the following to docker_image.yml:

deploy:
needs: build
name: Deploy image on OCI compute instance
runs-on: ubuntu-latest

steps:
- name: install ssh keys
run: |
install -m 600 -D /dev/null ~/.ssh/id_rsa
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts
- name: connect and pull
run: ssh ubuntu@${{ secrets.SSH_HOST }} "cd ${{ secrets.WORK_DIR }} && sudo docker compose down app && sudo docker compose pull && sudo docker compose up app -d && exit"
- name: cleanup
run: rm -rf ~/.ssh

--

--