Skip to content

Add JAVA_OPTS support for configurable memory settings in Dockerfiles#53

Merged
jclausen merged 2 commits intodevelopmentfrom
add_JAVA_OPTS_support
Nov 24, 2025
Merged

Add JAVA_OPTS support for configurable memory settings in Dockerfiles#53
jclausen merged 2 commits intodevelopmentfrom
add_JAVA_OPTS_support

Conversation

@otisnado
Copy link
Contributor

No description provided.

Copy link
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.

Pull Request Overview

This pull request refactors memory configuration in BoxLang Docker images by centralizing JAVA_OPTS handling in the run.sh script rather than hardcoding it in Dockerfiles. The changes remove the BOXLANG_MINISERVER_OPTS environment variable (which was simply duplicating JAVA_OPTS) and add dynamic logic to construct JAVA_OPTS based on MIN_MEMORY and MAX_MEMORY environment variables at runtime.

Key changes:

  • Removed memory-related environment variables (MIN_MEMORY, MAX_MEMORY, JAVA_OPTS) from base Dockerfiles (JRE21.Dockerfile, Alpine.JRE21.Dockerfile)
  • Removed BOXLANG_MINISERVER_OPTS environment variable from all Web Dockerfiles while keeping memory defaults
  • Added runtime logic in run.sh to dynamically construct JAVA_OPTS based on user-provided or default memory values

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
builds/base/JRE21.Dockerfile Removed all memory-related environment variables (MIN_MEMORY, MAX_MEMORY, JAVA_OPTS, BOXLANG_MINISERVER_OPTS) to make base image more flexible
builds/base/Alpine.JRE21.Dockerfile Removed all memory-related environment variables to match JRE21.Dockerfile changes
builds/base/JRE21.Web.Dockerfile Removed BOXLANG_MINISERVER_OPTS variable while preserving MIN_MEMORY, MAX_MEMORY, and JAVA_OPTS defaults
builds/base/Alpine.JRE21.Web.Dockerfile Removed BOXLANG_MINISERVER_OPTS variable while preserving memory configuration defaults
builds/base/JRE21.Web.Nginx.Dockerfile Removed BOXLANG_MINISERVER_OPTS variable while preserving memory configuration defaults
build/run.sh Added dynamic JAVA_OPTS construction logic to handle MIN_MEMORY/MAX_MEMORY overrides and set sensible defaults

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

build/run.sh Outdated
# Check if JAVA_OPTS already has heap settings - preserve other options if present
if [[ $JAVA_OPTS =~ -Xms && $JAVA_OPTS =~ -Xmx ]]; then
# Remove existing heap settings and rebuild
JAVA_OPTS_CLEAN=$(echo "$JAVA_OPTS" | sed -E 's/-Xms[^ ]+//g; s/-Xmx[^ ]+//g; s/ +/ /g; s/^ //; s/ $//')
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

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

The sed command may produce incorrect results if JAVA_OPTS contains quoted strings with spaces or special characters. The pattern -Xms[^ ]+ and -Xmx[^ ]+ assumes options are space-delimited, which could break if JAVA_OPTS contains something like -XX:OnOutOfMemoryError="kill -9 %p" or other quoted arguments.

Additionally, if JAVA_OPTS ends with -Xms or -Xmx (no trailing space), the final cleanup regex s/ $//' won't catch it properly if the cleaned string is empty or has leading spaces from the replacement.

Consider using more robust parsing or documenting that JAVA_OPTS should not contain quoted strings with spaces.

Suggested change
JAVA_OPTS_CLEAN=$(echo "$JAVA_OPTS" | sed -E 's/-Xms[^ ]+//g; s/-Xmx[^ ]+//g; s/ +/ /g; s/^ //; s/ $//')
JAVA_OPTS_CLEAN=""
for opt in $JAVA_OPTS; do
if [[ $opt != -Xms* && $opt != -Xmx* ]]; then
JAVA_OPTS_CLEAN="$JAVA_OPTS_CLEAN $opt"
fi
done
JAVA_OPTS_CLEAN=$(echo "$JAVA_OPTS_CLEAN" | sed -E 's/^ //')

Copilot uses AI. Check for mistakes.
build/run.sh Outdated
export JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${JAVA_OPTS_CLEAN}"
else
# No heap settings exist, prepend them
export JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${JAVA_OPTS}"
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

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

[nitpick] When no heap settings exist in JAVA_OPTS but JAVA_OPTS is not empty, line 129 prepends the memory settings with ${JAVA_OPTS} at the end. If JAVA_OPTS has a leading space, this could result in double spaces. While not critical, it could lead to inconsistent formatting.

