Skip to content

Commit 8622477

Browse files
committed
Update core container so that settings can be set in init
Ref #236
1 parent 2bcb931 commit 8622477

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

core/testcontainers/core/container.py

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from docker.models.containers import Container
22
import os
3-
from typing import Iterable, Optional, Tuple
3+
from typing import Iterable, Optional, Tuple, Any, Dict
44

55
from .waiting_utils import wait_container_is_ready
66
from .docker_client import DockerClient
@@ -14,6 +14,28 @@ class DockerContainer:
1414
"""
1515
Basic container object to spin up Docker instances.
1616
17+
Parameters
18+
----------
19+
image:
20+
The name of the image to start.
21+
docker_client_kw:
22+
Dictionary with arguments that will be passed to the
23+
docker.DockerClient init.
24+
command:
25+
Optional execution command for the container.
26+
name:
27+
Optional name for the container.
28+
ports:
29+
Ports to be exposed by the container. The port number will be
30+
automatically assigned on the host, use :code:`get_exposed_port(PORT)`
31+
method to get the port number on the host.
32+
volumes:
33+
Volumes to mount into the container. Each entry should be a tuple with
34+
three values:
35+
#. host path
36+
#. container path
37+
#. mode (default 'ro').
38+
1739
.. doctest::
1840
1941
>>> from testcontainers.core.container import DockerContainer
@@ -22,15 +44,40 @@ class DockerContainer:
2244
>>> with DockerContainer("hello-world") as container:
2345
... delay = wait_for_logs(container, "Hello from Docker!")
2446
"""
25-
def __init__(self, image: str, docker_client_kw: Optional[dict] = None, **kwargs) -> None:
47+
def __init__(
48+
self,
49+
image: str,
50+
docker_client_kw: Optional[Dict[str, Any]] = None,
51+
command: Optional[str] = None
52+
env: Optional[Dict[str, str]] = None,
53+
name: Optional[str] = None,
54+
ports = Optional[List[int]] = None,
55+
volumes = Optional[List[Tuple[str, str, str]]] = None,
56+
**kwargs
57+
) -> None:
58+
59+
self.image = image
60+
self._docker = DockerClient(**(docker_client_kw or {}))
61+
62+
self._command = command
63+
2664
self.env = {}
65+
if env:
66+
for variable, value in env.items():
67+
self.with_env(key, value)
68+
69+
self._name = name
70+
2771
self.ports = {}
72+
if ports:
73+
self.with_exposed_ports(*ports)
74+
2875
self.volumes = {}
29-
self.image = image
30-
self._docker = DockerClient(**(docker_client_kw or {}))
76+
if volumes:
77+
for vol in volumes:
78+
self.with_volume_mapping(*vol)
79+
3180
self._container = None
32-
self._command = None
33-
self._name = None
3481
self._kwargs = kwargs
3582

3683
def with_env(self, key: str, value: str) -> 'DockerContainer':

0 commit comments

Comments
 (0)