Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 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 @@ -236,6 +247,8 @@ def create(self):
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