diff --git a/appserver/extras/docker-images/micro/src/main/docker/Dockerfile b/appserver/extras/docker-images/micro/src/main/docker/Dockerfile index a7fc63468a0..6298abfb410 100644 --- a/appserver/extras/docker-images/micro/src/main/docker/Dockerfile +++ b/appserver/extras/docker-images/micro/src/main/docker/Dockerfile @@ -1,7 +1,7 @@ # # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. # -# Copyright (c) 2023 Payara Foundation and/or its affiliates. All rights reserved. +# Copyright (c) 2023-2025 Payara Foundation and/or its affiliates. All rights reserved. # # The contents of this file are subject to the terms of either the GNU # General Public License Version 2 only ("GPL") or the Common Development @@ -52,6 +52,9 @@ ENV PAYARA_DIR=${HOME_DIR} \ MEM_MAX_RAM_PERCENTAGE="70.0" \ MEM_XSS="512k" +# Add Tini +RUN apk add --no-cache tini + RUN true \ && mkdir -p "${HOME_DIR}" \ && addgroup --gid 1000 payara \ @@ -69,5 +72,5 @@ WORKDIR ${HOME_DIR} COPY --chown=payara:payara maven/bin/* ${SCRIPT_DIR}/ COPY --chown=payara:payara maven/artifacts/payara-micro.jar . -ENTRYPOINT ["/bin/sh", "entrypoint.sh"] -CMD ["--deploymentDir","/opt/payara/deployments"] +ENTRYPOINT ["tini", "--", "/opt/payara/entrypoint.sh"] +CMD ["--deploymentDir", "/opt/payara/deployments"] diff --git a/appserver/extras/docker-images/micro/src/main/docker/bin/entrypoint.sh b/appserver/extras/docker-images/micro/src/main/docker/bin/entrypoint.sh index b0ae2ce7aca..847d0fd2822 100644 --- a/appserver/extras/docker-images/micro/src/main/docker/bin/entrypoint.sh +++ b/appserver/extras/docker-images/micro/src/main/docker/bin/entrypoint.sh @@ -1,7 +1,7 @@ #!/bin/sh # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. # -# Copyright (c) 2023 Payara Foundation and/or its affiliates. All rights reserved. +# Copyright (c) 2023-2025 Payara Foundation and/or its affiliates. All rights reserved. # # The contents of this file are subject to the terms of either the GNU # General Public License Version 2 only ("GPL") or the Common Development @@ -39,4 +39,14 @@ set -e -exec java -XX:MaxRAMPercentage=${MEM_MAX_RAM_PERCENTAGE} -Xss${MEM_XSS} -XX:+UseContainerSupport ${JVM_ARGS} -jar payara-micro.jar "$@" +# Doing a Graceful Shutdown before container stops +trap 'echo "Stopping Payara Micro..."; + kill -TERM "$child" 2>/dev/null; + wait $child; + echo "Payara Micro stopped.";' SIGTERM + +exec java -XX:MaxRAMPercentage=${MEM_MAX_RAM_PERCENTAGE} -Xss${MEM_XSS} -XX:+UseContainerSupport ${JVM_ARGS} -jar payara-micro.jar "$@" & +child=$! + +# Wait Payara-Micro Process before finish the container +wait $child diff --git a/appserver/extras/docker-images/server-full/src/main/docker/Dockerfile b/appserver/extras/docker-images/server-full/src/main/docker/Dockerfile index 2d4f869065c..2324361d347 100644 --- a/appserver/extras/docker-images/server-full/src/main/docker/Dockerfile +++ b/appserver/extras/docker-images/server-full/src/main/docker/Dockerfile @@ -1,7 +1,7 @@ # # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. # -# Copyright (c) 2023-2024 Payara Foundation and/or its affiliates. All rights reserved. +# Copyright (c) 2023-2025 Payara Foundation and/or its affiliates. All rights reserved. # # The contents of this file are subject to the terms of either the GNU # General Public License Version 2 only ("GPL") or the Common Development @@ -111,5 +111,4 @@ RUN true \ ${PAYARA_DIR}/glassfish/domains/${DOMAIN_NAME}/logs \ && true -ENTRYPOINT ["tini", "--"] -CMD "${SCRIPT_DIR}/entrypoint.sh" +ENTRYPOINT ["tini", "--", "/opt/payara/scripts/entrypoint.sh"] diff --git a/appserver/extras/docker-images/server-full/src/main/docker/bin/entrypoint.sh b/appserver/extras/docker-images/server-full/src/main/docker/bin/entrypoint.sh index d2799a13790..50b6471439c 100644 --- a/appserver/extras/docker-images/server-full/src/main/docker/bin/entrypoint.sh +++ b/appserver/extras/docker-images/server-full/src/main/docker/bin/entrypoint.sh @@ -2,7 +2,7 @@ ################################################################################ # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. # -# Copyright (c) 2023 Payara Foundation and/or its affiliates. All rights reserved. +# Copyright (c) 2023-2025 Payara Foundation and/or its affiliates. All rights reserved. # # The contents of this file are subject to the terms of either the GNU # General Public License Version 2 only ("GPL") or the Common Development @@ -47,4 +47,13 @@ for f in ${SCRIPT_DIR}/init_* ${SCRIPT_DIR}/init.d/*; do echo done -exec ${SCRIPT_DIR}/startInForeground.sh $PAYARA_ARGS +# Doing a Graceful Shutdown before container stops +trap 'echo "Stopping Payara Server domain..."; ${PAYARA_DIR}/bin/asadmin --user=${ADMIN_USER} --passwordfile=${PASSWORD_FILE} stop-domain ${DOMAIN_NAME} & + pid=$!; wait $pid; + echo "Payara Server domain stopped.";' SIGTERM + +exec ${SCRIPT_DIR}/startInForeground.sh $PAYARA_ARGS & +payara_pid=$! + +# Wait Payara Process before finish the container +wait $payara_pid diff --git a/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java b/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java index 9133dc5d006..0b45f1064c2 100644 --- a/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java +++ b/appserver/extras/payara-micro/payara-micro-core/src/main/java/fish/payara/micro/impl/PayaraMicroImpl.java @@ -1,7 +1,7 @@ /* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * - * Copyright (c) [2016-2023] Payara Foundation and/or its affiliates. All rights reserved. + * Copyright (c) [2016-2025] Payara Foundation and/or its affiliates. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development @@ -73,6 +73,7 @@ import java.util.function.Function; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.logging.ConsoleHandler; import java.util.logging.Formatter; import java.util.logging.Handler; import java.util.logging.Level; @@ -1744,15 +1745,26 @@ private void deployAll() throws GlassFishException { LOGGER.log(Level.INFO, "Deployed {0} archive(s)", deploymentCount); } + private ConsoleHandler configureLogger(Logger logger) { + ConsoleHandler consoleHandler = new ConsoleHandler(); + consoleHandler.setLevel(Level.ALL); + logger.addHandler(consoleHandler); + logger.setLevel(Level.ALL); + return consoleHandler; + } + private void addShutdownHook() { Runtime.getRuntime().addShutdownHook(new Thread( - "GlassFish Shutdown Hook") { + "Payara Micro Shutdown Hook") { @Override public void run() { try { + ConsoleHandler handler = configureLogger(LOGGER); if (gf != null) { gf.stop(); gf.dispose(); + LOGGER.log(Level.INFO, "Payara Micro STOPPED."); + handler.flush(); } } catch (GlassFishException ex) { } catch (IllegalStateException ex) {