Skip to content

Fix CMD healthchecks running with /bin/sh #1214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/fix-cmd-healtchecks.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed support for CMD healthchecks to run using the given command directly and not using `/bin/sh -c`.
14 changes: 3 additions & 11 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@
from asyncio import Task
from enum import Enum

try:
from shlex import quote as cmd_quote
except ImportError:
from pipes import quote as cmd_quote # pylint: disable=deprecated-module

# import fnmatch
# fnmatch.fnmatchcase(env, "*_HOST")

import yaml
from dotenv import dotenv_values

Expand Down Expand Up @@ -1228,7 +1222,7 @@ async def container_to_args(compose, cnt, detached=True, no_deps=False):
# podman does not add shell to handle command with whitespace
podman_args.extend([
"--healthcheck-command",
"/bin/sh -c " + cmd_quote(healthcheck_test),
json.dumps(["CMD-SHELL", healthcheck_test]),
])
elif is_list(healthcheck_test):
healthcheck_test = healthcheck_test.copy()
Expand All @@ -1237,13 +1231,11 @@ async def container_to_args(compose, cnt, detached=True, no_deps=False):
if healthcheck_type == "NONE":
podman_args.append("--no-healthcheck")
elif healthcheck_type == "CMD":
cmd_q = "' '".join([cmd_quote(i) for i in healthcheck_test])
podman_args.extend(["--healthcheck-command", "/bin/sh -c " + cmd_q])
podman_args.extend(["--healthcheck-command", json.dumps(healthcheck_test)])
elif healthcheck_type == "CMD-SHELL":
if len(healthcheck_test) != 1:
raise ValueError("'CMD_SHELL' takes a single string after it")
cmd_q = cmd_quote(healthcheck_test[0])
podman_args.extend(["--healthcheck-command", "/bin/sh -c " + cmd_q])
podman_args.extend(["--healthcheck-command", json.dumps(healthcheck_test)])
else:
raise ValueError(
f"unknown healthcheck test type [{healthcheck_type}],\
Expand Down
70 changes: 70 additions & 0 deletions tests/unit/test_container_to_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,3 +694,73 @@ async def test_pids_limit_both_different(self):

with self.assertRaises(ValueError):
await container_to_args(c, cnt)

async def test_heathcheck_string(self):
c = create_compose_mock()
cnt = get_minimal_container()
cnt["healthcheck"] = {
"test": "cmd arg1 arg2",
}

args = await container_to_args(c, cnt)
self.assertEqual(
args,
[
"--name=project_name_service_name1",
"-d",
"--network=bridge:alias=service_name",
"--healthcheck-command",
'["CMD-SHELL", "cmd arg1 arg2"]',
"busybox",
],
)

async def test_heathcheck_cmd_args(self):
c = create_compose_mock()
cnt = get_minimal_container()
cnt["healthcheck"] = {
"test": ["CMD", "cmd", "arg1", "arg2"],
}

args = await container_to_args(c, cnt)
self.assertEqual(
args,
[
"--name=project_name_service_name1",
"-d",
"--network=bridge:alias=service_name",
"--healthcheck-command",
'["cmd", "arg1", "arg2"]',
"busybox",
],
)

async def test_heathcheck_cmd_shell(self):
c = create_compose_mock()
cnt = get_minimal_container()
cnt["healthcheck"] = {
"test": ["CMD-SHELL", "cmd arg1 arg2"],
}

args = await container_to_args(c, cnt)
self.assertEqual(
args,
[
"--name=project_name_service_name1",
"-d",
"--network=bridge:alias=service_name",
"--healthcheck-command",
'["cmd arg1 arg2"]',
"busybox",
],
)

async def test_heathcheck_cmd_shell_error(self):
c = create_compose_mock()
cnt = get_minimal_container()
cnt["healthcheck"] = {
"test": ["CMD-SHELL", "cmd arg1", "arg2"],
}

with self.assertRaises(ValueError):
await container_to_args(c, cnt)