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.
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@8Creating 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
