Skip to content

Warn when running in a container without being PID 1#48479

Open
EspenRoth wants to merge 1 commit intokeycloak:mainfrom
EspenRoth:warn-non-pid1-container
Open

Warn when running in a container without being PID 1#48479
EspenRoth wants to merge 1 commit intokeycloak:mainfrom
EspenRoth:warn-non-pid1-container

Conversation

@EspenRoth
Copy link
Copy Markdown

@EspenRoth EspenRoth commented Apr 25, 2026

Summary

When KC_RUN_IN_CONTAINER=true but the Keycloak process is not PID 1, graceful shutdown may fail silently because signals such as SIGTERM are not forwarded correctly. This can lead to cache inconsistencies or data loss.

This change adds a startup warning in kc.sh to alert users to use exec in their entrypoint scripts, with a link to the existing container documentation that already describes the correct approach. The warning is only emitted for server commands (start, start-dev) and is skipped for non-server commands such as import, export, and bootstrap-admin.

Changes

  • quarkus/dist/src/main/content/bin/kc.sh — added PID 1 check with warning message printed at startup when running in a container without being PID 1; command detection refined to identify the first non-flag argument so that flags like -v or -cf preceding the subcommand are handled correctly
  • quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/JavaOptsScriptTest.java — added three tests covering the presence and absence of the warning

Test plan

  • testContainerNonPid1Warning — asserts warning appears when KC_RUN_IN_CONTAINER=true and command is start
  • testNoContainerNonPid1Warning — asserts warning is absent when env var is not set
  • testContainerNonPid1WarningAbsentForNonServerCommands — asserts warning is absent for non-server commands (e.g. export)
  • All existing JavaOptsScriptTest tests pass
  • spotless:check passes

Notes

Claude Code was used to assist in writing the code, tests, and PR description for this contribution. All changes have been reviewed and are fully understood by the author.

Closes #48059


# Warn if running in a container without being PID 1, as signals may not be forwarded correctly and graceful shutdown may not work
if [ "$KC_RUN_IN_CONTAINER" = "true" ] && [ "$$" != "1" ]; then
echo "WARNING: Keycloak is running inside a container, but is not PID 1. Graceful shutdown may not work. Use 'exec' in your entrypoint script to ensure signals are forwarded correctly. See https://www.keycloak.org/server/containers for more details."
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only additional consideration here is that this warning shouldn't matter for non-server mode commands (import, export, boostrap-admin). Is it worth accounting for this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a look! Yeah I did not consider that. Will modify to exclude those scenarios

case "$1" in
-D*) SERVER_OPTS="$SERVER_OPTS ${OPT}";;
*) CONFIG_ARGS="$CONFIG_ARGS ${OPT}"
[ -z "$KC_CMD" ] && KC_CMD="$1"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this assumption won't generally hold. You are allowed to put arguments like -v or -cf ahead of command.

We could refine this to be the first argument without a leading -, or to avoid any additional arugment parsing in kc.sh we could instead set the initiating pid as an environment variable or system property and let the java logic emit the warning.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shawkins I think I've addressed this concern if you could take another look?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me kick off the ci run. I'll try to think of any additional corner cases.

When KC_RUN_IN_CONTAINER=true but the process is not PID 1, graceful
shutdown may fail silently because signals are not forwarded correctly.
This adds a warning at startup to alert users to use exec in their
entrypoint scripts.

Closes keycloak#48059

Signed-off-by: Espen Roth <[email protected]>
@EspenRoth EspenRoth force-pushed the warn-non-pid1-container branch from 110d400 to d1b6524 Compare April 27, 2026 19:34
@shawkins shawkins requested review from Pepo48, Copilot and vmuzikar April 29, 2026 17:53
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Safeguard against wrong PID in containers

3 participants