diff --git a/docker/Dockerfile.jvm-package b/docker/Dockerfile.jvm-package new file mode 100644 index 0000000000..33cf69e617 --- /dev/null +++ b/docker/Dockerfile.jvm-package @@ -0,0 +1,83 @@ +# JVM image built from an artifact that already exists on the host: +# ./mvnw package -DskipTests && docker build -f docker/Dockerfile.jvm-package -t floci/floci:dev . +# +# The counterpart to Dockerfile.native-package, for the JVM runtime: same idea, same reason. It +# exists for the local loop: docker/Dockerfile runs Maven inside the build, which re-resolves every +# dependency and wants a couple of gigabytes, so iterating on a change through it is slow and can +# fall over on a machine with little headroom. Consuming target/quarkus-app takes seconds. +# +# Everything below the FROM is a copy of the runtime stage of docker/Dockerfile, which is the +# canonical definition. Exactly two lines differ, both by design: the default VERSION, and where +# the application directory is copied from. DockerfileRuntimeParityTest enforces that, so a change +# to the canonical runtime stage fails the build here until it is mirrored. +FROM eclipse-temurin:25-jre-noble + +ARG VERSION=dev +ENV FLOCI_VERSION=${VERSION} +ENV FLOCI_STORAGE_PERSISTENT_PATH=/app/data + +ENV AWS_DEFAULT_REGION=us-east-1 +ENV AWS_ACCESS_KEY_ID=test +ENV AWS_SECRET_ACCESS_KEY=test +ENV AWS_CONFIG_FILE=/etc/floci/aws/config + +# Privilege drop happens in the entrypoint through coreutils chroot, so no gosu +# download; wget stays for the HEALTHCHECK below. +RUN set -eux; \ + apt-get update; \ + apt-get install -y --no-install-recommends ca-certificates passwd python3 python3-pip wget; \ + rm -rf /var/lib/apt/lists/*; \ + pip3 install --no-cache-dir --break-system-packages awscli boto3; \ + aws_bin="$(command -v aws)"; \ + mv "$aws_bin" /usr/bin/aws.real; \ + printf '#!/bin/sh\nexec /usr/bin/aws.real --endpoint-url=http://localhost:4566 "$@"\n' > /usr/bin/aws; \ + chmod +x /usr/bin/aws; \ + useradd -r -u 1001 -g root -d /app -s /sbin/nologin floci + +WORKDIR /app + +RUN mkdir -p /app/data /etc/floci/aws \ + && printf '[default]\nendpoint_url = http://localhost:4566\n' > /etc/floci/aws/config \ + && chown 1001:root /app \ + && chmod "g+rwX" /app \ + && chown 1001:root /app/data + +COPY target/quarkus-app/ quarkus-app/ +RUN chown -R 1001:root /app/quarkus-app + +COPY --chmod=755 docker/entrypoint.sh /usr/local/bin/docker-entrypoint.sh +COPY --chmod=755 docker/localstack-parity.sh /usr/local/bin/localstack-parity.sh + +VOLUME /app/data + +EXPOSE 4566 + +# Need to expose separately due to https://github.com/floci-io/floci/issues/2095. +# This preseres compatibility with https://github.com/docker-java/docker-java/blob/002b6eb7aa2b892be729c7c2a5e95f782924db38/docker-java-api/src/main/java/com/github/dockerjava/api/model/ExposedPort.java#L119 +EXPOSE 6379 +EXPOSE 6380 +EXPOSE 6381 +EXPOSE 6382 +EXPOSE 6383 +EXPOSE 6384 +EXPOSE 6385 +EXPOSE 6386 +EXPOSE 6387 +EXPOSE 6388 +EXPOSE 6389 +EXPOSE 6390 +EXPOSE 6391 +EXPOSE 6392 +EXPOSE 6393 +EXPOSE 6394 +EXPOSE 6395 +EXPOSE 6396 +EXPOSE 6397 +EXPOSE 6398 +EXPOSE 6399 + +HEALTHCHECK --interval=5s --timeout=3s --retries=5 \ + CMD wget -q --spider http://localhost:4566/_floci/health || exit 1 + +ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] +CMD ["java", "--enable-native-access=ALL-UNNAMED", "-jar", "/app/quarkus-app/quarkus-run.jar"] diff --git a/src/test/java/io/github/hectorvent/floci/tools/DockerfileRuntimeParityTest.java b/src/test/java/io/github/hectorvent/floci/tools/DockerfileRuntimeParityTest.java new file mode 100644 index 0000000000..3e1539248d --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/tools/DockerfileRuntimeParityTest.java @@ -0,0 +1,125 @@ +package io.github.hectorvent.floci.tools; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * Pins {@code docker/Dockerfile.jvm-package} to the runtime stage of {@code docker/Dockerfile}. + * + *

