Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(lambda): map host.docker.internal to host-gateway in containers (#1375)

iot-rocket99ecd6a7801d
diff --git a/README.md b/README.md
index d813b21f..d7b832cb 100644
--- a/README.md
+++ b/README.md
@@ -941,6 +941,8 @@ Containers are named `lambda-<random-hex-16>` and pooled across invocations. Idl
**Networking:** when MiniStack runs in Docker Compose, set `DOCKER_NETWORK` to the Compose network name. All container-backed services (Lambda, RDS, DSQL, EKS, ElastiCache) then attach to that network so Lambda code can reach MiniStack at `http://<ministack-service-name>:4566`. The legacy `LAMBDA_DOCKER_NETWORK` is still accepted (Lambda only) as a fallback.
+Without a shared network, `AWS_ENDPOINT_URL` inside the Lambda container is rewritten from `localhost` to `host.docker.internal`, which only resolves out of the box on Docker Desktop. Lambda containers are therefore started with `host.docker.internal` mapped to `host-gateway` (as ECS and EKS containers already are), so a handler that calls back into MiniStack during its own invocation works on native Linux engines too. Passing your own `--add-host host.docker.internal:...` through `LAMBDA_DOCKER_FLAGS` overrides the mapping.
+
Example `docker-compose.yml`:
```yaml
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 6566ec75..f485403c 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -3510,6 +3510,19 @@ def _spawn_lambda_container(config: dict, code_zip: bytes | None):
run_kwargs.setdefault("mounts", []).extend(df_mounts)
run_kwargs.update(df_kwargs)
+ # `host.docker.internal` only resolves out of the box on Docker Desktop
+ # (macOS/Windows). On a native Linux engine the name is undefined inside
+ # containers unless it is mapped to the special `host-gateway` address, so
+ # every nested SDK call a handler makes against AWS_ENDPOINT_URL dies on
+ # DNS before reaching this server. Map it whenever the container is pointed
+ # at that name, as the ECS and EKS container paths already do. After the
+ # flags merge, and setdefault twice over, so an explicit
+ # `--add-host host.docker.internal:...` in LAMBDA_DOCKER_FLAGS still wins.
+ if "host.docker.internal" in endpoint:
+ run_kwargs.setdefault("extra_hosts", {}).setdefault(
+ "host.docker.internal", "host-gateway"
+ )
+
# Pull the image on first use (both Zip RIE images and user Image types)
try:
client.images.get(image)
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 348c22e4..61f9ff75 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -6648,6 +6648,87 @@ def test_lambda_invoke_emits_cloudwatch_logs_nodejs(lam, logs):
lam.delete_function(FunctionName=fname)
+# ──────────────────── host.docker.internal → host-gateway ────────────────────
+
+
+def _spawn_capture_run_kwargs(monkeypatch, *, endpoint, docker_flags=""):
+ """Spawn one Lambda container against fakes and return the docker kwargs.
+
+ Captures both entry points: the DinD path uses ``containers.create`` (code
+ is docker-cp'd rather than bind-mounted) and everything else uses
+ ``containers.run``, and which one a runner takes is not this test's point.
+ """
+ monkeypatch.setattr(lsvc, "LAMBDA_DOCKER_FLAGS", docker_flags)
+ monkeypatch.setattr(lsvc, "_docker_available", True)
+ monkeypatch.setenv("AWS_ENDPOINT_URL", endpoint)
+
+ captured = {}
+ fake_container = _mk_container()
+ fake_container.ports = {"8080/tcp": [{"HostPort": "9999"}]}
+
+ def _capture(**kwargs):
+ captured.update(kwargs)
+ return fake_container
+
+ fake_client = MagicMock()
+ fake_client.containers.run = _capture
+ fake_client.containers.create = _capture
+ fake_client.images.get = MagicMock()
+ monkeypatch.setattr(lsvc, "_get_docker_client", lambda: fake_client)
+
+ lsvc._spawn_lambda_container(
+ {"FunctionName": "test-hg-fn", "Runtime": "python3.12",
+ "Handler": "index.handler", "PackageType": "Zip", "Timeout": 3,
+ "MemorySize": 128,
+ "FunctionArn": "arn:aws:lambda:us-east-1:000000000000:function:test-hg-fn"},
+ _make_zip("def handler(e, c): pass"),
+ )
+ return captured
+
+
+def test_lambda_container_maps_host_docker_internal_to_host_gateway(monkeypatch):
+ """A container pointed at host.docker.internal gets the name mapped.
+
+ AWS_ENDPOINT_URL is rewritten from localhost to host.docker.internal for
+ the container, but that name only resolves out of the box on Docker
+ Desktop. On a native Linux engine it is undefined unless mapped to the
+ magic `host-gateway` address, so a handler's callback dies on DNS.
+ """
+ captured = _spawn_capture_run_kwargs(
+ monkeypatch, endpoint="http://localhost:4566"
+ )
+
+ assert captured["environment"]["AWS_ENDPOINT_URL"] == (
+ "http://host.docker.internal:4566"
+ )
+ assert captured["extra_hosts"] == {"host.docker.internal": "host-gateway"}
+
+
+def test_lambda_container_host_gateway_yields_to_an_explicit_add_host(monkeypatch):
+ """An explicit --add-host for the same name still wins."""
+ captured = _spawn_capture_run_kwargs(
+ monkeypatch,
+ endpoint="http://localhost:4566",
+ docker_flags="--add-host host.docker.internal:10.5.0.1",
+ )
+
+ assert captured["extra_hosts"] == {"host.docker.internal": "10.5.0.1"}
+
+
+def test_lambda_container_without_host_docker_internal_gets_no_mapping(monkeypatch):
+ """An endpoint that names something else is left alone.
+
+ A Docker-network deployment points AWS_ENDPOINT_URL at a container name,
+ which resolves on the network's own DNS; mapping host.docker.internal
+ there would be noise at best.
+ """
+ captured = _spawn_capture_run_kwargs(
+ monkeypatch, endpoint="http://ministack:4566"
+ )
+
+ assert "extra_hosts" not in captured
+
+
# ──────────────────── LAMBDA_DOCKER_FLAGS ────────────────────
def test_lambda_docker_flags_applied_to_run_kwargs(monkeypatch):
@@ -6670,6 +6751,9 @@ def test_lambda_docker_flags_applied_to_run_kwargs(monkeypatch):
fake_client = MagicMock()
fake_client.containers.run = _fake_run
+ # A runner that is itself containerised takes the docker-cp path, which
+ # calls containers.create; the flags land in the same kwargs either way.
+ fake_client.containers.create = _fake_run
fake_client.images.get = MagicMock()
monkeypatch.setattr(lsvc, "_get_docker_client", lambda: fake_client)
@@ -6709,7 +6793,10 @@ def test_lambda_docker_flags_applied_to_run_kwargs(monkeypatch):
assert captured["mem_limit"] == "512m"
assert captured["shm_size"] == "256m"
assert captured["cap_add"] == ["SYS_PTRACE"]
- assert captured["extra_hosts"] == {"myhost": "10.0.0.1"}
+ # The host-gateway mapping is merged in alongside an explicit --add-host.
+ assert captured["extra_hosts"] == {
+ "myhost": "10.0.0.1", "host.docker.internal": "host-gateway",
+ }
assert captured["tmpfs"] == {"/run": "size=100m"}
assert captured["privileged"] is True
assert captured["read_only"] is True