Komodo Core: container_name with a Compose variable breaks stack state detection
Komodo: container_name with a Compose variable breaks stack state detection
Affected: v2.3.3 (current release) — and unchanged on main at time of writing.
Related existing issues: #874 (same root cause, open since 2025-09-23) and #343
(same symptom, open since 2025-03-05, labelled seen ).
Suggested action: this is likely best added as a comment on #874 rather than a
new issue — #874 already describes the defect correctly but does not identify the code
path or the escaping fix, which is what follows.
Summary
When a Compose service sets container_name using Compose variable syntax
(${VAR}, ${VAR:-default}, ${VAR?error}), Komodo Core builds its
container-name matching regex from the raw, un-interpolated string. Regex
metacharacters in that string — notably { and } — make the pattern invalid, so
regex construction fails.
Two consequences:
- Stack state is reported incorrectly as
Downeven while the containers are running, because no container can match a regex that failed to compile. - Unbounded log volume. The failure is re-logged on every monitoring cycle
(default
monitoring_interval15s). In our estate this single warning accounts for ~226,000 log lines/hour — about 80% of total log volume and 98% of all ERROR-level lines across the whole fleet. It exhausted our log backend's ingest budget, which silently discarded ~26.8M log lines from unrelated hosts before we found it.
Root cause
bin/core/src/stack/mod.rs (v2.3.3):
pub fn compose_container_match_regex(
container_name: &str,
) -> anyhow::Result<Regex> {
let regex = format!("^{container_name}-?[0-9]*$");
Regex::new(®ex).with_context(|| {
format!("failed to construct valid regex from {regex}")
})
}container_name is interpolated directly into a regex pattern with no escaping. A
container name is a literal, not a pattern, so any regex metacharacter in it is
mis-parsed. With container_name: ${CONTAINER_NAME:-my-service} the pattern becomes:
^${CONTAINER_NAME:-my-service}-?[0-9]*${...} is a repetition quantifier in regex, and CONTAINER_NAME:-my-service is not a
valid decimal, hence:
error: repetition quantifier expects a valid decimalThe value reaching this function is the raw Compose text. bin/core/src/stack/services.rs:
container_name: container_name.unwrap_or_else(|| {
format!("{project_name}-{service_name}")
}),When container_name is absent Komodo derives <project>-<service>, which is safe.
When it is present it is used verbatim, including any ${...}.
The Down misreport follows from bin/core/src/helpers/query.rs:
Ok(regex) => regex,
Err(e) => {
warn!("{e:#}");
return false // <-- no container matches
}
...
if containers.is_empty() {
return StackState::Down;
}Reproduction
- Create a Compose stack containing:
services:
app:
image: nginx:alpine
container_name: ${APP_NAME:-my-app}- Set
APP_NAME=my-appin the stack environment (or leave it unset — either way). - Deploy. The container starts correctly and is healthy in
docker ps. - Komodo shows the stack as DOWN, and Core logs, every monitoring interval:
WARN core::helpers::query: failed to construct container name matching regex for
service app: failed to construct valid regex from ^${APP_NAME:-my-app}-?[0-9]*$:
regex parse error:
^${APP_NAME:-my-app}-?[0-9]*$
^
error: repetition quantifier expects a valid decimalSetting the variable does not help, because the matcher is built before interpolation.
Suggested fix
Two parts. The first alone resolves the crash and the log flood and is a one-liner:
1. Escape the literal (regex::escape is already available via the regex crate):
let regex = format!("^{}-?[0-9]*$", regex::escape(container_name));This makes any container name safe regardless of its characters, which is correct behaviour for matching a literal name, and stops the repeated failure entirely.
2. Interpolate Compose variables before deriving the matcher, so the pattern is
built from the container's actual name. Without this, escaping alone stops the noise
and the error but the stack will still read Down, since the un-interpolated literal
will never match a real container.
A reasonable middle option if full interpolation is awkward: when container_name
contains ${, fall back to the {project_name}-{service_name} derivation already used
for the absent case, and log once at debug rather than per cycle.
Why this is worth prioritising
The failure is silent in the UI (a stack reads Down, which looks like a legitimate
state rather than a detection failure), and the log amplification is severe enough to
cause collateral data loss in a log pipeline — in our case masking unrelated errors for
weeks. The workaround space for operators is also unusually narrow: a literal
container_name is not viable where one Compose file is deliberately shared by several
stacks that each need a distinct container name.
Source: moghtech/komodo