DEV Community

Hossain Chisty
Hossain Chisty

Posted on

CI/CD pipeline setup: Building and pushing Docker images to Docker Hub using GitHub Actions

Image description

The present article aims to elucidate the process of pushing a Docker image to DockerHub via GitHub Actions. By automating the building and pushing of Docker images to DockerHub, developers can reap various advantages such as enhanced consistency, efficiency, version control, ease of deployment, and scalability.

Create a GitHub repository and place a Dockerfile in the root directory.

Let's create a workflow with the following steps:

Check out the repository: This step will check out the code from your repository.

- name: Checkout code
  uses: actions/checkout@v2
Enter fullscreen mode Exit fullscreen mode

Log in to Docker Hub: This step will log in to Docker Hub using your Docker Hub username and password.

- name: Login to Docker Hub
  uses: docker/login-action@v1
  with:
    username: ${{ secrets.DOCKER_USERNAME }}
    password: ${{ secrets.DOCKER_PASSWORD }}
Enter fullscreen mode Exit fullscreen mode

Note: You will need to add your Docker Hub username and password as secrets in your repository settings.

Build the Docker image: This step will build the Docker image using the Dockerfile in your repository.

- name: Build Docker image
  run: docker build -t <dockerhub-username>/<image-name>:<tag> .
Enter fullscreen mode Exit fullscreen mode

Note: Replace with your Docker Hub username, image name, and tag for the image.

Here's the complete workflow file:

name: Build and Push Docker Image

on:
  push:
    branches: [main]

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Login to Docker Hub
      uses: docker/login-action@v1
      with:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Build Docker image
      run: docker build -t <dockerhub-username>/<image-name>:<tag> .

    - name: Push Docker image to Docker Hub
      run: docker push <dockerhub-username>/<image-name>:<tag>
Enter fullscreen mode Exit fullscreen mode

Save this file as docker.yml in the .github/workflows/ directory of your repository, and GitHub Actions will automatically build and push the Docker image to Docker Hub every time you push changes to the main branch.

In case you don't know how to add secrets to your GitHub repository, follow these steps:

  1. Go to your repository on GitHub and click on the "Settings" tab.

  2. Click on "Secrets" in the left-hand menu.

  3. Click on "New repository secret".

  4. Give your secret a name (e.g. DOCKER_USERNAME or DOCKER_PASSWORD).

  5. Enter the value of the secret in the "Value" field. Click on "Add secret".

I hope you found this article informative and useful for your Docker image building and pushing needs. If you have any questions or comments, feel free to leave them below. I look forward to seeing you in the next article!

Take Care. Keep smiling. :)

Top comments (2)

Collapse
 
maurerkrisztian profile image
Krisztián Maurer

Nice article! I recenlty discovered that GitHub has its own Container registry, which means there's no need to create a Docker Hub account and you can manage them from one place. This makes things easier if your code is already on GitHub.
Also actions/checkout and docker/login-action has newer versions: actions/checkout@v3, docker/login-action@v2

Collapse
 
hossainchisty profile image
Hossain Chisty

Thanks you Maurer! Yeah I also discovered that really excited to test it.