How to Publish Docker Image to Dockerhub Using Github Actions?
Last Updated :
13 Mar, 2023
Pre-requisites: GitHub Actions, Docker Hub
Publishing Docker images to Docker Hub using GitHub Actions is a simple and efficient process that can automate your image-building and publishing process.
What are Github Actions?
GitHub Actions is a CI/CD (Continuous Integration/Continuous Deployment) platform integrated with GitHub. It enables developers to automate software workflows, such as building, testing, and deploying code, directly from their GitHub repositories. With GitHub Actions, developers can write custom scripts, called "Actions," that run on pre-configured virtual environments, known as "runners," in response to specified events, such as pushes to a branch or pull requests. The Actions can be run on the GitHub platform, on the developer's servers, or third-party cloud providers, providing a flexible and scalable CI/CD solution for software development. GitHub Actions is a paid feature, but free tiers are available for open-source projects. The following are required to set up GitHub actions.
- Please note you should already have a functioning application. The application can be written in any language.
- The Dockerfile of the application should be created already. The Dockerfile should be correct.
- You should have a GitHub account.
- You should have your docker hub account and docker hub repository.
- You should have the credentials that we use to login into Docker Hub.
- The Dockerfile along with the application should be present in the GitHub repository.
For this demonstration, we have added the source codes here.
In the demonstration, we considered a node application and created a Dockerfile for the same.
The Dockerfile that we have used for our application looks something like this:
If you do not have a docker hub repository you can create one by following these steps.
- Go to your docker hub account and select create. Add a name for the app e.g node-app
Step 1: Click on the Actions tab at the top of the GitHub repository.
Step 2: Click on the setup workflow yourself to create a new pipeline.
Step 3: Add the following code to the workflow.
Please note that the path to the repository should be changed before saving. The code rishaw00/node-app should be changed to <your-docker-username>/<your-repo-name> in the workflow.
Step 4: Before saving the workflow, Add Dockerhub credentials in the secrets:
Set up the GitHub Secrets: To authenticate with Docker Hub, you'll need to provide your Docker Hub username and password. You can store them as secrets in GitHub. Go to the repository's Settings > Secrets and add two secrets: `DOCKER_USERNAME` and `DOCKER_PASSWORD`.
Once this is done we can save the workflow.
Step 5: Trigger the Workflow:
Once you've created the workflow and added the secrets, you can trigger it by pushing code to the main branch of your repository.
Now, to check the workflow we can go to the actions tab and see the workflow in the process.
Step 6: Verify the Docker Image:
You can verify that the Docker image has been published to Docker Hub by logging into Docker Hub and checking the Tags section of your repository.
Similar Reads
How To Build Docker Image In GitHub Actions ? GitHub Actions is a powerful tool provided by GitHub to automate tasks directly within your repositories. It plays a crucial role in Continuous Integration/Continuous Deployment (CI/CD) pipelines, enabling developers to automate workflows, tests, and deployments seamlessly. With GitHub Actions, work
5 min read
How do I use Docker with GitHub Actions? Docker packages all the components required for software into containers, much like a recipe box. By automating chores in your development workflow, GitHub Actions serves as your personal chef. When combined, they optimize software development by effectively packaging applications and smoothly autom
5 min read
Docker - Using Public Repositories To Host Docker Images Docker is a software platform for creating isolated virtualized environments for building, deploying, and testing applications with ease. In this tutorial, we will learn how to host public repositories on docker hub which is a hosted repository service provided by Docker for finding and sharing cont
10 min read
Docker - Publishing Images to Docker Hub Docker is a container platform that facilitates creating and managing containers. In this article, we will see how docker stores the docker images in some popular registries like Dockerhub and how to publish the Docker images to Docker Hub. By publishing the images to the docker hub and making it pu
8 min read
How to Push Docker Image to AWS ECS Fargate Using Terraform? ECS helps to manage containers and this service is provided by AWS, it allows users to easily manage and scale docker containers on a cluster of ec2 instances, without having to manage the underlying infrastructure. What is ECS Fargate?Fargate provides a way to run containerized applications on a se
8 min read