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:
parent
458b2c605f
commit
06f34cdc6a
|
|
@ -189,33 +189,30 @@ class ContainerManager:
|
||||||
'spawner.managed': 'true'
|
'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:
|
try:
|
||||||
existing_container = self._get_client().containers.get(container_name)
|
filters = {
|
||||||
print(f"[SPAWNER] Container {container_name} existiert bereits (Status: {existing_container.status})")
|
'label': [
|
||||||
|
f'spawner.user_id={user_id}',
|
||||||
if existing_container.status == 'running':
|
f'spawner.container_type={container_type}'
|
||||||
# Container läuft bereits
|
]
|
||||||
return existing_container.id, 8080
|
}
|
||||||
else:
|
old_containers = self._get_client().containers.list(all=True, filters=filters)
|
||||||
# Container gestoppt - versuche zu starten
|
for old_container in old_containers:
|
||||||
try:
|
if old_container.status == 'running':
|
||||||
existing_container.start()
|
|
||||||
print(f"[SPAWNER] Existierender Container {container_name} neu gestartet")
|
|
||||||
return existing_container.id, 8080
|
|
||||||
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)}")
|
|
||||||
try:
|
try:
|
||||||
existing_container.remove(force=True)
|
old_container.stop(timeout=5)
|
||||||
print(f"[SPAWNER] Alten Container {container_name} gelöscht - erstelle neuen")
|
print(f"[SPAWNER] Alter Container {old_container.name} gestoppt")
|
||||||
# Fahre fort um neuen Container zu erstellen
|
except Exception as e:
|
||||||
except Exception as remove_err:
|
print(f"[SPAWNER] WARNUNG: Kann alten Container nicht stoppen: {str(e)}")
|
||||||
print(f"[SPAWNER] WARNUNG: Kann alten Container nicht löschen: {str(remove_err)}")
|
try:
|
||||||
# Fahre trotzdem fort und versuche neuen zu erstellen
|
old_container.remove(force=True)
|
||||||
except docker.errors.NotFound:
|
print(f"[SPAWNER] Alter Container {old_container.name} gelöscht (Traefik-Konflikt-Prävention)")
|
||||||
# Container existiert nicht - das ist normal, weiterfahren
|
except Exception as e:
|
||||||
pass
|
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
|
# Logging: Traefik-Labels ausgeben
|
||||||
print(f"[SPAWNER] Creating {container_type} container user-{slug}-{container_type}-{user_id}")
|
print(f"[SPAWNER] Creating {container_type} container user-{slug}-{container_type}-{user_id}")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user