@@ -14,6 +14,9 @@ class DockerCompose:
14
14
Args:
15
15
filepath: Relative directory containing the docker compose configuration file.
16
16
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.
17
20
pull: Pull images before launching environment.
18
21
build: Build images referenced in the configuration file.
19
22
env_file: Path to an env file containing environment variables to pass to docker compose.
@@ -45,6 +48,7 @@ def __init__(
45
48
self ,
46
49
filepath : str ,
47
50
compose_file_name : Union [str , Iterable ] = "docker-compose.yml" ,
51
+ compose_command : str = None ,
48
52
pull : bool = False ,
49
53
build : bool = False ,
50
54
env_file : Optional [str ] = None ,
@@ -57,6 +61,7 @@ def __init__(
57
61
self .build = build
58
62
self .env_file = env_file
59
63
self .services = services
64
+ self ._user_defined_compose_command = compose_command .split (" " ) if compose_command else None
60
65
61
66
def __enter__ (self ) -> "DockerCompose" :
62
67
self .start ()
@@ -65,14 +70,33 @@ def __enter__(self) -> "DockerCompose":
65
70
def __exit__ (self , exc_type , exc_val , exc_tb ) -> None :
66
71
self .stop ()
67
72
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
+
68
92
def docker_compose_command (self ) -> List [str ]:
69
93
"""
70
94
Returns command parts used for the docker compose commands
71
95
72
96
Returns:
73
97
cmd: Docker compose command parts.
74
98
"""
75
- docker_compose_cmd = [ 'docker-compose' ]
99
+ docker_compose_cmd = self . base_docker_compose [: ]
76
100
for file in self .compose_file_names :
77
101
docker_compose_cmd += ['-f' , file ]
78
102
if self .env_file :
@@ -92,7 +116,6 @@ def start(self) -> None:
92
116
up_cmd .append ('--build' )
93
117
if self .services :
94
118
up_cmd .extend (self .services )
95
-
96
119
self ._call_command (cmd = up_cmd )
97
120
98
121
def stop (self ) -> None :
@@ -168,7 +191,10 @@ def get_service_host(self, service_name: str, port: int) -> str:
168
191
169
192
def _get_service_info (self , service : str , port : int ) -> List [str ]:
170
193
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 ))
172
198
result = str (output ).rstrip ().split (":" )
173
199
if len (result ) != 2 or not all (result ):
174
200
raise NoSuchPortExposed (f"port { port } is not exposed for service { service } " )
0 commit comments