How to use GitHub Actions to push and build a Docker image to Docker Hub

How to use GitHub Actions to push and build a Docker image to Docker Hub

  1. Fork the repository: Go to the repository link (github.com/Shikhar99-lab/githubactions) and click on the "Fork" button at the top-right corner. This will create a copy of the repository under your GitHub account. (I'm providing my GitHub repository link which contains a basic code.)

  2. Clone the repository: On your local machine, open a terminal or command prompt and use the following command to clone the repository to your local machine:

bashCopy codegit clone https://github.com/YourUsername/githubactions.git

Replace YourUsername with your GitHub username.

  1. Navigate to the repository: Move into the cloned repository by using the following command:
bashCopy codecd githubactions
  1. Create a Docker Hub account: If you don't have a Docker Hub account, sign up for one at hub.docker.com/signup.

  2. Configure Docker Hub credentials: In order to push the Docker image to Docker Hub, you need to set up Docker Hub credentials as secrets in your GitHub repository. To do this, go to your repository page on GitHub, click on "Settings" at the top-right corner, and then click on "Secrets" in the left sidebar. Add two secrets:

    • DOCKER_USERNAME: Set this to your Docker Hub username.

    • DOCKER_PASSWORD: Set this to your Docker Hub password or access token.

  3. Modify the workflow file: Open the .github/workflows/docker.yml file in a text editor. This file contains the workflow configuration.

  4. Update the docker.yml file:

    • Replace <DOCKER_HUB_USERNAME> with your Docker Hub username on lines 22, 23, and 33.

    • Replace <DOCKER_HUB_REPOSITORY> with your desired repository name on lines 22, 23, 33, and 37.

  5. Commit and push changes: Save the modified docker.yml file. Then, commit the changes and push them to your GitHub repository:

sqlCopy codegit add .
git commit -m "Update workflow file"
git push origin main
  1. Enable workflows: On the GitHub repository page, go to the "Actions" tab. GitHub Actions should automatically detect the workflow file and display it. Click on the green "Enable" button to activate the workflow.

  2. Monitor the workflow: Once the workflow is enabled, it will be triggered automatically whenever you push changes to the repository. You can monitor the progress and view the build logs on the "Actions" tab.

  3. Verify the Docker image on Docker Hub: After the workflow completes successfully, go to your Docker Hub account and navigate to the repository specified in the workflow. You should see the Docker image pushed from the GitHub repository.

That's it! You have successfully set up GitHub Actions to push and build a Docker image to Docker Hub.