Skip to content

Commit 1113c83

Browse files
authored
Merge pull request #1242 from uosis/docker-compat
Add docker_compose_compat setting
2 parents fa22528 + 4177bae commit 1113c83

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

docs/Extensions.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ The options to the network modes are passed to the `--network` option of the `po
139139
as-is.
140140

141141

142+
## Docker Compose Compatibility
143+
144+
podman-compose aims to be compatible with docker-compose, but there are some differences in
145+
behavior and features. The following sections describe how to enable compatibility with docker-compose
146+
and how to handle some of the differences.
147+
148+
Compatibility settings can either be set explicitly as described below, or by setting the `docker_compose_compat` meta
149+
settings to `true` under the global `x-podman` key:
150+
151+
```yaml
152+
x-podman:
153+
docker_compose_compat: true
154+
```
155+
156+
This will enable all compatibility settings described below, and is equivalent to setting each of them to `true`.
157+
158+
This setting can also be changed by setting the `PODMAN_COMPOSE_DOCKER_COMPOSE_COMPAT` environment variable.
159+
142160
## Compatibility of name separators between docker-compose and podman-compose
143161

144162
Currently, podman-compose is using underscores (`_` character) as a separator in names of
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add new docker_compose_compat x-podman meta setting to enable all Docker Compose compatibility settings

podman_compose.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,7 @@ def dotenv_to_dict(dotenv_path: str) -> dict[str, str | None]:
19571957

19581958
class PodmanCompose:
19591959
class XPodmanSettingKey(Enum):
1960+
DOCKER_COMPOSE_COMPAT = "docker_compose_compat"
19601961
DEFAULT_NET_NAME_COMPAT = "default_net_name_compat"
19611962
DEFAULT_NET_BEHAVIOR_COMPAT = "default_net_behavior_compat"
19621963
NAME_SEPARATOR_COMPAT = "name_separator_compat"
@@ -2127,6 +2128,20 @@ def _parse_x_podman_settings(self, compose: dict[str, Any], environ: dict[str, s
21272128
", ".join(known_keys.keys()),
21282129
)
21292130

2131+
# If Docker Compose compatibility is enabled, set compatibility settings
2132+
# that are not explicitly set already.
2133+
if self.x_podman.get(PodmanCompose.XPodmanSettingKey.DOCKER_COMPOSE_COMPAT, False):
2134+
2135+
def set_if_not_already_set(key: PodmanCompose.XPodmanSettingKey, value: bool) -> None:
2136+
if key not in self.x_podman:
2137+
self.x_podman[key] = value
2138+
2139+
set_if_not_already_set(
2140+
PodmanCompose.XPodmanSettingKey.DEFAULT_NET_BEHAVIOR_COMPAT, True
2141+
)
2142+
set_if_not_already_set(PodmanCompose.XPodmanSettingKey.NAME_SEPARATOR_COMPAT, True)
2143+
set_if_not_already_set(PodmanCompose.XPodmanSettingKey.IN_POD, False)
2144+
21302145
def _parse_compose_file(self) -> None:
21312146
args = self.global_args
21322147
# cmd = args.command

tests/integration/in_pod/test_podman_compose_in_pod.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,57 @@ def test_x_podman_in_pod_not_exists_command_line_in_pod_false(self) -> None:
467467
# can not actually find this pod because it was not created
468468
self.run_subprocess_assert_returncode(command_rm_pod, 1)
469469

470+
def test_x_podman_in_pod_not_exists_command_line_in_pod_not_exists_docker_compat(self) -> None:
471+
"""
472+
Test that podman-compose will not create a pod when docker compat is requested.
473+
"""
474+
command_up = [
475+
"python3",
476+
os.path.join(base_path(), "podman_compose.py"),
477+
"-f",
478+
os.path.join(
479+
base_path(),
480+
"tests",
481+
"integration",
482+
"in_pod",
483+
"custom_x-podman_not_exists",
484+
"docker-compose.yml",
485+
),
486+
"up",
487+
"-d",
488+
]
489+
490+
down_cmd = [
491+
"python3",
492+
podman_compose_path(),
493+
"-f",
494+
os.path.join(
495+
base_path(),
496+
"tests",
497+
"integration",
498+
"in_pod",
499+
"custom_x-podman_not_exists",
500+
"docker-compose.yml",
501+
),
502+
"down",
503+
]
504+
505+
env = {
506+
"PODMAN_COMPOSE_DOCKER_COMPOSE_COMPAT": "1",
507+
}
508+
509+
try:
510+
self.run_subprocess_assert_returncode(
511+
command_up, failure_exitcode_when_rootful(), env=env
512+
)
513+
514+
finally:
515+
self.run_subprocess_assert_returncode(down_cmd, env=env)
516+
517+
command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_not_exists"]
518+
# can not actually find this pod because it was not created
519+
self.run_subprocess_assert_returncode(command_rm_pod, 1)
520+
470521
def test_x_podman_in_pod_not_exists_command_line_in_pod_not_exists_env_var(self) -> None:
471522
"""
472523
Test that podman-compose will not create a pod when env var is set.

tests/integration/name_separator_compat/test_podman_compose_name_separator_compat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class TestComposeNameSeparatorCompat(unittest.TestCase, RunSubprocessMixin):
1414
@parameterized.expand([
1515
('default', {}, '_'),
1616
('default', {'PODMAN_COMPOSE_NAME_SEPARATOR_COMPAT': '1'}, '-'),
17+
('default', {'PODMAN_COMPOSE_DOCKER_COMPOSE_COMPAT': '1'}, '-'),
1718
('compat', {}, '-'),
1819
('compat', {'PODMAN_COMPOSE_NAME_SEPARATOR_COMPAT': '1'}, '-'),
1920
('compat', {'PODMAN_COMPOSE_NAME_SEPARATOR_COMPAT': '0'}, '_'),

0 commit comments

Comments
 (0)