Skip to content

Commit 4cfe4c3

Browse files
authored
Update compose docs (#198)
1 parent 083487e commit 4cfe4c3

File tree

2 files changed

+79
-3
lines changed

2 files changed

+79
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ docs/_build/
6262
.noseids
6363
.idea/
6464
.venv/
65+
venv
6566
.testrepository/
6667

6768
# vscode:

testcontainers/compose.py

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Docker compose support
2+
Docker Compose Support
33
======================
44
55
Allows to spin up services configured via :code:`docker-compose.yml`.
@@ -14,7 +14,20 @@
1414

1515
class DockerCompose(object):
1616
"""
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
1831
1932
Example
2033
-------
@@ -54,7 +67,6 @@ class DockerCompose(object):
5467
expose:
5568
- "5555"
5669
"""
57-
5870
def __init__(
5971
self,
6072
filepath,
@@ -78,6 +90,14 @@ def __exit__(self, exc_type, exc_val, exc_tb):
7890
self.stop()
7991

8092
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+
"""
81101
docker_compose_cmd = ['docker-compose']
82102
for file in self.compose_file_names:
83103
docker_compose_cmd += ['-f', file]
@@ -86,6 +106,9 @@ def docker_compose_command(self):
86106
return docker_compose_cmd
87107

88108
def start(self):
109+
"""
110+
Starts the docker compose environment.
111+
"""
89112
if self.pull:
90113
pull_cmd = self.docker_compose_command() + ['pull']
91114
self._call_command(cmd=pull_cmd)
@@ -97,10 +120,21 @@ def start(self):
97120
self._call_command(cmd=up_cmd)
98121

99122
def stop(self):
123+
"""
124+
Stops the docker compose environment.
125+
"""
100126
down_cmd = self.docker_compose_command() + ['down', '-v']
101127
self._call_command(cmd=down_cmd)
102128

103129
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+
"""
104138
logs_cmd = self.docker_compose_command() + ["logs"]
105139
result = subprocess.run(
106140
logs_cmd,
@@ -136,9 +170,39 @@ def exec_in_container(self, service_name, command):
136170
return result.stdout.decode("utf-8"), result.stderr.decode("utf-8"), result.returncode
137171

138172
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+
"""
139188
return self._get_service_info(service_name, port)[1]
140189

141190
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+
"""
142206
return self._get_service_info(service_name, port)[0]
143207

144208
def _get_service_info(self, service, port):
@@ -157,5 +221,16 @@ def _call_command(self, cmd, filepath=None):
157221

158222
@wait_container_is_ready(requests.exceptions.ConnectionError)
159223
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+
"""
160235
requests.get(url)
161236
return self

0 commit comments

Comments
 (0)