Automate Docker Workflow with GitHub Actions

Automate Docker Workflow with GitHub Actions

In the era of DevOps and continuous integration/continuous delivery (CI/CD), automating the workflow is a crucial aspect. One of the profound tools that help in such automation is GitHub Actions. GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. It allows you to write individual tasks, called actions, and combine them to create a custom workflow.

In this blog, we'll focus on a specific task — building a Docker image and pushing it to Docker Hub using GitHub Actions. This is vital as every little update in your code could potentially be pushed into production automatically, making the development life cycle smoother and faster.


Step 1: Setup GitHub Repository

- Begin by creating a new repository on GitHub for your project.

Article content


Step 2: Prepare Dockerfile

- In your repository, create a Dockerfile with the necessary configuration for building the Docker image. 

FROM ubuntu:20.04

RUN apt-get update

RUN apt-get install nginx curl net-tools vim -y        

Step 3: GitHub Secrets Configuration

- Navigate to the "Settings" of your repository, then "Secrets", and add the following secrets on the Actions window:

  - DOCKER_USERNAME - your Docker Hub username.

  - DOCKER_PASSWORD - your Docker Hub password.

Article content
Final look after successful adding of the docker credential


Step 4: Create GitHub Action

- Create a directory .github/workflows and inside it, create a YAML file for the GitHub Action, e.g., docker-build-and-push.yml. 

  Fill it with the following content:

name: Build and Push Docker image

on:

  push:

    branches:

      - main

jobs:

  build:

    runs-on: ubuntu-latest

    

    steps:

    - name: Checkout code

      uses: actions/checkout@v2

      

    - name: Log in to Docker Hub

      run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin

      

    - name: Build Docker image

      run: docker build . -t ${{ secrets.DOCKER_USERNAME }}/your-image-name:latest

      

    - name: Push Docker image to Docker Hub

      run: docker push ${{ secrets.DOCKER_USERNAME }}/your-image-name:latest

        

Step 5: Testing

- Make a commit to your main branch and observe the Actions tab in your GitHub repository to watch the workflow execute and validate if the Docker image gets pushed to Docker Hub successfully.



By transcending through the steps above, you've successfully knitted a GitHub Actions CI/CD pipeline to automate the orchestration of building a Docker image and propelling it to Docker Hub. Feels like a breeze, right?

This is just a glimpse of what's possible with GitHub Actions. As your project expands, you can stitch in additional steps and hooks to adapt to your project’s needs. It's power-packed features like these that intelligently bridge the transition from code to deploy, making life a tad easier for developers around the globe.

Amazing work Chukwudi.

Like
Reply
Temidayo Omoniyi

Platform Data Engineer • Cloud Solution Architect • Software Engineer • MCT • Author

1y

Thanks for sharing.

Like
Reply

To view or add a comment, sign in

More articles by Chukwudi O.

Insights from the community

Others also viewed

Explore topics