Skip to content

Commit

Permalink
fix: handle docker network host
Browse files Browse the repository at this point in the history
Fixes #4236.

PR #669 allowed usage of the host network for Docker containers,
but that feature was removed in PR #5279.

This change reintroduces setting `network_mode` to `host` when `docker_network`
is `host`, and adds some additional debug logging to ease debugging.
  • Loading branch information
GuillaumeDesforges committed Jul 25, 2024
1 parent 5712c7f commit 9cf44b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
32 changes: 23 additions & 9 deletions samcli/local/docker/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def __init__(
self.rapid_port_host = find_free_port(
network_interface=self._container_host_interface, start=self._start_port_range, end=self._end_port_range
)

except NoFreePortsError as ex:
raise ContainerNotStartableException(str(ex)) from ex

Expand Down Expand Up @@ -216,15 +217,25 @@ def create(self):
if self._env_vars:
kwargs["environment"] = self._env_vars

kwargs["ports"] = {self.RAPID_PORT_CONTAINER: (self._container_host_interface, self.rapid_port_host)}

if self._exposed_ports:
kwargs["ports"].update(
{
container_port: (self._container_host_interface, host_port)
for container_port, host_port in self._exposed_ports.items()
}
)
if self.network_id == "host":
kwargs["network_mode"] = self.network_id
# When using host network, aws-lambda-rie will bind to 8080
self.rapid_port_host = int(self.RAPID_PORT_CONTAINER)
LOG.warning("Using host network mode will bind to port %s", self.RAPID_PORT_CONTAINER)

# It is not possible to both use host network and expose ports
if "network_mode" in kwargs and kwargs["network_mode"] == "host":
LOG.warning("Using host network mode, ignoring any port mappings")
else:
kwargs["ports"] = {self.RAPID_PORT_CONTAINER: (self._container_host_interface, self.rapid_port_host)}

if self._exposed_ports:
kwargs["ports"].update(
{
container_port: (self._container_host_interface, host_port)
for container_port, host_port in self._exposed_ports.items()
}
)

if self._entrypoint:
kwargs["entrypoint"] = self._entrypoint
Expand All @@ -233,9 +244,12 @@ def create(self):
# Ex: 128m => 128MB
kwargs["mem_limit"] = "{}m".format(self._memory_limit_mb)


if self._extra_hosts:
kwargs["extra_hosts"] = self._extra_hosts

LOG.debug("Docker will start with the following arguments: %s", kwargs)

try:
real_container = self.docker_client.containers.create(self._image, **kwargs)
except DockerAPIError as ex:
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/local/docker/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def test_must_connect_to_host_network_on_create(self, mock_resolve_symlinks):
self.image,
command=self.cmd,
working_dir=self.working_dir,
ports=self.always_exposed_ports,
network_mode='host',
tty=False,
use_config_proxy=True,
volumes=expected_volumes,
Expand Down

0 comments on commit 9cf44b4

Please sign in to comment.