Skip to content

Commit 34e7415

Browse files
committed
Docker: Simplify port-bound check
1 parent e1f3422 commit 34e7415

File tree

2 files changed

+6
-46
lines changed

2 files changed

+6
-46
lines changed

localstack-core/localstack/utils/docker_utils.py

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
from typing import List, Optional, Union
66

77
from localstack import config
8-
from localstack.constants import DEFAULT_VOLUME_DIR, DOCKER_IMAGE_NAME
8+
from localstack.constants import DEFAULT_VOLUME_DIR
99
from localstack.utils.collections import ensure_list
1010
from localstack.utils.container_utils.container_client import (
1111
ContainerClient,
1212
PortMappings,
1313
VolumeInfo,
1414
)
1515
from localstack.utils.net import IntOrPort, Port, PortNotAvailableException, PortRange
16-
from localstack.utils.objects import singleton_factory
1716
from localstack.utils.strings import to_str
1817

1918
LOG = logging.getLogger(__name__)
@@ -147,23 +146,22 @@ def container_ports_can_be_bound(
147146
port_mappings.add(port.port, port.port, protocol=port.protocol)
148147
try:
149148
result = DOCKER_CLIENT.run_container(
150-
_get_ports_check_docker_image(),
151-
entrypoint="sh",
152-
command=["-c", "echo test123"],
149+
"bash",
150+
command=["echo", "test123"],
153151
ports=port_mappings,
154152
remove=True,
155153
)
156154
except Exception as e:
157-
if "port is already allocated" not in str(e) and "address already in use" not in str(e):
155+
if "address already in use" not in str(e):
158156
LOG.warning(
159-
"Unexpected error when attempting to determine container port status: %s", e
157+
"Unexpected error when attempting to determine container port status", exc_info=e
160158
)
161159
return False
162160
# TODO(srw): sometimes the command output from the docker container is "None", particularly when this function is
163161
# invoked multiple times consecutively. Work out why.
164162
if to_str(result[0] or "").strip() != "test123":
165163
LOG.warning(
166-
"Unexpected output when attempting to determine container port status: %s", result[0]
164+
"Unexpected output when attempting to determine container port status: %s", result
167165
)
168166
return True
169167

@@ -240,29 +238,4 @@ def _random_port():
240238
)
241239

242240

243-
@singleton_factory
244-
def _get_ports_check_docker_image() -> str:
245-
"""
246-
Determine the Docker image to use for Docker port availability checks.
247-
Uses either PORTS_CHECK_DOCKER_IMAGE (if configured), or otherwise inspects the running container's image.
248-
"""
249-
if config.PORTS_CHECK_DOCKER_IMAGE:
250-
# explicit configuration takes precedence
251-
return config.PORTS_CHECK_DOCKER_IMAGE
252-
if not config.is_in_docker:
253-
# local import to prevent circular imports
254-
from localstack.utils.bootstrap import get_docker_image_to_start
255-
256-
# Use whatever image the user is trying to run LocalStack with, since they either have
257-
# it already, or need it by definition to start LocalStack.
258-
return get_docker_image_to_start()
259-
try:
260-
# inspect the running container to determine the image
261-
container = DOCKER_CLIENT.inspect_container(get_current_container_id())
262-
return container["Config"]["Image"]
263-
except Exception:
264-
# fall back to using the default Docker image
265-
return DOCKER_IMAGE_NAME
266-
267-
268241
DOCKER_CLIENT: ContainerClient = create_docker_client()

tests/integration/docker_utils/test_docker.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from localstack import config
1515
from localstack.config import in_docker
1616
from localstack.testing.pytest import markers
17-
from localstack.utils import docker_utils
1817
from localstack.utils.common import is_ipv4_address, save_file, short_uid, to_str
1918
from localstack.utils.container_utils.container_client import (
2019
AccessDenied,
@@ -1894,18 +1893,6 @@ def test_container_with_sec_opt(self, docker_client: ContainerClient, create_con
18941893
assert security_opt == inspect_result["HostConfig"]["SecurityOpt"]
18951894

18961895

1897-
@pytest.fixture
1898-
def set_ports_check_image_alpine(monkeypatch):
1899-
"""Set the ports check Docker image to 'alpine', to avoid pulling the larger localstack image in the tests"""
1900-
1901-
def _get_ports_check_docker_image():
1902-
return "alpine"
1903-
1904-
monkeypatch.setattr(
1905-
docker_utils, "_get_ports_check_docker_image", _get_ports_check_docker_image
1906-
)
1907-
1908-
19091896
@pytest.mark.parametrize("protocol", [None, "tcp", "udp"])
19101897
class TestDockerPorts:
19111898
def test_reserve_container_port(self, docker_client, set_ports_check_image_alpine, protocol):

0 commit comments

Comments
 (0)