Skip to content

Commit dc956cc

Browse files
weixu365SergeyPirogov
authored andcommitted
Allow passing in any parameters supported by docker (#34)
1 parent d9c8a22 commit dc956cc

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

testcontainers/core/container.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class DockerContainer(object):
11-
def __init__(self, image):
11+
def __init__(self, image, **kargs):
1212
self.env = {}
1313
self.ports = {}
1414
self.volumes = {}
@@ -17,6 +17,7 @@ def __init__(self, image):
1717
self._container = None
1818
self._command = None
1919
self._name = None
20+
self._kargs = kargs
2021

2122
def with_env(self, key: str, value: str) -> 'DockerContainer':
2223
self.env[key] = value
@@ -32,6 +33,10 @@ def with_exposed_ports(self, *ports) -> 'DockerContainer':
3233
self.ports[port] = None
3334
return self
3435

36+
def with_kargs(self, **kargs) -> 'DockerContainer':
37+
self._kargs = kargs
38+
return self
39+
3540
def start(self):
3641
print("")
3742
print("{} {}".format(crayons.yellow("Pulling image"),
@@ -44,7 +49,9 @@ def start(self):
4449
environment=self.env,
4550
ports=self.ports,
4651
name=self._name,
47-
volumes=self.volumes)
52+
volumes=self.volumes,
53+
**self._kargs
54+
)
4855
print("")
4956
print("Container started: ",
5057
crayons.yellow(self._container.short_id, bold=True))

tests/test_new_docker_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from pathlib import Path
23

34
from testcontainers import mysql
45

@@ -30,3 +31,17 @@ def test_docker_env_variables():
3031
with db:
3132
url = db.get_connection_url()
3233
assert url == 'mysql+pymysql://demo:[email protected]:32785/custom_db'
34+
35+
def test_docker_kargs():
36+
code_dir = Path(__file__).parent
37+
container_first = GenericContainer("nginx:latest")
38+
container_first.with_volume_mapping(code_dir, '/code')
39+
40+
container_second = GenericContainer("nginx:latest")
41+
42+
with container_first:
43+
container_second.with_kargs(volumes_from=[container_first._container.short_id])
44+
with container_second:
45+
files_first = container_first.exec('ls /code').output.decode('utf-8').strip()
46+
files_second = container_second.exec('ls /code').output.decode('utf-8').strip()
47+
assert files_first == files_second

0 commit comments

Comments
 (0)