How do I use Docker with GitHub Actions?
Last Updated :
07 May, 2024
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.

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.
- 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.
- jobs:
- This workflow contains one job named "build", which defines a series of steps to execute.
- runs-on:
- Specifies the type of virtual machine to run the job on. In this case, it's set to
ubuntu-latest
.
- steps:
- This section contains the individual tasks to be executed as part of the job.
- actions/checkout@v2:
- Checks out the code repository into the runner's workspace.
- actions/setup-java@v4:
- Sets up Java 11 for the job using AdoptOpenJDK, and configures Maven caching for faster builds.
- Build with Maven:
- Runs the Maven command to build the Java web application using the specified
pom.xml
file.
- 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.
- docker/setup-buildx-action@v1:
- Sets up Docker Buildx, a Docker CLI plugin for extended build capabilities.
- docker/login-action@v2:
- Logs in to DockerHub using provided credentials stored in GitHub secrets.
- 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.
- google-github-actions/setup-gcloud@v0:
- Sets up the Google Cloud SDK (gcloud) CLI for interacting with Google Cloud services.
- 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.
- 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.
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 to Deploy a Go Web Application with Docker?
Pre-requisite: Docker A popular containerization tool called Docker enables developers to quickly package programs and their environments, enabling shorter iteration times and greater resource efficiency while delivering the same intended environment each time. A container orchestration tool called
3 min read
How to Publish Docker Image to Dockerhub Using Github Actions?
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) plat
3 min read
How to Run GUI Based Applications inside Docker?
A Docker Container is an isolated application platform that contains everything needed to run an application built from one or more images. Docker is an Open Source project that provides an open platform to run any number of applications inside a container according to your requirements and you can
7 min read
How to Run Bash Script in Github Actions ?
GitHub Actions are helpful resources for coding. They automate processes in your GitHub projects, save you time and effort. It is possible that GitHub Actions will automate the steps involved in testing, deploying, and alerting users of your code. Because you can configure them to run at specified t
5 min read
How to Use Docker Compose With Jenkins
In the fast-paced world of software development, it is mainly ensured by automation of repetition and task persistence. This is where Docker Compose and Jenkins come into play. Docker Compose is a tool for defining and running multi-container Docker applications. On the other hand, Jenkins is a wide
8 min read
How to Skip a Job in GitHub Actions ?
With the help of GitHub Actions, developers can effectively automate a range of processes and workflows right within their repositories. You can develop, test, and publish your code using GitHub Actions without ever leaving the GitHub website. It provides an adaptable and configurable workflow frame
6 min read
How To Use Docker For IoT Applications?
Docker is a super tool that makes our lives much less complicated by providing us with standardization, productivity, performance, maintainability, and compatibility of our code. It lets us continuously and hastily install and test our code, and it is platform-impartial. Docker provides the ability
8 min read
Creating a Docker Image with Git Installed
Version control is one of the most important aspects of any software development project and when we talk about version control, there is no better tool than Git. The majority of the developers depend upon Git to manage and share their project components among the team members. Even if you are runn
10 min read
How To Clone Private Git Repo With Dockerfile?
Cloning the private git repository in the Dockerfile is often used in situations where you want to build a Docker image of the source code or any assets from the private git repository. Cloning git will reduce the size of the docker image by separating the build environment from the runtime environm
5 min read