← Writing

Rehearse a Linux Deployment with Docker — From Replacing a Spring Boot Image to Rolling Back

On this page

Use Docker Desktop’s Linux VM to rehearse replacing and rolling back a Spring Boot image. Record the digests of two images pushed to a local registry, then start 0.9.0 → 1.0.0 → 0.9.0 in a single Compose project. Check Actuator health and the exact version returned by the app at every step. Start with Java 21, Maven, Actuator and the multistage Dockerfile from the previous Spring Boot deployment example.

Build two release images and push them to a local registry. Record the push digests, run Compose pull and up, then check health and version. Repeat with the previous digest after the replacement.

The first three steps in the diagram apply to each release. If the replacement fails, run the same checks with the last known good digest. After a successful replacement, switch back as well to rehearse recovery.

What to verify in each environment

Environment What to verify
Docker Desktop’s Linux VM Linux image builds, port publishing, local registry push and pull, Compose replacement and rollback
Separate Linux VM Production CPU architecture, systemd startup, file ownership and permissions, firewall, proxy
Staging Real TLS and domain, private registry permissions, external service connectivity, managed database

Docker Desktop runs the engine inside a Linux VM and forwards traffic from the host’s published ports to the VM. If production runs on x86-64, check the linux/amd64 image and its startup on that architecture even if the build succeeds on an ARM64 Mac. Docker Desktop networking · Multi-platform builds

Return the release version from the app

Replace home() in the preceding article’s DeployDemoApplication. Keep the existing GetMapping import and Actuator configuration.

@GetMapping("/")
public String home() {
    return System.getenv("APP_RELEASE");
}

Add these two lines to the Dockerfile’s runtime stage after the last FROM. The value passed at build time becomes a container environment variable.

ARG APP_RELEASE=dev
ENV APP_RELEASE=${APP_RELEASE}

Push two versions to a local registry

You need Bash, the Docker CLI and Compose, curl and jq. Run the blocks in order from the project root. Make sure the current context points to Docker Desktop on this machine, the engine responds, and ports 5500 and 18080, the registry name and the Compose project name are free. Push only personal test images to this HTTP registry.

(
  set -e
  docker context show
  docker info --format '{{.OSType}}/{{.Architecture}}'
  docker run -d --name spring-rehearsal-registry \
    -p 127.0.0.1:5500:5000 registry:3
  curl -fsS --retry 10 --retry-delay 1 --retry-connrefused \
    --retry-max-time 15 --max-time 2 http://127.0.0.1:5500/v2/
)

On Docker Engine releases earlier than 28.0.0, hosts on the same L2 network can reach ports published to localhost. On those versions, use a trusted, isolated network for the rehearsal. Use TLS and access controls for a real registry. Docker port publishing · Deploy a CNCF Distribution registry

Read the digest for the tag from the push output and check for 64 lowercase hexadecimal digits after sha256:. The block stops if the build, push or digest check fails. Each release file records APP_IMAGE in the form repo@sha256:…. Pull an image by digest

(
  set -euo pipefail
  repo=127.0.0.1:5500/spring-app

  for release in 0.9.0 1.0.0; do
    tag="$repo:$release"
    docker build --build-arg "APP_RELEASE=$release" -t "$tag" .
    push_output=$(docker push "$tag")
    printf '%s\n' "$push_output"

    digest=$(printf '%s\n' "$push_output" |
      awk -v tag="$release:" '$1 == tag && $2 == "digest:" {print $3}')
    if [[ ! $digest =~ ^sha256:[0-9a-f]{64}$ ]]; then
      printf 'Missing or invalid push digest for %s\n' "$tag" >&2
      exit 1
    fi
    printf 'APP_IMAGE=%s@%s\n' "$repo" "$digest" > "release-$release.env"
  done
)

Replace and roll back in the same Compose project

Save compose.yaml in the project root. APP_IMAGE is required, and the app port is published at 127.0.0.1:18080 on the host.

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

Run this Bash block in the same directory. It unsets any inherited APP_IMAGE so each --env-file supplies the value and prints the reference each time with config --images. It runs up only after a successful pull app. If a block fails, investigate before moving to the next one. Compose variable precedence · Compose up

After startup the block can begin checks for 30 seconds, with up to two seconds per HTTP request. To pass, /actuator/health must return a status of UP and the body of / must equal the version string exactly. The block prints app logs if up or the response check fails. If the new version fails, it attempts to restore the old one and exits with a failure status.

