Skip to main content

GitHub Actions Workflow: Build a Docker Container Image and Push the Image to DockerHub

438 words·
GitHub GitHub Actions CI Pipeline DockerHub Docker
Table of Contents

DockerHub
#

DockerHub Access Token
#

Create a DockerHub access token:

  • Open the DockerHub settings: https://hub.docker.com/settings/security

  • Go to: “Security” > “Personal access tokens”

  • Define a access token description like GitHub

  • Select access permissions: “Read, Write Delete”

  • Click “Generate”

# User name
jueklu

# Access token
dckr_pat_some-token...

GitHub Actions Example
#

Overview
#

The following GitHub Action Workflow creates an example Docker image based on Apache and pushes it into to DockerHub.

The file and folder structure of the repository looks like this:

├── Dockerfile
├── .github
│   └── workflows
│       └── build.yaml
├── HTML
│   └── index.html

GitHub: https://github.com/jueklu/github-actions-workflow


Create Repository
#

Create a new private repository, mine is named github-actions-workflow-example.


Add Repository Secrets
#

Store the DockerHub access token and user name as GitHub repository secrets:

  • Go to: “Settings”

  • Select (Security) “Secrets and variables” > “Actions”

  • Click “New repository secret”

  • Add the following secrets:

# Name
DOCKERHUB_USERNAME

# Secret
jueklu
# Name
DOCKERHUB_TOKEN

# Secret
dckr_pat_some-token...

The secrets should look like this:


GitHub Actions Workflow
#

Actions Workflow Manifest
#

github/workflows/build.yaml
Build Manifest
name: Build Container Image, Push to DockerHub

on:
  push:
    branches:
      - main

# Job Declaration
jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

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

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          push: true
          tags: jueklu/github-actions-workflow-example:latest # DockerHub User / GitHub Repository
Build Manifest: Push Details

Optional, adopt the manifest to only run when changes in the HTML directory are pushed:

name: Build Container Image, Push to DockerHub

on:
  push:
    branches:
      - main
    paths:
      - "HTML/**" # Changes of the HTML files trigger the workflow

# Job Declaration
jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

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

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          push: true
          tags: jueklu/github-actions-workflow-example:latest # DockerHub User / GitHub Repository

Dockerfile
#

Dockerfile
FROM httpd:latest

COPY HTML/index.html /usr/local/apache2/htdocs/
EXPOSE 80

HTML
#

HTML/index.html
<!DOCTYPE html>
<html>

<head>
	<title>jklug.work</title>
</head>

<body>
	<h1>GitHub Actions Example</h1>
	<p>Build Container</p>
</body>

</html>

Verify the Workflow
#

Workflow Run
#

Verify the GitHub Actions Workflow:


Pull the Image & Run Container
#

# Pull & run the image from DockerHub
docker run -d --name example-app -p 8080:80 jueklu/github-actions-workflow-example

# Test the container
curl localhost:8080

DockerHub
#

Repository Settings
#

Change the repository image to private:

  • Open the “Repositories” tab

  • Select the “github-actions-workflow-example” repository

  • Go to “Settings”

  • Click “Make private”