1
1
"""
2
- Docker compose support
2
+ Docker Compose Support
3
3
======================
4
4
5
5
Allows to spin up services configured via :code:`docker-compose.yml`.
14
14
15
15
class DockerCompose (object ):
16
16
"""
17
- Docker compose containers.
17
+ Manage docker compose environments.
18
+
19
+ Parameters
20
+ ----------
21
+ filepath: str
22
+ The relative directory containing the docker compose configuration file
23
+ compose_file_name: str
24
+ The file name of the docker compose configuration file
25
+ pull: bool
26
+ Attempts to pull images before launching environment
27
+ build: bool
28
+ Whether to build images referenced in the configuration file
29
+ env_file: str
30
+ Path to an env file containing environment variables to pass to docker compose
18
31
19
32
Example
20
33
-------
@@ -54,7 +67,6 @@ class DockerCompose(object):
54
67
expose:
55
68
- "5555"
56
69
"""
57
-
58
70
def __init__ (
59
71
self ,
60
72
filepath ,
@@ -78,6 +90,14 @@ def __exit__(self, exc_type, exc_val, exc_tb):
78
90
self .stop ()
79
91
80
92
def docker_compose_command (self ):
93
+ """
94
+ Returns command parts used for the docker compose commands
95
+
96
+ Returns
97
+ -------
98
+ list[str]
99
+ The docker compose command parts
100
+ """
81
101
docker_compose_cmd = ['docker-compose' ]
82
102
for file in self .compose_file_names :
83
103
docker_compose_cmd += ['-f' , file ]
@@ -86,6 +106,9 @@ def docker_compose_command(self):
86
106
return docker_compose_cmd
87
107
88
108
def start (self ):
109
+ """
110
+ Starts the docker compose environment.
111
+ """
89
112
if self .pull :
90
113
pull_cmd = self .docker_compose_command () + ['pull' ]
91
114
self ._call_command (cmd = pull_cmd )
@@ -97,10 +120,21 @@ def start(self):
97
120
self ._call_command (cmd = up_cmd )
98
121
99
122
def stop (self ):
123
+ """
124
+ Stops the docker compose environment.
125
+ """
100
126
down_cmd = self .docker_compose_command () + ['down' , '-v' ]
101
127
self ._call_command (cmd = down_cmd )
102
128
103
129
def get_logs (self ):
130
+ """
131
+ Returns all log output from stdout and stderr
132
+
133
+ Returns
134
+ -------
135
+ tuple[bytes, bytes]
136
+ stdout, stderr
137
+ """
104
138
logs_cmd = self .docker_compose_command () + ["logs" ]
105
139
result = subprocess .run (
106
140
logs_cmd ,
@@ -136,9 +170,39 @@ def exec_in_container(self, service_name, command):
136
170
return result .stdout .decode ("utf-8" ), result .stderr .decode ("utf-8" ), result .returncode
137
171
138
172
def get_service_port (self , service_name , port ):
173
+ """
174
+ Returns the mapped port for one of the services.
175
+
176
+ Parameters
177
+ ----------
178
+ service_name: str
179
+ Name of the docker compose service
180
+ port: int
181
+ The internal port to get the mapping for
182
+
183
+ Returns
184
+ -------
185
+ str:
186
+ The mapped port on the host
187
+ """
139
188
return self ._get_service_info (service_name , port )[1 ]
140
189
141
190
def get_service_host (self , service_name , port ):
191
+ """
192
+ Returns the host for one of the services.
193
+
194
+ Parameters
195
+ ----------
196
+ service_name: str
197
+ Name of the docker compose service
198
+ port: int
199
+ The internal port to get the host for
200
+
201
+ Returns
202
+ -------
203
+ str:
204
+ The hostname for the service
205
+ """
142
206
return self ._get_service_info (service_name , port )[0 ]
143
207
144
208
def _get_service_info (self , service , port ):
@@ -157,5 +221,16 @@ def _call_command(self, cmd, filepath=None):
157
221
158
222
@wait_container_is_ready (requests .exceptions .ConnectionError )
159
223
def wait_for (self , url ):
224
+ """
225
+ Waits for a response from a given URL. This is typically used to
226
+ block until a service in the environment has started and is responding.
227
+ Note that it does not assert any sort of return code, only check that
228
+ the connection was successful.
229
+
230
+ Parameters
231
+ ----------
232
+ url: str
233
+ URL from one of the services in the environment to use to wait on
234
+ """
160
235
requests .get (url )
161
236
return self
0 commit comments