Skip to content

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

fix(rds): detect a containerised MiniStack by its marker files, not its hostname (#1887)

Jeff Hellercfb9fff510f8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 36f8bc81..84c2ad88 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,9 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
+### Fixed
+
+- **RDS — `MINISTACK_RDS_PUBLIC_ENDPOINT=1` works with a Compose `hostname:`** — the #1884 fix told a containerised MiniStack from a host-run one by its `HOSTNAME` self-lookup, which a Compose `hostname:` defeats, so instances again stayed `creating`. MiniStack now checks Docker's or Podman's marker file for that, and a containerised MiniStack detects its network as with the setting off (`DOCKER_NETWORK`, then the self-lookup).
### Added
- **IoT — registry events** — with a type enabled through `UpdateEventConfigurations`, the thing, thing type, thing type association, thing group, thing group hierarchy and thing group membership operations publish the AWS payload to `$aws/events/...`, where MQTT subscribers and topic rules receive it.
diff --git a/ministack/services/rds.py b/ministack/services/rds.py
index d4be75a9..b093e291 100644
--- a/ministack/services/rds.py
+++ b/ministack/services/rds.py
@@ -2550,18 +2550,27 @@ def _get_docker():
return _docker
+def _in_container():
+ """Docker's and Podman's markers, as lambda_svc._running_in_container checks first."""
+ return os.path.exists("/.dockerenv") or os.path.exists("/run/.containerenv")
+
+
def _get_ministack_network(docker_client):
"""Detect the Docker network MiniStack is running on (if containerised).
- Under MINISTACK_RDS_PUBLIC_ENDPOINT the database containers join the
- network only when MiniStack itself is containerised, so readiness and
- internal wiring can reach them while _reported_endpoint reports the
- published port.
+ Under MINISTACK_RDS_PUBLIC_ENDPOINT a host-run MiniStack keeps its
+ database containers off the network, since it cannot reach their
+ addresses; a containerised one detects its network as usual, so
+ readiness and internal wiring reach them while _reported_endpoint
+ reports the published port.
"""
global _ministack_network
if _ministack_network is not None:
return _ministack_network or None
- if DOCKER_NETWORK and not RDS_PUBLIC_ENDPOINT:
+ if RDS_PUBLIC_ENDPOINT and not _in_container():
+ _ministack_network = ""
+ return None
+ if DOCKER_NETWORK:
_ministack_network = DOCKER_NETWORK
logger.debug("RDS: using DOCKER_NETWORK=%s", DOCKER_NETWORK)
return DOCKER_NETWORK
@@ -2571,8 +2580,7 @@ def _get_ministack_network(docker_client):
nets = list(
self_container.attrs["NetworkSettings"]["Networks"].keys())
if nets:
- _ministack_network = (DOCKER_NETWORK if DOCKER_NETWORK in nets
- else nets[0])
+ _ministack_network = nets[0]
logger.debug("RDS: detected MiniStack network: %s",
_ministack_network)
return _ministack_network
diff --git a/tests/test_rds.py b/tests/test_rds.py
index 0f7e197d..3f6bf830 100644
--- a/tests/test_rds.py
+++ b/tests/test_rds.py
@@ -16161,31 +16161,37 @@ def test_rds_server_certificate_without_cryptography_raises(monkeypatch):
rds_service._pg_server_material(["localhost"], [])
-def _fake_docker_self(networks):
- """A Docker client whose HOSTNAME lookup finds MiniStack's own container."""
+class _FailedSelfLookup:
+ """MiniStack's container under a Compose `hostname:`: HOSTNAME names no container."""
- class FakeContainers:
- def get(self, _identifier):
- if networks is None:
- raise Exception("not a container")
- return types.SimpleNamespace(
- attrs={"NetworkSettings": {"Networks": {n: {} for n in networks}}})
+ class containers:
+ @staticmethod
+ def get(_identifier):
+ raise Exception("no such container")
- class FakeDocker:
- containers = FakeContainers()
- return FakeDocker()
+class _FoundSelfLookup:
+ """MiniStack's own container, found by HOSTNAME, on the network `ms_net`."""
+ class containers:
+ @staticmethod
+ def get(_identifier):
+ return types.SimpleNamespace(attrs={"NetworkSettings": {"Networks": {"ms_net": {}}}})
-def test_rds_public_endpoint_containerised_ministack_joins_its_network(monkeypatch):
+
[email protected]("docker_network, client, expected", [
+ ("compose_default", _FailedSelfLookup(), "compose_default"),
+ ("", _FoundSelfLookup(), "ms_net"),
+])
+def test_rds_public_endpoint_containerised_ministack_joins_its_network(monkeypatch, docker_network, client, expected):
"""#1884: the database container must stay reachable from a containerised MiniStack."""
from ministack.services import rds as m
monkeypatch.setattr(m, "RDS_PUBLIC_ENDPOINT", True)
- monkeypatch.setattr(m, "DOCKER_NETWORK", "compose_default")
+ monkeypatch.setattr(m, "DOCKER_NETWORK", docker_network)
monkeypatch.setattr(m, "_ministack_network", None)
- client = _fake_docker_self(["bridge", "compose_default"])
- assert m._get_ministack_network(client) == "compose_default"
+ monkeypatch.setattr(m, "_in_container", lambda: True)
+ assert m._get_ministack_network(client) == expected
def test_rds_public_endpoint_host_run_ministack_stays_off_network(monkeypatch):
@@ -16195,7 +16201,8 @@ def test_rds_public_endpoint_host_run_ministack_stays_off_network(monkeypatch):
monkeypatch.setattr(m, "RDS_PUBLIC_ENDPOINT", True)
monkeypatch.setattr(m, "DOCKER_NETWORK", "compose_default")
monkeypatch.setattr(m, "_ministack_network", None)
- assert m._get_ministack_network(_fake_docker_self(None)) is None
+ monkeypatch.setattr(m, "_in_container", lambda: False)
+ assert m._get_ministack_network(_FailedSelfLookup()) is None
def test_rds_public_endpoint_reports_published_port_probes_container(monkeypatch):