fix: Prevent Traefik router conflicts by deleting old containers

- Before spawning new container with same user_id and container_type, delete all old containers with same config
- This prevents 'Router defined multiple times with different configurations' error from Traefik
- Implements solution B: Old containers are now automatically cleaned up
- Prevents 404 errors caused by Traefik routing conflicts
This commit is contained in:
XPS\Micro 2026-03-19 07:10:04 +01:00
parent 458b2c605f
commit 06f34cdc6a

View File

@ -189,33 +189,30 @@ class ContainerManager:
'spawner.managed': 'true'
}
# Prüfe ob Container bereits existiert (z.B. nach Fehler oder fehlgeschlagener Löschung)
# Lösche ALLE alten Container mit gleicher user_id und container_type (B)
# Dies verhindert Traefik Router-Konflikte mit mehreren Containern gleicher Config
try:
existing_container = self._get_client().containers.get(container_name)
print(f"[SPAWNER] Container {container_name} existiert bereits (Status: {existing_container.status})")
if existing_container.status == 'running':
# Container läuft bereits
return existing_container.id, 8080
else:
# Container gestoppt - versuche zu starten
filters = {
'label': [
f'spawner.user_id={user_id}',
f'spawner.container_type={container_type}'
]
}
old_containers = self._get_client().containers.list(all=True, filters=filters)
for old_container in old_containers:
if old_container.status == 'running':
try:
existing_container.start()
print(f"[SPAWNER] Existierender Container {container_name} neu gestartet")
return existing_container.id, 8080
old_container.stop(timeout=5)
print(f"[SPAWNER] Alter Container {old_container.name} gestoppt")
except Exception as e:
# Container kann nicht gestartet werden - lösche ihn und erstelle neuen
print(f"[SPAWNER] Kann Container nicht starten, lösche: {str(e)}")
print(f"[SPAWNER] WARNUNG: Kann alten Container nicht stoppen: {str(e)}")
try:
existing_container.remove(force=True)
print(f"[SPAWNER] Alten Container {container_name} gelöscht - erstelle neuen")
# Fahre fort um neuen Container zu erstellen
except Exception as remove_err:
print(f"[SPAWNER] WARNUNG: Kann alten Container nicht löschen: {str(remove_err)}")
# Fahre trotzdem fort und versuche neuen zu erstellen
except docker.errors.NotFound:
# Container existiert nicht - das ist normal, weiterfahren
pass
old_container.remove(force=True)
print(f"[SPAWNER] Alter Container {old_container.name} gelöscht (Traefik-Konflikt-Prävention)")
except Exception as e:
print(f"[SPAWNER] WARNUNG: Kann alten Container nicht löschen: {str(e)}")
except Exception as e:
print(f"[SPAWNER] WARNUNG: Fehler beim Löschen alter Container: {str(e)}")
# Logging: Traefik-Labels ausgeben
print(f"[SPAWNER] Creating {container_type} container user-{slug}-{container_type}-{user_id}")