@@ -320,7 +320,7 @@ def get_network_mode(self) -> Optional[str]:
320
320
# Default to unset
321
321
return None
322
322
323
- def get_extra_hosts (self , docker_client ) -> Optional [dict [str , str ]]:
323
+ def get_extra_hosts (self , docker_client : DockerClient ) -> Optional [dict [str , str ]]:
324
324
"""
325
325
A host.docker.internal -> host-gateway mapping is necessary for communicating
326
326
with the API on Linux machines. Docker Desktop on macOS will automatically
@@ -665,9 +665,29 @@ def _build_container_settings(
665
665
container_create_kwargs = {
666
666
k : v
667
667
for k , v in container_create_kwargs .items ()
668
- if k not in configuration .model_fields .keys ()
668
+ if k not in configuration .__class__ . model_fields .keys ()
669
669
}
670
670
671
+ # Get extra_hosts from configuration
672
+ extra_hosts = configuration .get_extra_hosts (docker_client )
673
+
674
+ # If user provided extra_hosts in container_create_kwargs, merge them
675
+ if "extra_hosts" in container_create_kwargs :
676
+ user_extra_hosts = container_create_kwargs .pop ("extra_hosts" )
677
+ if extra_hosts :
678
+ # Merge user's extra_hosts with the auto-generated ones
679
+ # Convert list format to dict if necessary
680
+ if isinstance (user_extra_hosts , list ):
681
+ for host_entry in user_extra_hosts :
682
+ if ":" in host_entry :
683
+ host , ip = host_entry .split (":" , 1 )
684
+ extra_hosts [host ] = ip
685
+ elif isinstance (user_extra_hosts , dict ):
686
+ extra_hosts .update (user_extra_hosts )
687
+ else :
688
+ # No auto-generated extra_hosts, use user's directly
689
+ extra_hosts = user_extra_hosts
690
+
671
691
return dict (
672
692
image = configuration .image ,
673
693
network = configuration .networks [0 ] if configuration .networks else None ,
@@ -676,7 +696,7 @@ def _build_container_settings(
676
696
environment = configuration .env ,
677
697
auto_remove = configuration .auto_remove ,
678
698
labels = configuration .labels ,
679
- extra_hosts = configuration . get_extra_hosts ( docker_client ) ,
699
+ extra_hosts = extra_hosts ,
680
700
name = configuration .name ,
681
701
volumes = configuration .volumes ,
682
702
mem_limit = configuration .mem_limit ,
0 commit comments