The local-package image exists only to skip the in-image Maven build, so everything it does + * after the {@code FROM} is meant to be the canonical image's runtime stage verbatim. Nothing + * enforced that, and the first copy silently dropped the {@code EXPOSE 6379-6399} block that exists + * for the docker-java compatibility workaround in issue #2095: an image that looked equivalent + * published a different port set. + * + *

Two lines are allowed to differ and are listed in {@link #INTENDED_DIFFERENCES}. Everything + * else, comments included, has to match, so a change to the canonical runtime stage fails here + * until it is mirrored. + */ +class DockerfileRuntimeParityTest { + + private static final Path CANONICAL = Path.of("docker/Dockerfile"); + private static final Path LOCAL_PACKAGE = Path.of("docker/Dockerfile.jvm-package"); + + /** + * Canonical line to the line the local-package image carries instead. + * + *

The version default, because this image is built from a working tree rather than a + * release, and where the application comes from, which is the whole point of the file. Both + * sides are asserted present, so an entry cannot quietly stop applying. + */ + private static final Map INTENDED_DIFFERENCES = new LinkedHashMap<>(Map.of( + "ARG VERSION=latest", "ARG VERSION=dev", + "COPY --from=build /build/target/quarkus-app/ quarkus-app/", + "COPY target/quarkus-app/ quarkus-app/")); + + @Test + void theLocalPackageImageMirrorsTheCanonicalRuntimeStage() { + List canonical = runtimeStage(CANONICAL, lastFrom(read(CANONICAL))); + List local = runtimeStage(LOCAL_PACKAGE, onlyFrom(read(LOCAL_PACKAGE))); + + List expected = canonical.stream() + .map(line -> INTENDED_DIFFERENCES.getOrDefault(line, line)) + .toList(); + + for (int i = 0; i < Math.min(expected.size(), local.size()); i++) { + if (!expected.get(i).equals(local.get(i))) { + fail("docker/Dockerfile.jvm-package has drifted from the runtime stage of " + + "docker/Dockerfile at line " + (i + 1) + " of that stage.\n" + + " canonical: " + expected.get(i) + "\n" + + " local: " + local.get(i) + "\n" + + "Mirror the canonical change, or add the line to INTENDED_DIFFERENCES " + + "with a reason if it is meant to differ."); + } + } + assertEquals(expected.size(), local.size(), + "the two runtime stages have different lengths; docker/Dockerfile.jvm-package must " + + "carry every instruction of the canonical runtime stage, " + + "EXPOSE block included"); + } + + /** + * Guards the allow-list itself: an entry whose canonical side has been edited away would + * otherwise sit there looking like it still permits something. + */ + @Test + void everyIntendedDifferenceIsStillPresentOnBothSides() { + String canonical = String.join("\n", read(CANONICAL)); + String local = String.join("\n", read(LOCAL_PACKAGE)); + INTENDED_DIFFERENCES.forEach((canonicalLine, localLine) -> { + assertTrue(canonical.contains(canonicalLine), + "docker/Dockerfile no longer contains the allow-listed line: " + canonicalLine); + assertTrue(local.contains(localLine), + "docker/Dockerfile.jvm-package no longer contains its replacement: " + localLine); + }); + } + + /** The canonical file is multi-stage; its runtime stage is the last one. */ + private static int lastFrom(List lines) { + for (int i = lines.size() - 1; i >= 0; i--) { + if (lines.get(i).startsWith("FROM ")) { + return i; + } + } + return fail("docker/Dockerfile has no FROM instruction"); + } + + /** The local-package file has no build stage, so a second FROM would mean it grew one. */ + private static int onlyFrom(List lines) { + int found = -1; + for (int i = 0; i < lines.size(); i++) { + if (lines.get(i).startsWith("FROM ")) { + if (found >= 0) { + fail("docker/Dockerfile.jvm-package has more than one FROM; it is meant to be a " + + "single runtime stage consuming a prebuilt target/quarkus-app"); + } + found = i; + } + } + return found >= 0 ? found : fail("docker/Dockerfile.jvm-package has no FROM instruction"); + } + + private static List runtimeStage(Path file, int from) { + return read(file).subList(from, read(file).size()); + } + + private static List read(Path file) { + try { + return Files.readAllLines(file, StandardCharsets.UTF_8); + } catch (IOException e) { + throw new UncheckedIOException("Could not read " + file, e); + } + } +}