Operations 4 min read

Automate Docker Image Builds and Pushes with GitHub Actions

Learn how to set up a GitHub Actions workflow that builds a Docker image from a Dockerfile, tags it, logs into Docker Hub using repository secrets, and automatically pushes the image upon creating a new release tag.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Automate Docker Image Builds and Pushes with GitHub Actions

This guide walks through creating a GitHub repository, adding a Dockerfile, and configuring a GitHub Actions workflow that builds, tags, and pushes a Docker image to Docker Hub.

Creating the GitHub Repository

Start by creating a new repository on GitHub. Add a Dockerfile at the root; the example builds an Angular CLI image using Node 12 Alpine. Adjust the Dockerfile content if you need a different base image.

# ./Dockerfile
FROM node:12-alpine as node-angular-cli
LABEL authors="Tinywan"
# Linux setup
# I got this from another, deprecated Angular CLI image.
# I trust developer, so I continued to use this, but you can leave it out if you want.
RUN apk update \
  && apk add --update alpine-sdk \
  && apk del alpine-sdk \
  && rm -rf /tmp/* /var/cache/apk/* *.tar.gz ~/.npm \
  && npm cache verify \
  && sed -i -e "s/bin\/ash/bin\/sh/" /etc/passwd
# Angular CLI
RUN npm install -g @angular/cli@8

Creating the GitHub Action

In the repository, open the Actions tab and select a new workflow. The workflow will log in to Docker Hub using secrets you define (Docker Hub username and password) so credentials are not exposed in the code.

Tagging and Releasing

After the workflow file is committed, create a new release tag via the Releases section of the repository. The tag name becomes the Docker image tag. Publishing the release triggers the workflow to build and push the image.

Viewing Workflow Output

Monitor the workflow execution on the Actions tab to see logs and debug if needed. After a successful run, verify the new image appears in your Docker Hub repository.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Dockerci/cdworkflowGitHub ActionsDocker Hub
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.