Skip to content

Add inspect(), isRunning() and getExitCode() to started containers - #1664

Open
Pushkar236 wants to merge 1 commit into
testcontainers:mainfrom
Pushkar236:feat/started-container-inspect
Open

Pushkar236 wants to merge 1 commit into
testcontainers:mainfrom
Pushkar236:feat/started-container-inspect

Conversation

@Pushkar236

Copy link
Copy Markdown
Contributor

Summary

A started container's getters return what was inspected when it was started or last restarted, so there was no way to ask whether it is still running or how it exited without going through getContainerRuntimeClient() and Dockerode.

StartedTestContainer now has:

  • inspect(): Promise<InspectResult>: a fresh inspect, mapped like the one the lifecycle hooks receive. It doesn't replace the cached result, so getId(), getMappedPort() and the other getters stay as they were.
  • isRunning(): Promise<boolean>: state.running from a fresh inspect (Docker's State.Running, so a paused container counts as running, as in Java).
  • getExitCode(): Promise<number | undefined>: the exit code of the main process, or undefined while the container is running or has never run.

InspectResult.state gains exitCode. StartedGenericContainer implements the three methods and AbstractStartedContainer forwards them, so every module's started container has them. The health status is already on InspectResult (healthCheckStatus). docs/features/containers.md gets an "Inspecting a container" section.

Verification

New generic-container-inspect.test.ts:

  • a running container: inspect() reports running, with its name and labels; isRunning() is true, getExitCode() is undefined;
  • after SIGKILL: inspect() reports exited, isRunning() is false, getExitCode() is 137;
  • the same through an AbstractStartedContainer subclass.

All three fail on main. npm run check-compiles (the package and all modules), biome ci --error-on-warnings, npm run knip, and the abstract-started, lifecycle and restart tests pass.

Not breaking (minor)

This adds members to StartedTestContainer and a field to InspectResult. Containers built on GenericContainer or AbstractStartedContainer get them automatically. Code that implements StartedTestContainer from scratch or builds an InspectResult by hand would need to add them, but neither is a documented extension point, so I've kept the minor label from the issue.

Suggested labels: enhancement, minor.

Closes #1545

The getters on a started container return what was inspected when it
started. inspect() returns a fresh InspectResult, which now includes the
exit code, and isRunning() and getExitCode() read the current state from it.
getExitCode() returns undefined while the container is running.

Closes testcontainers#1545

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add inspect() and state accessors to started containers

1 participant