1212from localstack .utils .docker_utils import DOCKER_CLIENT
1313from localstack .extensions .api import Extension , http
1414from localstack .http import Request
15- from localstack .utils .container_utils .container_client import PortMappings
15+ from localstack .utils .container_utils .container_client import (
16+ PortMappings ,
17+ SimpleVolumeBind ,
18+ )
1619from localstack .utils .net import get_addressable_container_host
1720from localstack .utils .sync import retry
1821from rolo import route
@@ -52,11 +55,17 @@ class ProxiedDockerContainerExtension(Extension):
5255 """Optional command (and flags) to execute in the container."""
5356 env_vars : dict [str , str ] | None
5457 """Optional environment variables to pass to the container."""
58+ volumes : list [SimpleVolumeBind ] | None
59+ """Optional volumes to mount into the container."""
5560 health_check_fn : Callable [[], None ] | None
5661 """
5762 Optional custom health check function. If not provided, defaults to HTTP GET on main_port.
5863 The function should raise an exception if the health check fails.
5964 """
65+ health_check_retries : int
66+ """Number of times to retry the health check before giving up."""
67+ health_check_sleep : float
68+ """Time in seconds to sleep between health check retries."""
6069
6170 request_to_port_router : Callable [[Request ], int ] | None
6271 """Callable that returns the target port for a given request, for routing purposes"""
@@ -87,7 +96,10 @@ def __init__(
8796 path : str | None = None ,
8897 command : list [str ] | None = None ,
8998 env_vars : dict [str , str ] | None = None ,
99+ volumes : list [SimpleVolumeBind ] | None = None ,
90100 health_check_fn : Callable [[], None ] | None = None ,
101+ health_check_retries : int = 60 ,
102+ health_check_sleep : float = 1.0 ,
91103 request_to_port_router : Callable [[Request ], int ] | None = None ,
92104 http2_ports : list [int ] | None = None ,
93105 tcp_ports : list [int ] | None = None ,
@@ -101,7 +113,10 @@ def __init__(
101113 self .container_name = re .sub (r"\W" , "-" , f"ls-ext-{ self .name } " )
102114 self .command = command
103115 self .env_vars = env_vars
116+ self .volumes = volumes
104117 self .health_check_fn = health_check_fn
118+ self .health_check_retries = health_check_retries
119+ self .health_check_sleep = health_check_sleep
105120 self .request_to_port_router = request_to_port_router
106121 self .http2_ports = http2_ports
107122 self .tcp_ports = tcp_ports
@@ -193,6 +208,8 @@ def start_container(self) -> None:
193208 kwargs ["command" ] = self .command
194209 if self .env_vars :
195210 kwargs ["env_vars" ] = self .env_vars
211+ if self .volumes :
212+ kwargs ["volumes" ] = self .volumes
196213
197214 try :
198215 DOCKER_CLIENT .run_container (
@@ -213,7 +230,11 @@ def start_container(self) -> None:
213230 health_check = self .health_check_fn or self ._default_health_check
214231
215232 try :
216- retry (health_check , retries = 60 , sleep = 1 )
233+ retry (
234+ health_check ,
235+ retries = self .health_check_retries ,
236+ sleep = self .health_check_sleep ,
237+ )
217238 except Exception as e :
218239 LOG .info ("Failed to connect to container %s: %s" , self .container_name , e )
219240 self ._remove_container ()
0 commit comments