Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(rds): recreate container on start (#694)
Nahuel Nucera79e962ebc452
diff --git a/ministack/services/rds.py b/ministack/services/rds.pyindex 17c3ae30..98508838 100644--- a/ministack/services/rds.py+++ b/ministack/services/rds.py@@ -107,18 +107,130 @@ def restore_state(data):if "port_counter" in data:_port_counter[0] = data["port_counter"]instances_data = data.get("instances", {})+ to_respawn = []if isinstance(instances_data, AccountScopedDict):# New format: AccountScopedDict with full multi-account datafor key, inst in list(instances_data._data.items()):inst["_docker_container_id"] = None- inst["DBInstanceStatus"] = "available"+ inst["DBInstanceStatus"] = "creating"_instances._data[key] = inst+ to_respawn.append((inst.get("DBInstanceIdentifier") or key[1], inst))else:# Legacy format: plain dict keyed by instance namefor name, inst in instances_data.items():inst["_docker_container_id"] = None- inst["DBInstanceStatus"] = "available"+ inst["DBInstanceStatus"] = "creating"_instances[name] = inst+ to_respawn.append((name, inst))++ # Re-spin backing containers for persisted instances. Mirrors the MWAA+ # restore pattern: persistence saves the instance metadata but the Docker+ # container itself is killed by the host restart, so the restore path has+ # to bring it back. Without this, restored instances stay marked+ # "available" with no running container, and StartDBInstance is+ # metadata-only so it can't recover them either.+ for db_id, inst in to_respawn:+ threading.Thread(+ target=_start_rds_container_for_instance,+ args=(db_id, inst),+ daemon=True,+ ).start()+++def _start_rds_container_for_instance(db_id, instance):+ """Re-spin (or re-attach to) the Docker container for a restored instance.++ Reads engine, credentials, and endpoint info from the persisted instance+ dict instead of CreateDBInstance request params. If a container with the+ deterministic name ``ministack-rds-{db_id}`` already exists (e.g. host+ rebooted but Docker preserved stopped containers), it is removed first so+ a clean run can attach to the persistent named volume. Sets+ ``DBInstanceStatus`` to ``available`` on success, ``failed`` on Docker+ error.+ """+ docker_client = _get_docker()+ if not docker_client:+ instance["DBInstanceStatus"] = "available"+ return++ engine = instance.get("Engine", "postgres")+ engine_version = instance.get("EngineVersion") or _default_engine_version(engine)+ master_user = instance.get("MasterUsername", "admin")+ master_pass = instance.get("_MasterUserPassword", "password")+ db_name = instance.get("DBName") or "mydb"+ endpoint = instance.get("Endpoint") or {}+ host_port = endpoint.get("Port") or _next_port()++ image, env_vars, container_port, data_path = _docker_image_for_engine(+ engine, engine_version, master_user, master_pass, db_name,+ )+ if not image:+ instance["DBInstanceStatus"] = "available"+ return++ container_name = f"ministack-rds-{db_id}"+ try:+ existing = docker_client.containers.get(container_name)+ try:+ existing.stop(timeout=2)+ except Exception:+ pass+ try:+ existing.remove(v=False)+ except Exception as e:+ logger.warning("RDS: failed to remove stale container %s: %s",+ container_name, e)+ except Exception:+ pass # No existing container with that name — fine++ ms_network = _get_ministack_network(docker_client)+ container_kwargs = dict(+ image=image, detach=True,+ environment=env_vars,+ ports={f"{container_port}/tcp": host_port},+ name=container_name,+ labels={"ministack": "rds", "db_id": db_id},+ )+ if ms_network:+ container_kwargs["network"] = ms_network+ if RDS_PERSIST:+ container_kwargs["volumes"] = {+ f"ministack-rds-{db_id}-data": {"bind": data_path, "mode": "rw"},+ }+ else:+ container_kwargs["tmpfs"] = {+ data_path: f"rw,noexec,nosuid,size={RDS_TMPFS_SIZE}",+ }++ try:+ container = docker_client.containers.run(**container_kwargs)+ except Exception as e:+ logger.warning("RDS: failed to respawn container for %s: %s", db_id, e)+ instance["DBInstanceStatus"] = "failed"+ return++ instance["_docker_container_id"] = container.id++ internal_host = None+ internal_port = None+ if ms_network:+ try:+ container.reload()+ networks = container.attrs.get(+ "NetworkSettings", {}).get("Networks", {})+ container_ip = networks.get(ms_network, {}).get("IPAddress", "")+ if container_ip:+ internal_host = container_ip+ internal_port = container_port+ instance.setdefault("Endpoint", {})["Address"] = container_ip+ instance["Endpoint"]["Port"] = container_port+ except Exception:+ pass+ instance["_internal_address"] = internal_host+ instance["_internal_port"] = internal_port+ instance["DBInstanceStatus"] = "available"+ logger.info("RDS: respawned container %s for instance %s",+ container_name, db_id)try:diff --git a/tests/test_rds.py b/tests/test_rds.pyindex 913f30e6..bcf7d350 100644--- a/tests/test_rds.py+++ b/tests/test_rds.py@@ -87,7 +87,10 @@ def test_rds_create_instance_v2(rds):)inst = resp["DBInstance"]assert inst["DBInstanceIdentifier"] == "rds-ci-v2"- assert inst["DBInstanceStatus"] == "available"+ # Real AWS CreateDBInstance returns "creating" when a backing container+ # is being spawned, "available" when the call is control-plane-only.+ # Both are valid post-create states; ministack mirrors that.+ assert inst["DBInstanceStatus"] in ("available", "creating")assert inst["Engine"] == "postgres"assert "Address" in inst["Endpoint"]assert "Port" in inst["Endpoint"]@@ -1198,6 +1201,22 @@ def test_rds_restore_from_snapshot_not_found(rds):assert exc.value.response["Error"]["Code"] == "DBSnapshotNotFound"+def _wait_for_status(rds, db_id, expected, timeout=10):+ """Poll DescribeDBInstances until status == expected. Needed because+ CreateDBInstance spawns a background readiness thread that flips status+ to "available" on its own clock, which can race with a subsequent+ StopDBInstance and overwrite the "stopped" state we just set."""+ deadline = time.time() + timeout+ last = None+ while time.time() < deadline:+ last = rds.describe_db_instances(+ DBInstanceIdentifier=db_id)["DBInstances"][0]["DBInstanceStatus"]+ if last == expected:+ return last+ time.sleep(0.2)+ return last++def test_rds_start_db_instance(rds):"""StartDBInstance transitions a stopped instance to available."""rds.create_db_instance(@@ -1209,9 +1228,11 @@ def test_rds_start_db_instance(rds):AllocatedStorage=10,)try:+ # Let the bg readiness thread (if any) settle before stopping, so it+ # can't race-overwrite "stopped" back to "available".+ _wait_for_status(rds, "start-test", "available")rds.stop_db_instance(DBInstanceIdentifier="start-test")- stopped = rds.describe_db_instances(DBInstanceIdentifier="start-test")["DBInstances"][0]- assert stopped["DBInstanceStatus"] == "stopped"+ assert _wait_for_status(rds, "start-test", "stopped") == "stopped"resp = rds.start_db_instance(DBInstanceIdentifier="start-test")assert resp["DBInstance"]["DBInstanceStatus"] == "available"@@ -1240,11 +1261,13 @@ def test_rds_stop_db_instance(rds):AllocatedStorage=10,)try:+ # Wait for bg readiness thread to settle so it can't race-overwrite+ # the "stopped" state.+ _wait_for_status(rds, "stop-test", "available")resp = rds.stop_db_instance(DBInstanceIdentifier="stop-test")assert resp["DBInstance"]["DBInstanceStatus"] == "stopped"- desc = rds.describe_db_instances(DBInstanceIdentifier="stop-test")["DBInstances"][0]- assert desc["DBInstanceStatus"] == "stopped"+ assert _wait_for_status(rds, "stop-test", "stopped") == "stopped"finally:rds.delete_db_instance(DBInstanceIdentifier="stop-test", SkipFinalSnapshot=True)@@ -1445,6 +1468,153 @@ def test_rds_create_db_instance_postgres_18(rds):rds.delete_db_instance(DBInstanceIdentifier="pg18-test", SkipFinalSnapshot=True)+def test_rds_restore_state_respawns_docker_container(monkeypatch):+ """restore_state must spawn a Docker container for every persisted+ instance. Without this, instances come back marked "available" with no+ running container, and the metadata-only StartDBInstance /+ RebootDBInstance ops can't recover them. Regression test for #692.+ """+ from ministack.services import rds as m++ runs = []++ class FakeContainer:+ def __init__(self, name, container_id="cid-fake"):+ self.id = container_id+ self.name = name+ self.attrs = {"NetworkSettings": {"Networks": {}}}++ def reload(self): pass+ def stop(self, timeout=2): pass+ def remove(self, v=False): pass++ class FakeContainers:+ def get(self, name):+ raise Exception("not found")++ def run(self, **kwargs):+ runs.append(kwargs)+ return FakeContainer(kwargs["name"])++ class FakeDocker:+ def __init__(self):+ self.containers = FakeContainers()++ monkeypatch.setattr(m, "_get_docker", lambda: FakeDocker())+ monkeypatch.setattr(m, "_get_ministack_network", lambda c: None)++ db_id = "respawn-test-db"+ persisted_state = {+ "instances": {db_id: {+ "DBInstanceIdentifier": db_id,+ "Engine": "postgres",+ "EngineVersion": "16.3",+ "MasterUsername": "admin",+ "_MasterUserPassword": "password123",+ "DBName": "mydb",+ "DBInstanceStatus": "available",+ "Endpoint": {"Address": "localhost", "Port": 15500, "HostedZoneId": "Z"},+ }},+ "clusters": {}, "subnet_groups": {}, "param_groups": {},+ "snapshots": {}, "db_cluster_param_groups": {},+ "db_cluster_snapshots": {}, "option_groups": {},+ "global_clusters": {}, "tags": {}, "port_counter": 15500,+ }++ m._instances.clear()+ m.restore_state(persisted_state)++ deadline = time.time() + 5+ while time.time() < deadline and not runs:+ time.sleep(0.05)++ assert runs, "restore_state did not respawn the Docker container"+ assert runs[0]["name"] == f"ministack-rds-{db_id}"+ assert runs[0]["image"].endswith("postgres:16-alpine")+ assert runs[0]["environment"]["POSTGRES_USER"] == "admin"+ assert runs[0]["environment"]["POSTGRES_PASSWORD"] == "password123"+ assert runs[0]["environment"]["POSTGRES_DB"] == "mydb"+ assert runs[0]["labels"] == {"ministack": "rds", "db_id": db_id}++ restored = m._instances.get(db_id)+ assert restored is not None+ assert restored["_docker_container_id"] == "cid-fake"+ assert restored["DBInstanceStatus"] == "available"++ m._instances.clear()+++def test_rds_restore_state_removes_stale_container_before_respawn(monkeypatch):+ """If a container with the deterministic name already exists, restore+ must remove the stale one before re-creating, otherwise containers.run+ would fail with a name conflict.+ """+ from ministack.services import rds as m++ runs = []+ removed = []++ class FakeContainer:+ def __init__(self, name, container_id="cid-new"):+ self.id = container_id+ self.name = name+ self.attrs = {"NetworkSettings": {"Networks": {}}}++ def reload(self): pass+ def stop(self, timeout=2): pass+ def remove(self, v=False):+ removed.append(self.name)++ stale = FakeContainer(name="ministack-rds-stale-db", container_id="cid-stale")++ class FakeContainers:+ def get(self, name):+ if name == "ministack-rds-stale-db":+ return stale+ raise Exception("not found")++ def run(self, **kwargs):+ runs.append(kwargs)+ return FakeContainer(kwargs["name"])++ class FakeDocker:+ def __init__(self):+ self.containers = FakeContainers()++ monkeypatch.setattr(m, "_get_docker", lambda: FakeDocker())+ monkeypatch.setattr(m, "_get_ministack_network", lambda c: None)++ persisted = {+ "instances": {"stale-db": {+ "DBInstanceIdentifier": "stale-db",+ "Engine": "postgres",+ "EngineVersion": "16.3",+ "MasterUsername": "admin",+ "_MasterUserPassword": "pw",+ "DBName": "db",+ "DBInstanceStatus": "available",+ "Endpoint": {"Address": "localhost", "Port": 15501, "HostedZoneId": "Z"},+ }},+ "clusters": {}, "subnet_groups": {}, "param_groups": {},+ "snapshots": {}, "db_cluster_param_groups": {},+ "db_cluster_snapshots": {}, "option_groups": {},+ "global_clusters": {}, "tags": {}, "port_counter": 15501,+ }++ m._instances.clear()+ m.restore_state(persisted)++ deadline = time.time() + 5+ while time.time() < deadline and not runs:+ time.sleep(0.05)++ assert "ministack-rds-stale-db" in removed, "stale container not removed"+ assert runs, "fresh container not spawned after removing stale one"+ assert runs[0]["name"] == "ministack-rds-stale-db"++ m._instances.clear()++# ========== from test_rds_lambda_network.py ==========# RDS+Lambda network reachability via DOCKER_NETWORK auto-detect.import io@@ -1455,11 +1625,6 @@ import zipfileimport pytest-pytestmark = pytest.mark.skipif(- not os.environ.get("DOCKER_NETWORK"),- reason="DOCKER_NETWORK not set — skipping network connectivity test",-)-_LAMBDA_ROLE = "arn:aws:iam::000000000000:role/lambda-role"@@ -1489,6 +1654,10 @@ def _wait_for_rds(rds_client, db_id, timeout=120):raise TimeoutError(f"RDS instance {db_id} not available after {timeout}s")+ not os.environ.get("DOCKER_NETWORK"),+ reason="DOCKER_NETWORK not set — skipping network connectivity test",+)def test_rds_lambda_network_connectivity(rds, lam):"""Prove that Lambda containers can TCP-connect to an RDS container."""db_id = "net-test-pg"