Open In App

How do I use Docker with GitHub Actions?

Last Updated : 07 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 automating monotonous operations. While GitHub Actions automates tasks like testing and deployment, Docker guarantees consistency in application deployment. Docker integration with GitHub Actions produces an effective workflow for easily developing, testing, and deploying apps. This combination makes it easier for teams to produce high-quality software quickly and improves communication across the software development lifecycle.

Step-by-step instructions for using Docker with GitHub Actions

Step 1: Create the repository.

Step 2: Here is my repository maven web app.

repo

Here is the code i used and kept in the workflow file.

name: Java CI with Maven, Docker, and Cloud Run

on:
workflow_dispatch:
# push:
# branches: [ master ]
# pull_request:
# branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up JDK 11
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'adopt'
cache: maven

- name: Build with Maven
run: mvn -B package --file pom.xml

- name: Archive Artifact
uses: actions/upload-artifact@v2
with:
name: maven-web-application
path: /home/runner/work/maven-web-application/maven-web-application/target/maven-web-application.war

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

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

- name: Build and push Docker image to DockerHub
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: true
tags: larasai/maven-web-app:v1

- name: install the gcloud cli
uses: google-github-actions/setup-gcloud@v0
with:
project_id: ${{ secrets.GCLOUD_PROJECT_ID }}
service_account_key: ${{ secrets.GCLOUD_SA_KEY }}
export_default_credentials: true

- name: Build and push Docker image to Google Artifact Registry
run: |
gcloud auth configure-docker us-central1-docker.pkg.dev
docker pull larasai/maven-web-app:v1
docker tag larasai/maven-web-app:v1 us-central1-docker.pkg.dev/stone-trees-422304-p7/demo-app/maven-web-app:v1
docker push us-central1-docker.pkg.dev/stone-trees-422304-p7/demo-app/maven-web-app:v1

- name: Deploy to Cloud Run
uses: google-github-actions/deploy-cloudrun@v2
with:
service: maven-web-app
image: us-central1-docker.pkg.dev/stone-trees-422304-p7/demo-app/maven-web-app:v1
platform: managed
region: us-central1
project_id: ${{ secrets.GCLOUD_PROJECT_ID }}
service_account_key: ${{ secrets.GCLOUD_SA_KEY }}
flags: '--allow-unauthenticated'

Here is the detail explanation about the code.

  1. on:
    • This section defines when the workflow should run. In this case, it’s configured to run manually (workflow_dispatch), meaning someone needs to trigger it explicitly. The commented-out lines indicate configurations for running the workflow automatically on specific events like pushes to the master branch or pull requests.
  2. jobs:
    • This workflow contains one job named “build”, which defines a series of steps to execute.
  3. runs-on:
    • Specifies the type of virtual machine to run the job on. In this case, it’s set to ubuntu-latest.
  4. steps:
    • This section contains the individual tasks to be executed as part of the job.
  5. actions/checkout@v2:
    • Checks out the code repository into the runner’s workspace.
  6. actions/setup-java@v4:
    • Sets up Java 11 for the job using AdoptOpenJDK, and configures Maven caching for faster builds.
  7. Build with Maven:
    • Runs the Maven command to build the Java web application using the specified pom.xml file.
  8. Archive Artifact:
    • Uploads the built artifact (in this case, a .war file) to GitHub as an artifact, making it available for download in subsequent steps or workflows.
  9. docker/setup-buildx-action@v1:
    • Sets up Docker Buildx, a Docker CLI plugin for extended build capabilities.
  10. docker/login-action@v2:
    • Logs in to DockerHub using provided credentials stored in GitHub secrets.
  11. docker/build-push-action@v2:
    • Builds the Docker image using the specified Dockerfile, tags it with a version (larasai/maven-web-app:v1), and pushes it to DockerHub.
  12. google-github-actions/setup-gcloud@v0:
    • Sets up the Google Cloud SDK (gcloud) CLI for interacting with Google Cloud services.
  13. Build and push Docker image to Google Artifact Registry:
    • Configures Docker to authenticate with Google Artifact Registry, pulls the Docker image from DockerHub, retags it, and pushes it to Google Artifact Registry.
  14. Deploy to Cloud Run:
    • Deploys the Docker image to Google Cloud Run, a managed serverless platform, using the specified image from Google Artifact Registry. It configures the deployment to be accessible without authentication, using the provided Google Cloud Project ID and service account key.

Step 3: CLick on actions and click on the java ci with maven on left sisde and trigger the pipeline.

Step 4: The process was successfully executed here is the build.

Step 5: Refer the below image and the all steps was completed successfully.

Step 6: Here the image was successfully build and pushed to the docker hub refer the below image.

Step 7: We have stored the all secrets in the repository secrets.

Step 8: The docker build image was successfully pushed in to the google cloud artifact repository refer the below image. In service account we need to provide the sufficient permissions while pushing the image inro the artifact repository.

Step 9: The application was successfully deployed into the cloudrun service for your reference refer the below image.

Step 10: The revision was successfully created, here are the revision details and that is serving the traffic successfully.

Step 11: Here is the application tomcat web page.

Conclusion

Docker and GitHub Actions work together to streamline software development, testing, and deployment. Applications are cleanly packaged by Docker, guaranteeing consistent operation everywhere. Development is streamlined by GitHub Actions, which automates processes like testing and deployment. Teams may quickly set up and start the workflow by following the included step-by-step instructions. Software delivery is accelerated and cooperation is improved by this integration. Teams may confidently and effectively produce high-quality software by utilizing Docker and GitHub Actions.

Docker with GitHub Actions – FAQs

How to integrate Docker with GitHub?

Use Docker Hub’s automated builds for repositories to integrate Docker with GitHub. Link these repositories to GitHub repositories to enable smooth Docker image updates as code changes.

Does GitHub Actions use containers?

Yes, GitHub Actions can run workflows in containers, providing dependable and private environments for software development, testing, and deployment.

How to run a Docker image from GitHub?

A Docker image from GitHub can be launched by using the commands docker pull <username>/<repository>:<tag> to pull the image and docker run <username>/<repository>:<tag> to launch it.

Can you host Docker on GitHub?

Although GitHub can not directly host Docker images, you can keep them inside of your repositories using GitHub Packages.

Can I run Docker in GitHub Actions?

Yes, you may use the Docker-related actions offered by the GitHub Actions marketplace to run Docker in GitHub Actions. This allows you to perform operations like creating Docker images and launching containers without interruption in your workflows.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads