← Writing

Deploy Spring Boot with Docker — from building the JAR to rolling back the image

On this page

To deploy a Spring Boot app with Docker, record the digest of the image your build host pushed and use that digest on the server. If the response checks fail, reapply the last known-good digest in the same Compose project to restore the previous image. This guide assumes an existing Maven app on Java 21 and Spring Boot 3.5.16, plus a single Linux host with Docker Engine and Compose.

Maven verifies the source and produces an executable JAR. Build and push the image, record its digest, and deploy it with Compose. Check health and the app route. On failure, reapply the last known-good digest and repeat the checks.

The new and previous images go through the same Compose and response checks.

Build the executable JAR with Maven

Declare spring-boot-starter-web, spring-boot-starter-actuator, and spring-boot-maven-plugin in your existing pom.xml. Set <finalName>app</finalName> inside <build>. The spring-boot-starter-parent preconfigures the plugin’s repackage execution. With a different parent POM, explicitly add the repackage goal under the plugin’s <executions>. From the project root, mvn -B verify runs the configured checks and packaging and produces an executable target/app.jar. Spring Boot Maven Plugin

Here is a minimal example for src/main/java/example/deploy/DeployDemoApplication.java. For your existing app, use its entry point and a representative route. In the example, GET / returns spring-deploy-demo, and the default Actuator configuration provides /actuator/health. Spring Boot Actuator

package example.deploy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DeployDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DeployDemoApplication.class, args);
    }

    @GetMapping("/")
    public String home() {
        return "spring-deploy-demo";
    }
}

Separate the build and runtime in the Dockerfile

Put this .dockerignore at the project root to leave build artifacts and Git metadata out of the build context.

target/
.git/
build-metadata.json
release-*.env

Put the Dockerfile in the same directory. The first stage builds the JAR with Maven. The runtime stage copies only the JAR. The process runs with UID and GID 10001. If your production app writes files, give this UID and GID write access to the required directories or volumes. Docker · Multi-stage builds

FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /src
COPY pom.xml .
COPY src ./src
RUN mvn -B verify

FROM eclipse-temurin:21-jre-jammy
WORKDIR /app
COPY --from=build /src/target/app.jar /app/app.jar
USER 10001:10001
ENTRYPOINT ["java", "-jar", "/app/app.jar"]

Push to the registry and record the digest

Run this in CI or on a build host with Buildx and jq. Replace ghcr.io/example/spring-app with your registry path and use credentials with write access for the push. The target Linux host in this example is x86-64, so the build uses linux/amd64. Use linux/arm64 for an ARM64 host. The block stops if the push or digest lookup fails. Docker · buildx build · GitHub Container Registry

(
  set -e
  IMAGE_REPO=ghcr.io/example/spring-app
  VERSION=1.0.0

  docker buildx build \
    --platform linux/amd64 \
    --tag "$IMAGE_REPO:$VERSION" \
    --metadata-file build-metadata.json \
    --push .

  DIGEST=$(jq -er '."containerimage.digest"' build-metadata.json)
  printf 'APP_IMAGE=%s@%s\n' "$IMAGE_REPO" "$DIGEST" > "release-$VERSION.env"
)

The 1.0.0 tag helps you find the release. For deployment, use the APP_IMAGE=registry-path@sha256:… reference written to release-1.0.0.env. Keep the last known-good release file and retain its image in the registry so the host can still pull it. Docker · Image digests

Deploy to the Linux host and check responses

Put compose.yaml and the release files in the same deployment directory on the server. For a private image, give the server read-only registry credentials. Keep the build host’s write credentials on the build side and inject application secrets through the server’s secret management.

services:
  app:
    image: "${APP_IMAGE:?Set APP_IMAGE to the release digest}"
    ports:
      - "127.0.0.1:8080:8080"
    restart: unless-stopped

Run the commands in the same directory and Compose project. unset APP_IMAGE keeps a shell variable from overriding the release file. Check that config --images prints the intended digest, then use the same release file to pull the image and replace the service. When running the commands one by one, stop at any failed step. Docker Compose · Variable interpolation · Docker Compose · up

(
  set -e
  unset APP_IMAGE
  docker compose --env-file release-1.0.0.env config --images
)
(
  set -e
  unset APP_IMAGE
  docker compose --env-file release-1.0.0.env pull app
  docker compose --env-file release-1.0.0.env up -d --no-build app
)

Retry the health request while the app starts. Check that the two responses are {"status":"UP"} and spring-deploy-demo, respectively. If startup or either response differs from what you expect, check startup logs with docker compose --env-file release-1.0.0.env logs --tail=100 app.

(
  set -e
  curl -fsS --retry 12 --retry-delay 2 --retry-max-time 30 --retry-connrefused \
    --max-time 5 http://127.0.0.1:8080/actuator/health
  printf '\n'
  curl -fsS --max-time 5 http://127.0.0.1:8080/
  printf '\n'
)

The port is published on the host’s 127.0.0.1:8080. With Docker Engine older than 28.0.0, hosts on the same layer-2 network can also reach ports published to localhost. If the service is public, check a representative external URL through the reverse proxy as well. Docker · Port publishing

Restore the last known-good digest

If release-0.9.0.env is the last known-good release, run the following commands in the same directory and project. After startup, check both responses again and, for a public service, repeat the proxy check. Replacing a container on a single host can cause a brief interruption.

(
  set -e
  unset APP_IMAGE
  docker compose --env-file release-0.9.0.env config --images
)
(
  set -e
  unset APP_IMAGE
  docker compose --env-file release-0.9.0.env pull app
  docker compose --env-file release-0.9.0.env up -d --no-build app
)

Rolling back the image leaves the database schema and data in their current state. Before deploying, check that the previous app can still read and write against the migrated database. Use the database’s separate recovery procedure if you need to restore data. Spring Boot and PostgreSQL rollback exercise

What was run

On December 1 KST, mvn -q -B verify passed on macOS ARM64 with Zulu Java 21.0.1, Maven 3.9.5, and Spring Boot 3.5.16. The sample contained no application tests. The executable target/app.jar ran in a local JVM and returned {"status":"UP"} for health and spring-deploy-demo for GET /. Compose 2.20.2-desktop.1 config resolved the sample digest and port mapping. Docker CLI 24.0.5 could not connect to the engine. Image build and execution, registry push, and remote deployment and rollback are documented procedures only.

Discussion

View on GitHub ↗