(
  set -euo pipefail
  unset APP_IMAGE
  base=http://127.0.0.1:18080

  deploy() {
    local release=$1
    local -a compose=(docker compose -p spring-rehearsal
      --env-file "release-$release.env")
    local deadline

    "${compose[@]}" config --images || return 1
    "${compose[@]}" pull app || return 1
    if ! "${compose[@]}" up -d --no-build --pull never app; then
      "${compose[@]}" logs --tail=100 app
      return 1
    fi

    deadline=$((SECONDS + 30))
    while (( SECONDS < deadline )); do
      if curl -fs --max-time 2 "$base/actuator/health" |
          jq -e '.status == "UP"' >/dev/null &&
         curl -fs --max-time 2 "$base/" |
          jq -eRs --arg release "$release" '. == $release' >/dev/null; then
        printf '%s\n' "$release"
        return 0
      fi
      sleep 1
    done
    printf 'Health or release check failed for %s\n' "$release" >&2
    "${compose[@]}" logs --tail=100 app
    return 1
  }

  deploy 0.9.0
  if ! deploy 1.0.0; then
    deploy 0.9.0
    exit 1
  fi
  deploy 0.9.0
)

With three successful checks, expect the version responses in this order.

0.9.0
1.0.0
0.9.0

Replacing a single container can briefly interrupt service. Rolling back the image leaves the database at its current schema and data. Check separately that the old app can read and write against the migrated schema. Spring Boot and PostgreSQL rollback rehearsal

After the rehearsal, remove the Compose project you created, the registry container and its anonymous volumes. The release files and cached images remain available for inspection.

(
  set -e
  unset APP_IMAGE
  docker compose -p spring-rehearsal --env-file release-0.9.0.env down
  docker rm -fv spring-rehearsal-registry
)

Verify startup with systemctl on a separate Linux VM

Use a separate systemd test VM with rootful Docker Engine’s docker.service and the Compose plugin. Put the same compose.yaml and a release-current.env containing the last verified image reference in /opt/spring-rehearsal. On the VM, 127.0.0.1 refers to the VM itself. The image reference must match the host, port and repository of a registry the VM can reach. Use a verified digest served by that registry and an image that matches the VM’s architecture. For private images, give the administrator performing the pull read access.

Check Docker’s absolute path. If it differs from /usr/bin/docker, update the commands below and both executable paths in the unit. Review the saved digest and pre-pull the image into the rootful engine.

(
  set -e
  command -v docker
  cd /opt/spring-rehearsal
  sudo /usr/bin/docker compose version
  sudo env -u APP_IMAGE /usr/bin/docker compose -p spring-rehearsal \
    --env-file release-current.env config --images
  sudo env -u APP_IMAGE /usr/bin/docker compose -p spring-rehearsal \
    --env-file release-current.env pull app
)

Save the following as /etc/systemd/system/spring-rehearsal.service. The unit starts the locally cached image after the Docker service starts. Boot skips the pull to avoid waiting for a registry connection, authentication and download. The specified digest must already be available in this engine. systemd.service

[Unit]
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/opt/spring-rehearsal
ExecStart=/usr/bin/docker compose -p spring-rehearsal --env-file release-current.env up -d --no-build --pull never app
ExecStop=/usr/bin/docker compose -p spring-rehearsal --env-file release-current.env stop app

[Install]
WantedBy=multi-user.target

Verify and register the unit. Run the status and HTTP checks below before rebooting the test VM, then repeat them after it starts.

(
  set -e
  sudo systemd-analyze verify /etc/systemd/system/spring-rehearsal.service
  sudo systemctl daemon-reload
  sudo systemctl enable --now spring-rehearsal.service
)
sudo systemctl reboot

Reconnect, read the status and journal for the current boot and run the HTTP checks. This example assumes release-current.env points to the verified 0.9.0 image. active (exited) means the start command succeeded. Continuous monitoring and app health checks are separate.

sudo systemctl status spring-rehearsal.service --no-pager
sudo journalctl -b -u spring-rehearsal.service --no-pager

(
  set -euo pipefail
  expected=0.9.0
  base=http://127.0.0.1:18080
  deadline=$((SECONDS + 30))
  while (( SECONDS < deadline )); do
    if curl -fs --max-time 2 "$base/actuator/health" |
        jq -e '.status == "UP"' >/dev/null &&
       curl -fs --max-time 2 "$base/" |
        jq -eRs --arg release "$expected" '. == $release' >/dev/null; then
      printf '%s\n' "$expected"
      exit 0
    fi
    sleep 1
  done
  exit 1
)

What was actually checked

On macOS ARM64, Docker CLI 24.0.5 could not connect to the desktop-linux engine. Compose v2.20.2-desktop.1 resolved two dummy digests and the 18080:8080 port mapping and rejected a missing required APP_IMAGE value. The image build, registry push, container replacement and rollback, systemd unit registration and VM reboot were not run.

Discussion

View on GitHub ↗