Build and Push Docker Image to Docker Hub using GitHub Action

Introduction

GitHub Actions is a powerful continuous integration and deployment (CI/CD) tool provided by GitHub. It allows developers to automate software workflows and build, test, and deploy code changes from within the GitHub platform.

Docker Hub is a cloud-based registry service provided by Docker for storing and sharing containerized applications and images. Docker is a platform that enables developers to create, deploy, and run applications in containers. Containers are lightweight, portable, and self-sufficient units that encapsulate an application and its dependencies.

Docker image is a lightweight, standalone, and executable package that contains everything needed to run a software application, including the code, runtime environment, system tools, libraries, and settings. Docker images are built from a set of instructions called a Dockerfile, which defines the steps needed to create the image.

Prerequisites

  • Required GitHub repository.
  • Basic knowledge of YAML language.
  • Docker hub account.

In this post, We will show you how to create GitHub action’s workflow to build and push docker image to Docker hub image registry using GitHub Action.

Step 1: Creating Dockerfile

We need to create simple Dockerfile to build docker image and push on Docker hub image Registry, Use the following command for the same.

To create Dockerfile.

nano Dockerfile

Paste the following set of commands.

FROM ubuntu:16.04
RUN apt-get update && apt-get upgrade -y
RUN apt-get install apache2 -y
RUN rm /var/www/html/index.html
RUN echo "Hello DevOpstricks" > /var/www/html/index.html
EXPOSE 80
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

Save and push in Git repository.

Step 2: Configure Secret Variables

We need to the following secrets from the Docker hub that we will use the GitHub action’s secrets variable.

Collect the following Creds from docker hub account.

  • DOCKERHUB_USERNAME
  • DOCKERHUB_TOKEN

Go to the Setting tab and click on Secrets and variables button on left side, and the click on Action button.

and Create and update the Docker hub Creds over their.

Step 3: Creating Build and Push Workflow

We are good to create build and push the Docker image to the Docker hub using GitHub Action workflow.

Go to the Action tab.

Click on set up a workflow yourself button.

Now we need to the paste the following yaml configuration.

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "master" branch
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
      -
        name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/imagename:tag

Click on Commit Changes button.

Step 4: Validate Docker Image in Docker Hub

We need to go to the Docker repository to see the docker images.

Conclusion

We have successfully Build and push the Docker image to Docker Hub with GitHub Action , If you still have questions, please post them in the comments section below.

Author

Build and Push Docker Image to Docker Hub using GitHub Action

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top