# Multi-stage build for JSON-Java project FROM maven:3.8.7-openjdk-17 AS build WORKDIR /build # Copy pom to leverage caching for dependencies COPY pom.xml . # Copy source and build COPY src ./src RUN mvn -q -DskipTests package FROM eclipse-temurin:17-jre WORKDIR /app # Create a non-root user to run the application RUN groupadd -r app && useradd -r -g app app USER app # Copy built artifacts from the builder stage COPY --from=build /build/target /app/target # Convenience launcher: attempt to run the built jar, otherwise drop into a shell RUN /bin/sh -lc ' JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1) || true; if [ -n "$JAR" ]; then echo "Found jar: $JAR"; fi '["" ] # Create a small entrypoint script to auto-run the jar if present RUN /bin/sh -lc ' set -e JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1) if [ -n "$JAR" ]; then echo "Executing jar: $JAR"; echo "#!/bin/sh\nset -e\nJAR=\"$JAR\"\nexec java -jar \"$JAR\"" > /app/run.sh else echo "#!/bin/sh\n\nexec /bin/sh" > /app/run.sh fi chmod +x /app/run.sh ' ENTRYPOINT ["/app/run.sh"]
Image maven:3.8.7-openjdk-17 NOT found on Docker Hub.
# Multi-stage build for JSON-Java project FROM maven:3.6.3-openjdk-8 AS build WORKDIR /build # Copy pom and first stage to leverage caching for dependencies COPY pom.xml ./ # Build the project (skip tests for a faster image build) COPY src ./src RUN mvn -B -q -DskipTests package FROM eclipse-temurin:8-jre WORKDIR /app # Create a non-root user to run the application RUN groupadd -r app && useradd -r -g app app USER app # Copy built artifacts from the builder stage COPY --from=build /build/target /app/target # Create a launcher script that will run the built jar if present RUN /bin/sh -lc ' JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1) || true; if [ -n "$JAR" ]; then echo "Found jar: $JAR"; echo "#!/bin/sh\nset -e\nJAR=\"$JAR\"\nexec java -jar \"$JAR\"" > /app/run.sh else echo "#!/bin/sh\nset -e\n# No jar found, drop into shell" > /app/run.sh fi chmod +x /app/run.sh ' ENTRYPOINT ["/app/run.sh"]
git node_modules target *.log .dockerignore Dockerfile *.md *.mdx *.png *.jpg *.jpeg *.gif
#0 building with "default" instance using docker driver #1 [internal] load build definition from Dockerfile #1 transferring dockerfile: 965B done #1 DONE 0.1s Dockerfile:25 -------------------- 23 | # Create a launcher script that will run the built jar if present 24 | RUN /bin/sh -lc ' 25 | >>> JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1) || true; 26 | if [ -n "$JAR" ]; then 27 | echo "Found jar: $JAR"; -------------------- ERROR: failed to build: failed to solve: dockerfile parse error on line 25: unknown instruction: JAR=$(ls
# Multi-stage build for JSON-Java project FROM maven:3.6.3-jdk-8 AS build WORKDIR /build # Copy pom and source for dependency caching COPY pom.xml ./ # Build the project (skip tests for a faster image build) COPY src ./src RUN mvn -B -q -DskipTests package FROM eclipse-temurin:8-jre WORKDIR /app # Create a non-root user to run the application RUN groupadd -r app && useradd -r -g app app USER app # Copy built artifacts from the builder stage COPY --from=build /build/target /app/target # Create a launcher script that will run the built jar if present RUN bash -lc 'cat > /app/run.sh <<"EOF" #!/bin/sh set -e JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1) if [ -n "$JAR" ]; then echo "Running $JAR" exec java -jar "$JAR" else echo "No jar found in /app/target; dropping to shell." exec /bin/sh fi EOF chmod +x /app/run.sh' ENTRYPOINT ["/app/run.sh"]
#0 building with "default" instance using docker driver #1 [internal] load build definition from Dockerfile #1 transferring dockerfile: 913B done #1 DONE 0.0s Dockerfile:26 -------------------- 24 | RUN bash -lc 'cat > /app/run.sh <<"EOF" 25 | #!/bin/sh 26 | >>> set -e 27 | JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1) 28 | if [ -n "$JAR" ]; then -------------------- ERROR: failed to build: failed to solve: dockerfile parse error on line 26: unknown instruction: set (did you mean user?)
# Multi-stage build for JSON-Java project FROM maven:3.6.3-jdk-8 AS build WORKDIR /build # Copy pom and source for dependency caching COPY pom.xml ./ COPY src ./src # Build the project (skip tests for a faster image build) RUN mvn -B -q -DskipTests package FROM eclipse-temurin:8-jre WORKDIR /app # Create a non-root user to run the application RUN groupadd -r app && useradd -r -g app app USER app # Copy built artifacts from the builder stage COPY --from=build /build/target /app/target # Entry point: try to run the built jar; fallback to an interactive shell ENTRYPOINT ["/bin/sh", "-c", "JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1); if [ -n \"$JAR\" ]; then echo Running $JAR; exec java -jar \"$JAR\"; else echo No jar found in /app/target; exec /bin/sh; fi"]
Concerns: COPY --from=build /build/target /app/target runs while the container user is 'app', but the directory /app was created with WORKDIR and is owned by root; thus this COPY will fail due to insufficient permissions., The USER app instruction is before COPY; it should be placed after the COPY so COPY runs as root, then switch to non-root for runtime., Overall, the approach is sound, but the permission issue must be fixed for the image to build successfully.
# Multi-stage build for JSON-Java project FROM maven:3.6.3-jdk-8 AS build WORKDIR /build # Copy pom and source for dependency caching COPY pom.xml ./ COPY src ./src # Build the project (skip tests for a faster image build) RUN mvn -B -q -DskipTests package FROM eclipse-temurin:8-jre WORKDIR /app # Copy built artifacts from the builder stage; run as root to allow this copy COPY --from=build /build/target /app/target # Create a non-root user to run the application RUN groupadd -r app && useradd -r -g app app USER app # Entry point: try to run the built jar; fallback to an interactive shell ENTRYPOINT ["/bin/sh", "-c", "JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1); if [ -n \"$JAR\" ]; then echo Running $JAR; exec java -jar \"$JAR\"; else echo No jar found in /app/target; exec /bin/sh; fi"]
Concerns: Final image runs as non-root; ensure the non-root user can read the built JAR in /app/target., Maven tests are skipped during build; consider enabling tests in CI for regression coverage. Smoke [PASS]: jarpath=$(ls /app/target/*.jar 2>/dev/null | head -n 1); if [ -n "$jarpath" ]; then echo "FOUND_JAR:$jarpath"; else echo "NO_JAR"; fi Smoke [PASS]: java -version 2>&1 | head -n 1 Smoke [FAIL]: jarcount=$(ls /app/target/*.jar 2>/dev/null | wc -l); echo "JAR_COUNT:$jarcount Output: sh: 1: Syntax error: Unterminated quoted string
# Multi-stage build for JSON-Java project FROM maven:3.6.3-jdk-8 AS build WORKDIR /build # Copy pom and source for dependency caching COPY pom.xml ./ COPY src ./src # Build the project (skip tests for a faster image build) RUN mvn -B -q -DskipTests package FROM eclipse-temurin:8-jre WORKDIR /app # Copy built artifacts from the builder stage; run as root to allow this copy COPY --from=build /build/target /app/target # Create a non-root user to run the application RUN groupadd -r app && useradd -r -g app app USER app # Entry point: try to run the built jar; fallback to an interactive shell ENTRYPOINT ["/bin/sh", "-c", "JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1); if [ -n \"$JAR\" ]; then echo Running $JAR; exec java -jar \"$JAR\"; else echo No jar found in /app/target; exec /bin/sh; fi"]
# Multi-stage build for JSON-Java project FROM maven:3.6.3-jdk-8 AS build WORKDIR /build # Copy pom and source for dependency caching COPY pom.xml ./ COPY src ./src # Build the project (skip tests for a faster image build) RUN mvn -B -q -DskipTests package FROM eclipse-temurin:8-jre WORKDIR /app # Copy built artifacts from the builder stage; run as root to allow this copy COPY --from=build /build/target /app/target # Create a non-root user to run the application RUN groupadd -r app && useradd -r -g app app # Create launcher script before switching to non-root so it is accessible RUN bash -lc 'cat > /app/run.sh << "EOS" #!/bin/sh set -e JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1) if [ -n "$JAR" ]; then echo "Running $JAR" exec java -jar "$JAR" else echo "No jar found in /app/target" exec /bin/sh fi EOS chmod +x /app/run.sh' USER app # Entry point: run the launcher script ENTRYPOINT ["/app/run.sh"]
#0 building with "default" instance using docker driver #1 [internal] load build definition from Dockerfile #1 transferring dockerfile: 974B done #1 DONE 0.1s Dockerfile:25 -------------------- 23 | RUN bash -lc 'cat > /app/run.sh << "EOS" 24 | #!/bin/sh 25 | >>> set -e 26 | JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1) 27 | if [ -n "$JAR" ]; then -------------------- ERROR: failed to build: failed to solve: dockerfile parse error on line 25: unknown instruction: set (did you mean user?)
# Multi-stage build for JSON-Java project FROM maven:3.6.3-jdk-8 AS build WORKDIR /build # Copy pom and source for dependency caching COPY pom.xml ./ COPY src ./src # Build the project (skip tests for a faster image build) RUN mvn -B -q -DskipTests package FROM eclipse-temurin:8-jre WORKDIR /app # Copy built artifacts from the builder stage; run as root to allow this copy COPY --from=build /build/target /app/target # Create a non-root user to run the application RUN groupadd -r app && useradd -r -g app app # Ensure app user can access /app RUN mkdir -p /app && chown -R app:app /app USER app # Entry point: run the launcher script; if no jar is present drop into a shell RUN /bin/bash -lc 'printf "%s\n" "#!/bin/sh" "set -e" "" "JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1)" "if [ -n \"\\$JAR\" ]; then" " echo Running \\\"$JAR\\\"" " exec java -jar \\\"$JAR\\\"" "else" " echo No jar found in /app/target" " exec /bin/sh" "fi" > /app/run.sh && chmod +x /app/run.sh' ENTRYPOINT ["/app/run.sh"]
Concerns: The build uses -DskipTests; unit tests won't run during image build. If you rely on test execution, consider enabling tests or running them in a separate stage., If the packaged artifact isn't a runnable jar, the container will drop into a shell rather than starting a service. This is acceptable for a library, but may be unexpected in some deployments., The entrypoint relies on a single jar found via glob; if multiple jars exist, behavior depends on the glob order (which could be non-deterministic). Smoke [PASS]: JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1); if [ -n "$JAR" ]; then echo Found jar: "$JAR"; else echo No jar found; fi Smoke [FAIL]: test -d /app/target && echo '/app/target exists' || echo '/app/target missing Output: sh: 1: Syntax error: Unterminated quoted string Smoke [FAIL]: test -x /app/run.sh && echo 'run.sh is executable' || echo 'run.sh is not executable Output: sh: 1: Syntax error: Unterminated quoted string
# Multi-stage build for JSON-Java project FROM maven:3.6.3-jdk-8 AS build WORKDIR /build # Copy pom and source for dependency caching COPY pom.xml ./ COPY src ./src # Build the project (skip tests for a faster image build) RUN mvn -B -q -DskipTests package FROM eclipse-temurin:8-jre WORKDIR /app # Copy built artifacts from the builder stage; run as root to allow this copy COPY --from=build /build/target /app/target # Create a non-root user to run the application RUN groupadd -r app && useradd -r -g app app # Ensure app user can access /app RUN mkdir -p /app && chown -R app:app /app USER app # Entry point: run the launcher script; if no jar is present drop into a shell RUN bash -lc 'cat > /app/run.sh << "EOS" #!/bin/sh set -e JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1) if [ -n "$JAR" ]; then echo "Running $JAR" exec java -jar "$JAR" else echo "No jar found in /app/target" exec /bin/sh fi EOS chmod +x /app/run.sh' ENTRYPOINT ["/app/run.sh"]
#0 building with "default" instance using docker driver #1 [internal] load build definition from Dockerfile #1 transferring dockerfile: 1.02kB done #1 DONE 0.0s Dockerfile:30 -------------------- 28 | RUN bash -lc 'cat > /app/run.sh << "EOS" 29 | #!/bin/sh 30 | >>> set -e 31 | JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1) 32 | if [ -n "$JAR" ]; then -------------------- ERROR: failed to build: failed to solve: dockerfile parse error on line 30: unknown instruction: set (did you mean user?)
# Multi-stage build for JSON-Java project FROM maven:3.6.3-jdk-8 AS build WORKDIR /build # Copy pom and source for dependency caching COPY pom.xml ./ COPY src ./src # Build the project (skip tests for a faster image build) RUN mvn -B -q -DskipTests package FROM eclipse-temurin:8-jre WORKDIR /app # Copy built artifacts from the builder stage; run as root to allow this copy COPY --from=build /build/target /app/target # Create a non-root user to run the application RUN groupadd -r app && useradd -r -g app app # Ensure app user can access /app RUN mkdir -p /app && chown -R app:app /app USER app # Entry point: try to run the built jar; fallback to an interactive shell ENTRYPOINT ["/bin/sh", "-c", "JAR=$(ls /app/target/*.jar 2>/dev/null | head -n 1); if [ -n \"$JAR\" ]; then echo Running $JAR; exec java -jar \"$JAR\"; else echo No jar found in /app/target; exec /bin/sh; fi"]
Smoke [PASS]: ls -la /app/target Smoke [PASS]: if ls /app/target/*.jar 1> /dev/null 2>&1; then echo JAR_FOUND; else echo NO_JAR_FOUND; fi Smoke [PASS]: java -version