Skip to content

Commit 210719e

Browse files
committed
Add support for tmpfs mounting
1 parent 700d1a8 commit 210719e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

core/testcontainers/core/container.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ def __init__(
8282
for vol in volumes:
8383
self.with_volume_mapping(*vol)
8484

85+
self.tmpfs: dict[str, str] = {}
86+
8587
self.image = image
8688
self._docker = DockerClient(**(docker_client_kw or {}))
8789
self._container: Optional[Container] = None
@@ -198,6 +200,7 @@ def start(self) -> Self:
198200
ports=cast("dict[int, Optional[int]]", self.ports),
199201
name=self._name,
200202
volumes=self.volumes,
203+
tmpfs=self.tmpfs,
201204
**{**network_kwargs, **self._kwargs},
202205
)
203206

@@ -270,6 +273,16 @@ def with_volume_mapping(self, host: Union[str, PathLike[str]], container: str, m
270273
self.volumes[str(host)] = mapping
271274
return self
272275

276+
def with_tmpfs_mount(self, container_path: str, size: Optional[str] = None) -> Self:
277+
"""Mount a tmpfs volume on the container.
278+
279+
:param container_path: Container path to mount tmpfs on (e.g., '/data')
280+
:param size: Optional size limit (e.g., '256m', '1g'). If None, unbounded.
281+
:return: Self for chaining
282+
"""
283+
self.tmpfs[container_path] = size or ""
284+
return self
285+
273286
def get_wrapped_container(self) -> "Container":
274287
return self._container
275288

modules/mqtt/testcontainers/mqtt/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def start(self, configfile: Optional[str] = None) -> Self:
122122
configfile = Path(__file__).parent / MosquittoContainer.CONFIG_FILE
123123
self.with_volume_mapping(configfile, "/mosquitto/config/mosquitto.conf")
124124
# since version 2.1.1 - 2026-02-04, which fixed a PUID/PGID issue, the container needs to write to the data directory,
125-
# so we need to map it to a volume
126-
self.with_volume_mapping("mosquitto_data", "/data", mode="rw")
125+
# so we mount it as tmpfs for better performance in tests
126+
self.with_tmpfs_mount("/data")
127127

128128
# if self.password:
129129
# # TODO: add authentication

0 commit comments

Comments
 (0)