Skip to content

Commit 1e71e03

Browse files
author
Davide Melfi
committed
chore: add new logic for network address
1 parent 5e19a7a commit 1e71e03

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

action.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ inputs:
2020
required: false
2121
dockerSharedNetwork:
2222
description: 'Docker network to attach lambda containers to'
23-
required: false
24-
23+
required: false
2524
runs:
2625
using: 'docker'
2726
image: 'Dockerfile'

src/containerized_test_runner/docker.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def _capture(self,
259259
if self.entrypoint is not None:
260260
extra_docker_args += ["--entrypoint", self.entrypoint]
261261

262-
cmd = ["docker", "run", "-d", "-i", "--rm", "-p", "127.0.0.1:0:8080"]
262+
cmd = ["docker", "run", "-d", "-i", "--rm", "-p", "0.0.0.0:0:8080"]
263263

264264
if self.task_root != None:
265265
cmd += ["-v", "{}:/var/task".format(self.task_root)]
@@ -351,25 +351,26 @@ def _render_response(self, resp):
351351
return resp
352352

353353
def _get_local_addr(self, container_id):
354-
# If on a shared network, use the container's IP directly (no port mapping needed)
354+
import re
355+
# If on a shared network, use the container's IP on that network directly
355356
shared_network = os.environ.get("DOCKER_SHARED_NETWORK")
356357
if shared_network:
357358
docker_inspect_cmd = ["docker", "inspect", "-f",
358359
f'{{{{index .NetworkSettings.Networks "{shared_network}" "IPAddress"}}}}', container_id]
359360
docker_inspect_proc = subprocess.Popen(docker_inspect_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
360361
ip_address = docker_inspect_proc.communicate()[0].decode().rstrip()
361-
import re
362362
if ip_address and re.match(r'^\d+\.\d+\.\d+\.\d+$', ip_address):
363363
self.logger.debug(f"shared network: using container IP {ip_address}")
364364
return f"{ip_address}:8080"
365365

366-
# Fallback: use mapped host port
367-
docker_port_cmd = ["docker", "port", container_id, "8080"]
368-
docker_port_proc = subprocess.Popen(docker_port_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
369-
port_output = docker_port_proc.communicate()[0].decode().rstrip()
370-
self.logger.debug(f"docker port output: '{port_output}'")
371-
if port_output and port_output != "":
372-
return port_output
366+
# Fallback: use container's bridge network IP directly
367+
docker_inspect_cmd = ["docker", "inspect", "-f",
368+
'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}', container_id]
369+
docker_inspect_proc = subprocess.Popen(docker_inspect_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
370+
ip_address = docker_inspect_proc.communicate()[0].decode().rstrip()
371+
self.logger.debug(f"bridge network IP: '{ip_address}'")
372+
if ip_address and re.match(r'^\d+\.\d+\.\d+\.\d+$', ip_address):
373+
return f"{ip_address}:8080"
373374

374375
self.logger.warning("Could not determine container address, using localhost:8080")
375376
return "127.0.0.1:8080"

0 commit comments

Comments
 (0)