Consider trimming JAVA_OPTS before prepending to ensure consistent spacing: export JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${JAVA_OPTS## }" to remove leading spaces.

Suggested change
export JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${JAVA_OPTS}"
export JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${JAVA_OPTS## }"

Copilot uses AI. Check for mistakes.
build/run.sh Outdated
Comment on lines 116 to 130
# If MIN_MEMORY or MAX_MEMORY are provided, rebuild JAVA_OPTS with those values
if [[ -n "${MIN_MEMORY}" ]] || [[ -n "${MAX_MEMORY}" ]]; then
# Set defaults if only one is provided
MIN_MEMORY=${MIN_MEMORY:-512m}
MAX_MEMORY=${MAX_MEMORY:-512m}

# Check if JAVA_OPTS already has heap settings - preserve other options if present
if [[ $JAVA_OPTS =~ -Xms && $JAVA_OPTS =~ -Xmx ]]; then
# Remove existing heap settings and rebuild
JAVA_OPTS_CLEAN=$(echo "$JAVA_OPTS" | sed -E 's/-Xms[^ ]+//g; s/-Xmx[^ ]+//g; s/ +/ /g; s/^ //; s/ $//')
export JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${JAVA_OPTS_CLEAN}"
else
# No heap settings exist, prepend them
export JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${JAVA_OPTS}"
fi
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

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

The logic for handling JAVA_OPTS has a flaw. The condition on line 117 checks if MIN_MEMORY or MAX_MEMORY are provided, but then the nested condition on line 123 checks if JAVA_OPTS already has heap settings.

However, for the Web Dockerfiles (JRE21.Web.Dockerfile, Alpine.JRE21.Web.Dockerfile, JRE21.Web.Nginx.Dockerfile), JAVA_OPTS is still defined with -Xmx${MAX_MEMORY} -Xms${MIN_MEMORY}, meaning the condition on line 123 will always be true even when MIN_MEMORY and MAX_MEMORY are not overridden by the user.

This creates unnecessary processing and defeats the purpose of making memory settings configurable at runtime. The script will always enter the regex matching and sed replacement logic even when no user override was provided.

Consider simplifying this logic or ensuring JAVA_OPTS in the Dockerfiles doesn't pre-expand MIN_MEMORY/MAX_MEMORY variables.

Suggested change
# If MIN_MEMORY or MAX_MEMORY are provided, rebuild JAVA_OPTS with those values
if [[ -n "${MIN_MEMORY}" ]] || [[ -n "${MAX_MEMORY}" ]]; then
# Set defaults if only one is provided
MIN_MEMORY=${MIN_MEMORY:-512m}
MAX_MEMORY=${MAX_MEMORY:-512m}
# Check if JAVA_OPTS already has heap settings - preserve other options if present
if [[ $JAVA_OPTS =~ -Xms && $JAVA_OPTS =~ -Xmx ]]; then
# Remove existing heap settings and rebuild
JAVA_OPTS_CLEAN=$(echo "$JAVA_OPTS" | sed -E 's/-Xms[^ ]+//g; s/-Xmx[^ ]+//g; s/ +/ /g; s/^ //; s/ $//')
export JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${JAVA_OPTS_CLEAN}"
else
# No heap settings exist, prepend them
export JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${JAVA_OPTS}"
fi
# Only rebuild JAVA_OPTS if MIN_MEMORY or MAX_MEMORY are provided by the user
if [[ -n "${MIN_MEMORY}" ]] || [[ -n "${MAX_MEMORY}" ]]; then
# Set defaults if only one is provided
MIN_MEMORY=${MIN_MEMORY:-512m}
MAX_MEMORY=${MAX_MEMORY:-512m}
# Remove any existing heap settings and rebuild JAVA_OPTS
JAVA_OPTS_CLEAN=$(echo "$JAVA_OPTS" | sed -E 's/-Xms[^ ]+//g; s/-Xmx[^ ]+//g; s/ +/ /g; s/^ //; s/ $//')
export JAVA_OPTS="-Xms${MIN_MEMORY} -Xmx${MAX_MEMORY} ${JAVA_OPTS_CLEAN}"

Copilot uses AI. Check for mistakes.
build/run.sh Outdated
fi
fi

# If JAVA_OPTS is still not set (shouldn't happen with Dockerfile defaults), set minimal default
Copy link

Copilot AI Nov 21, 2025

Choose a reason for hiding this comment

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

The comment states "shouldn't happen with Dockerfile defaults", but this is incorrect. The base Dockerfiles (JRE21.Dockerfile and Alpine.JRE21.Dockerfile) no longer define JAVA_OPTS, MIN_MEMORY, or MAX_MEMORY environment variables after this PR's changes.

For containers built from these base images without the Web layer, JAVA_OPTS will be unset when the script reaches this point (assuming no user override), making this fallback logic necessary and expected rather than exceptional. The comment should be updated to reflect this reality.

Suggested change
# If JAVA_OPTS is still not set (shouldn't happen with Dockerfile defaults), set minimal default
# If JAVA_OPTS is still not set (not set by base Dockerfile defaults), set minimal default

Copilot uses AI. Check for mistakes.
Copy link
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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jclausen jclausen merged commit ab76ab4 into development Nov 24, 2025
9 of 13 checks passed
@jclausen jclausen deleted the add_JAVA_OPTS_support branch November 24, 2025 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments