From 6911f6921444f8fc14e5f2d1db6ea0ef361facce Mon Sep 17 00:00:00 2001 From: Guillaume Le Floch Date: Fri, 16 Oct 2020 16:56:41 +0200 Subject: [PATCH] Simplify dockerfile declaration in codestart --- .../base/Dockerfile-layout.include.qute | 48 ++++++++++++++++ .../main/docker/Dockerfile.tpl.qute.fast-jar | 57 +++---------------- .../src/main/docker/Dockerfile.tpl.qute.jvm | 57 +++---------------- .../main/docker/Dockerfile.tpl.qute.native | 2 +- .../QuarkusCodestartGenerationTest.java | 18 +++++- 5 files changed, 79 insertions(+), 103 deletions(-) create mode 100644 devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/Dockerfile-layout.include.qute diff --git a/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/Dockerfile-layout.include.qute b/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/Dockerfile-layout.include.qute new file mode 100644 index 0000000000000..c3980ad764f94 --- /dev/null +++ b/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/Dockerfile-layout.include.qute @@ -0,0 +1,48 @@ +#### +# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode +# +# Before building the container image run: +# +# {#insert quarkusbuild} mvn package {/} +# +# Then, build the image with: +# +# docker build -f src/main/docker/Dockerfile.{type} -t quarkus/{project.artifact-id}-{type} . +# +# Then run the container using: +# +# docker run -i --rm -p 8080:8080 quarkus/{project.artifact-id}-{type} +# +# If you want to include the debug port into your docker image +# you will have to expose the debug port (default 5005) like this : EXPOSE 8080 5050 +# +# Then run the container using : +# +# docker run -i --rm -p 8080:8080 -p 5005:5005 -e JAVA_ENABLE_DEBUG="true" quarkus/{project.artifact-id}-{type} +# +### +FROM {#insert image /} +{#insert args /} +ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' +# Install java and the run-java script +# Also set up permissions for user `1001` +RUN microdnf install curl ca-certificates $\{JAVA_PACKAGE} \ + && microdnf update \ + && microdnf clean all \ + && mkdir /deployments \ + && chown 1001 /deployments \ + && chmod "g+rwX" /deployments \ + && chown 1001:root /deployments \ + && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/$\{RUN_JAVA_VERSION}/run-java-sh-$\{RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \ + && chown 1001 /deployments/run-java.sh \ + && chmod 540 /deployments/run-java.sh \ + && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security + +# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size. +ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" +{#insert copy /} + +EXPOSE 8080 +USER 1001 + +ENTRYPOINT [ "/deployments/run-java.sh" ] diff --git a/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.fast-jar b/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.fast-jar index b33d7d4e1b983..6ce0cd733ef51 100644 --- a/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.fast-jar +++ b/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.fast-jar @@ -1,57 +1,16 @@ -#### -# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode -# -# Before building the container image run: -# -# mvn package -Dquarkus.package.type=fast-jar -# -# Then, build the image with: -# -# docker build -f src/main/docker/Dockerfile.fast-jar -t quarkus/{project.artifact-id}-fast-jar . -# -# Then run the container using: -# -# docker run -i --rm -p 8080:8080 quarkus/{project.artifact-id}-fast-jar -# -# If you want to include the debug port into your docker image -# you will have to expose the debug port (default 5005) like this : EXPOSE 8080 5050 -# -# Then run the container using : -# -# docker run -i --rm -p 8080:8080 -p 5005:5005 -e JAVA_ENABLE_DEBUG="true" quarkus/{project.artifact-id}-fast-jar -# -### -FROM {dockerfile.fast-jar.from} - +{#include Dockerfile-layout type='fast-jar'} + {#quarkusbuild}mvn package -Pquarkus.package.type=fast-jar{/quarkusbuild} + {#image}{dockerfile.fast-jar.from} {/image} + {#args} ARG JAVA_PACKAGE={dockerfile.fast-jar.java-package} ARG RUN_JAVA_VERSION={dockerfile.fast-jar.run-java-version} - -ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' - -# Install java and the run-java script -# Also set up permissions for user `1001` -RUN microdnf install curl ca-certificates $\{JAVA_PACKAGE} \ - && microdnf update \ - && microdnf clean all \ - && mkdir /deployments \ - && chown 1001 /deployments \ - && chmod "g+rwX" /deployments \ - && chown 1001:root /deployments \ - && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/$\{RUN_JAVA_VERSION}/run-java-sh-$\{RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \ - && chown 1001 /deployments/run-java.sh \ - && chmod 540 /deployments/run-java.sh \ - && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security - -# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size. -ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" - + {/args} + {#copy} # We make four distinct layers so if there are application changes the library layers can be re-used COPY --chown=1001 {buildtool.build-dir}/quarkus-app/lib/ /deployments/lib/ COPY --chown=1001 {buildtool.build-dir}/quarkus-app/*.jar /deployments/ COPY --chown=1001 {buildtool.build-dir}/quarkus-app/app/ /deployments/app/ COPY --chown=1001 {buildtool.build-dir}/quarkus-app/quarkus/ /deployments/quarkus/ + {/copy} +{/include} -EXPOSE 8080 -USER 1001 - -ENTRYPOINT [ "/deployments/run-java.sh" ] diff --git a/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.jvm b/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.jvm index 9c6079cfb05fa..f751c87b49730 100644 --- a/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.jvm +++ b/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.jvm @@ -1,54 +1,11 @@ -#### -# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode -# -# Before building the container image run: -# -# mvn package -# -# Then, build the image with: -# -# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/{project.artifact-id}-jvm . -# -# Then run the container using: -# -# docker run -i --rm -p 8080:8080 quarkus/{project.artifact-id}-jvm -# -# If you want to include the debug port into your docker image -# you will have to expose the debug port (default 5005) like this : EXPOSE 8080 5050 -# -# Then run the container using : -# -# docker run -i --rm -p 8080:8080 -p 5005:5005 -e JAVA_ENABLE_DEBUG="true" quarkus/{project.artifact-id}-jvm -# -### -FROM {dockerfile.jvm.from} - +{#include Dockerfile-layout type='jvm'} + {#image}{dockerfile.jvm.from} {/} + {#args} ARG JAVA_PACKAGE={dockerfile.jvm.java-package} ARG RUN_JAVA_VERSION={dockerfile.jvm.run-java-version} - -ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' - -# Install java and the run-java script -# Also set up permissions for user `1001` -RUN microdnf install curl ca-certificates $\{JAVA_PACKAGE} \ - && microdnf update \ - && microdnf clean all \ - && mkdir /deployments \ - && chown 1001 /deployments \ - && chmod "g+rwX" /deployments \ - && chown 1001:root /deployments \ - && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/$\{RUN_JAVA_VERSION}/run-java-sh-$\{RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \ - && chown 1001 /deployments/run-java.sh \ - && chmod 540 /deployments/run-java.sh \ - && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security - -# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size. -ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager" - + {/args} + {#copy} COPY {buildtool.build-dir}/lib/* /deployments/lib/ COPY {buildtool.build-dir}/*-runner.jar /deployments/app.jar - -EXPOSE 8080 -USER 1001 - -ENTRYPOINT [ "/deployments/run-java.sh" ] \ No newline at end of file + {/copy} +{/include} diff --git a/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.native b/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.native index 3c1fdfba8b729..290f4ec2b5e28 100644 --- a/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.native +++ b/devtools/platform-descriptor-json/src/main/resources/codestarts/quarkus/core/tooling/dockerfiles/base/src/main/docker/Dockerfile.tpl.qute.native @@ -24,4 +24,4 @@ COPY --chown=1001:root {buildtool.build-dir}/*-runner /work/application EXPOSE 8080 USER 1001 -CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] \ No newline at end of file +CMD ["./application", "-Dquarkus.http.host=0.0.0.0"] diff --git a/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/QuarkusCodestartGenerationTest.java b/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/QuarkusCodestartGenerationTest.java index 32d051f580520..52e3dcd607097 100644 --- a/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/QuarkusCodestartGenerationTest.java +++ b/integration-tests/devtools/src/test/java/io/quarkus/devtools/codestarts/QuarkusCodestartGenerationTest.java @@ -518,9 +518,21 @@ private void checkNoExample(Path projectDir) { private void checkDockerfiles(Path projectDir) { assertThat(projectDir.resolve(".dockerignore")).exists(); - assertThat(projectDir.resolve("src/main/docker/Dockerfile.jvm")).exists(); - assertThat(projectDir.resolve("src/main/docker/Dockerfile.native")).exists(); - assertThat(projectDir.resolve("src/main/docker/Dockerfile.fast-jar")).exists(); + assertThat(projectDir.resolve("src/main/docker/Dockerfile.jvm")).exists() + .satisfies(checkContains("mvn package")) + .satisfies(checkContains("docker build -f src/main/docker/Dockerfile.jvm")) + .satisfies(checkContains("registry.access.redhat.com/ubi8/ubi-minimal:8.1")) + .satisfies(checkContains("ARG JAVA_PACKAGE=java-11-openjdk-headless")) + .satisfies(checkContains("ENTRYPOINT [ \"/deployments/run-java.sh\" ]")); + assertThat(projectDir.resolve("src/main/docker/Dockerfile.fast-jar")).exists() + .satisfies(checkContains("mvn package -Pquarkus.package.type=fast-jar")) + .satisfies(checkContains("docker build -f src/main/docker/Dockerfile.fast-jar")) + .satisfies(checkContains("registry.access.redhat.com/ubi8/ubi-minimal:8.1")) + .satisfies(checkContains("ARG JAVA_PACKAGE=java-11-openjdk-headless")) + .satisfies(checkContains("ENTRYPOINT [ \"/deployments/run-java.sh\" ]")); + assertThat(projectDir.resolve("src/main/docker/Dockerfile.native")).exists() + .satisfies(checkContains("registry.access.redhat.com/ubi8/ubi-minimal:8.1")) + .satisfies(checkContains("CMD [\"./application\", \"-Dquarkus.http.host=0.0.0.0\"]")); } private void checkConfigProperties(Path projectDir) {