Skip to content

Commit

Permalink
feat,fix(ct-base): add extension point for background script IQSS#8932
Browse files Browse the repository at this point in the history
By moving from tini to dumb-init, we can offer a new extension point:
if an application image extending this base image provides an executable
script at ${SCRIPT_DIR}/startInBackground.sh, it will be executed
after the init scripts and in parallel to the application server.

By adding ${SCRIPT_DIR} to $PATH, we can now also skip variable expansion,
fixing a bug: formerly, the "exec" in entrypoint.sh and startInForeground.sh
where not replacing the shell properly.

The switch to dumb-init makes sure signals will be transferred also to any
background processes!
  • Loading branch information
poikilotherm committed Sep 14, 2022
1 parent 4aa2c01 commit 358f1a2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
10 changes: 5 additions & 5 deletions modules/container-base/src/main/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ENV PAYARA_DIR="${HOME_DIR}/appserver" \
ADMIN_PASSWORD="admin" \
DOMAIN_NAME="domain1" \
PAYARA_ARGS=""
ENV PATH="${PATH}:${PAYARA_DIR}/bin" \
ENV PATH="${PATH}:${PAYARA_DIR}/bin:${SCRIPT_DIR}" \
DOMAIN_DIR="${PAYARA_DIR}/glassfish/domains/${DOMAIN_NAME}" \
DEPLOY_PROPS="" \
PREBOOT_COMMANDS="${CONFIG_DIR}/pre-boot-commands.asadmin" \
Expand Down Expand Up @@ -88,7 +88,7 @@ EOF

ARG JATTACH_VERSION="v2.1"
ARG JATTACH_CHECKSUM="07885fdc782e02e7302c6d190f54c3930afa10a38140365adf54076ec1086a8e"
ARG PKGS="jq imagemagick curl unzip wget acl dirmngr gpg lsof procps netcat tini"
ARG PKGS="jq imagemagick curl unzip wget acl dirmngr gpg lsof procps netcat dumb-init"

# Installing the packages in an extra container layer for better caching
RUN <<EOF
Expand Down Expand Up @@ -214,9 +214,9 @@ RUN <<EOF
EOF

# Set the entrypoint to tini (as a process supervisor)
ENTRYPOINT ["/usr/bin/tini", "--"]
# JSON syntax should be used, but bypassed shell. Thus re-add expansion via shell exec.
CMD ["sh", "-c", "${SCRIPT_DIR}/entrypoint.sh"]
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
# This works because we add ${SCRIPT_DIR} to $PATH above!
CMD ["entrypoint.sh"]

LABEL org.opencontainers.image.created="@git.build.time@" \
org.opencontainers.image.authors="Research Data Management at FZJ <forschungsdaten@fz-juelich.de>" \
Expand Down
17 changes: 16 additions & 1 deletion modules/container-base/src/main/docker/scripts/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/bin/bash
#!/usr/bin/dumb-init /bin/bash
##########################################################################################################
#
# This script is a fork of https://github.com/payara/Payara/blob/master/appserver/extras/docker-images/
# server-full/src/main/docker/bin/entrypoint.sh and licensed under CDDL 1.1 by the Payara Foundation.
#
##########################################################################################################

# This shellscript is supposed to be executed by https://github.com/Yelp/dumb-init to keep subprocesses
# and zombies under control. If the ENTRYPOINT command is changed, it will still use dumb-init because shebang.
# dumb-init takes care to send any signals to subshells, too! (Which might run in the background...)


# Execute any scripts BEFORE the appserver starts
for f in "${SCRIPT_DIR}"/init_* "${SCRIPT_DIR}"/init.d/*; do
# shellcheck disable=SC1090
case "$f" in
Expand All @@ -15,4 +21,13 @@ for f in "${SCRIPT_DIR}"/init_* "${SCRIPT_DIR}"/init.d/*; do
echo
done

# If present, run a startInBackground.sh in the background (e.g. to run tasks AFTER the application server starts)
if [ -x "${SCRIPT_DIR}/startInBackground.sh" ]; then
echo "[Entrypoint] running ${SCRIPT_DIR}/startInBackground.sh in background"
"${SCRIPT_DIR}"/startInBackground.sh &
fi

# Start the application server and make it REPLACE this shell, so init system and Java directly interact
# Remember - this means no code below this statement will be run!
echo "[Entrypoint] running ${SCRIPT_DIR}/startInForeground.sh in foreground"
exec "${SCRIPT_DIR}"/startInForeground.sh "${PAYARA_ARGS}"

0 comments on commit 358f1a2

Please sign in to comment.