AuraWorks
EcosystemJune 10, 20232 min read

Spring Boot Docker Images with GitHub Actions

Automate the Docker image build for a Spring Boot application with GitHub Actions, from the workflow file to pushing the tagged image.

In this tutorial, we will learn how to use GitHub Actions to automate the build process for a Docker image of a Spring Boot application. By automating the build process, we can ensure that our Docker image is always up to date and ready for deployment.

Prerequisites

  • A Spring Boot application.
  • A GitHub account and repository for your application.
  • Docker installed on your local machine.

Step 1: Create a Dockerfile

Create a Dockerfile for your Spring Boot application. This Dockerfile should specify the base image for your application, copy the jar file for your application into the image, and specify the command to run the application when the Docker container is started.

dockerfile
FROM openjdk:8-jdk-alpine
WORKDIR /app
COPY target/*.jar app.jar

ENTRYPOINT ["java","-jar","/app/app.jar"]

Step 2: Create a GitHub Action

Create a GitHub Action to automate the build process. In the Action, specify the steps for building the Spring Boot application, building the Docker image, and pushing the image to a Docker registry.

Here is an example of a build-and-push.yml file for a GitHub Action that builds and pushes a Spring Boot application to a Docker registry:

yaml
on:
  push:
    branches:
      - master
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Build and test
        run: ./mvnw clean package
      - name: Build Docker image
        run: |
          docker build -t my-spring-boot-app .
          docker tag my-spring-boot-app my-docker-registry/my-spring-boot-app
          docker push my-docker-registry/my-spring-boot-app

Make sure to replace “my-spring-boot-app” and “my-docker-registry/my-spring-boot-app” with the names of your Docker image and Docker registry.

With these steps, you should be able to use GitHub Actions to automate the build and push process for a Docker image of your Spring Boot application.

This workflow assumes a working Dockerfile. If you do not have one yet, Spring Boot in Docker writes it from scratch, and Code Analysis with SonarCloud and GitHub Actions adds a quality gate to the same pipeline.

From the same desk

AuraLyrics

If you listen to music while you write code, AuraLyrics puts the lyrics over your editor instead of behind another window.

brew install --cask ozcberkay/tap/auralyrics

Notes like this one, once a month

Java, AWS and what shipping a side product actually costs.

No tracking pixels, unsubscribe in one click.