Skip to content

Commit 847a9d3

Browse files
committed
add support for docker compose v2
Signed-off-by: Paul S. Schweigert <[email protected]>
1 parent 94a63d6 commit 847a9d3

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

compose/testcontainers/compose/__init__.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class DockerCompose:
1414
Args:
1515
filepath: Relative directory containing the docker compose configuration file.
1616
compose_file_name: File name of the docker compose configuration file.
17+
compose_command: The command to use for docker compose. If not specified, a call to
18+
docker compose --help will be made to determine the correct command to use.
19+
If docker compose is not installed, docker-compose will be used.
1720
pull: Pull images before launching environment.
1821
build: Build images referenced in the configuration file.
1922
env_file: Path to an env file containing environment variables to pass to docker compose.
@@ -45,6 +48,7 @@ def __init__(
4548
self,
4649
filepath: str,
4750
compose_file_name: Union[str, Iterable] = "docker-compose.yml",
51+
compose_command: str = None,
4852
pull: bool = False,
4953
build: bool = False,
5054
env_file: Optional[str] = None,
@@ -57,6 +61,7 @@ def __init__(
5761
self.build = build
5862
self.env_file = env_file
5963
self.services = services
64+
self._user_defined_compose_command = compose_command.split(" ") if compose_command else None
6065

6166
def __enter__(self) -> "DockerCompose":
6267
self.start()
@@ -65,14 +70,33 @@ def __enter__(self) -> "DockerCompose":
6570
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
6671
self.stop()
6772

73+
def base_docker_compose(self):
74+
"""
75+
Returns the basecommand parts used for the docker compose commands
76+
depending on the docker compose api.
77+
78+
Returns
79+
-------
80+
list[str]
81+
The docker compose command parts
82+
"""
83+
if self._user_defined_compose_command:
84+
return self._user_defined_compose_command
85+
86+
if subprocess.run(["docker", "compose", "--help"], stdout=subprocess.DEVNULL,
87+
stderr=subprocess.STDOUT).returncode == 0:
88+
return ["docker", "compose"]
89+
90+
return ["docker-compose"]
91+
6892
def docker_compose_command(self) -> List[str]:
6993
"""
7094
Returns command parts used for the docker compose commands
7195
7296
Returns:
7397
cmd: Docker compose command parts.
7498
"""
75-
docker_compose_cmd = ['docker-compose']
99+
docker_compose_cmd = self.base_docker_compose[:]
76100
for file in self.compose_file_names:
77101
docker_compose_cmd += ['-f', file]
78102
if self.env_file:
@@ -92,7 +116,6 @@ def start(self) -> None:
92116
up_cmd.append('--build')
93117
if self.services:
94118
up_cmd.extend(self.services)
95-
96119
self._call_command(cmd=up_cmd)
97120

98121
def stop(self) -> None:
@@ -168,7 +191,10 @@ def get_service_host(self, service_name: str, port: int) -> str:
168191

169192
def _get_service_info(self, service: str, port: int) -> List[str]:
170193
port_cmd = self.docker_compose_command() + ["port", service, str(port)]
171-
output = subprocess.check_output(port_cmd, cwd=self.filepath).decode("utf-8")
194+
try:
195+
output = subprocess.check_output(port_cmd, cwd=self.filepath).decode("utf-8")
196+
except subprocess.CalledProcessError as e:
197+
raise NoSuchPortExposed(str(e.stderr))
172198
result = str(output).rstrip().split(":")
173199
if len(result) != 2 or not all(result):
174200
raise NoSuchPortExposed(f"port {port} is not exposed for service {service}")

compose/tests/test_docker_compose.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def test_can_build_images_before_spawning_service_via_compose():
3434

3535
assert compose.build
3636
docker_compose_cmd = call_mock.call_args_list[0][1]["cmd"]
37-
assert "docker-compose" in docker_compose_cmd
37+
assert "docker-compose" in docker_compose_cmd or ("docker" in docker_compose_cmd
38+
and "compose" in docker_compose_cmd)
3839
assert "up" in docker_compose_cmd
3940
assert "--build" in docker_compose_cmd
4041

0 commit comments

Comments
 (0)