Skip to content

fix: run containers with --rm to avoid lingering exited containers#348

Open
gtsiolis wants to merge 2 commits into
mainfrom
devx-943-run-cli-containers-with-rm-to-avoid-lingering-exited-55a3
Open

fix: run containers with --rm to avoid lingering exited containers#348
gtsiolis wants to merge 2 commits into
mainfrom
devx-943-run-cli-containers-with-rm-to-avoid-lingering-exited-55a3

Conversation

@gtsiolis

Copy link
Copy Markdown
Member

Containers were created without --rm (AutoRemove=false), so stopped containers stayed in Exited state and could linger indefinitely on mixed hosts. This sets AutoRemove on the HostConfig in DockerRuntime.Start, the Docker SDK equivalent of --rm, matching the Python CLI lifecycle.

The existing Stop already tolerates the container being gone (it ignores conflict/not-found from ContainerRemove), so it stays correct now that Docker may auto-remove the container on stop.

Adds an auto remove assertion to TestStartCommandSetsUpContainerCorrectly to guard against regression.

@gtsiolis gtsiolis marked this pull request as ready for review June 29, 2026 21:49
@gtsiolis gtsiolis requested a review from a team as a code owner June 29, 2026 21:49
@gtsiolis gtsiolis self-assigned this Jun 30, 2026
@gtsiolis gtsiolis added semver: patch docs: skip Pull request does not require documentation changes labels Jun 30, 2026
@gtsiolis gtsiolis force-pushed the devx-943-run-cli-containers-with-rm-to-avoid-lingering-exited-55a3 branch 2 times, most recently from 27f0184 to add38b3 Compare July 1, 2026 07:04
gtsiolis and others added 2 commits July 1, 2026 13:53
Containers were created without --rm (AutoRemove=false), leaving stopped
containers in Exited state indefinitely on mixed hosts. Set AutoRemove on
the HostConfig to match the Python CLI lifecycle.

Generated with [Linear](https://linear.app/localstack/issue/DEVX-943/run-cli-containers-with-rm-to-avoid-lingering-exited-containers#agent-session-0a1c1991)

Co-authored-by: linear-code[bot] <222613912+linear-code[bot]@users.noreply.github.com>
Running containers with --rm (AutoRemove) introduced two regressions that broke `lstk restart` / start-over-existing and erased crash diagnostics:

1. Removal race. With --rm, stopping a container kicks off an asynchronous removal. The start flow's "remove existing container before create" (pullImages -> rt.Remove) then hit "removal of container ... is already in progress" (a conflict, not not-found) and failed; even when tolerated, the name could still be taken when ContainerCreate ran next. DockerRuntime.Remove now tolerates the conflict and waits until the container is actually gone before returning, so the subsequent create does not race the in-flight removal. This fixes TestRestartCommandSucceeds, TestRestartCommandPersistFlagSetsPersistenceEnv, and TestSAME2EDeployCustomAccount.

2. Lost startup logs. awaitStartup fetched the container's logs to explain a crash, but --rm removed the container before the poll detected the exit, so the fetch returned nothing. The start flow now follows the container's logs into a bounded buffer while it runs, so the tail survives auto-removal and still explains a failed start.

Also make the custom-image integration test's post-hoc container inspect best-effort, since the stub container is auto-removed the instant it exits; the "Using local image" output remains the authoritative signal.
@gtsiolis gtsiolis force-pushed the devx-943-run-cli-containers-with-rm-to-avoid-lingering-exited-55a3 branch from add38b3 to a2ebb89 Compare July 1, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs: skip Pull request does not require documentation changes semver: patch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant