Class DockerContainer

java.lang.Object
atessera.sensei.util.DockerContainer
All Implemented Interfaces:
AutoCloseable

public final class DockerContainer extends Object implements AutoCloseable
Represents a Docker container managed by DockerRunner.

Implements AutoCloseable to guarantee container cleanup. It provides non-blocking execution management, container status inspection, truncated log retrieving, and graceful/forced termination commands.

  • Constructor Summary

    Constructors
    Constructor
    Description
    DockerContainer(String dockerExecutable, String containerId, ResourceQuota quota)
    Creates a new Docker container reference.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Guarantees container cleanup.
    Captures current container logs (stdout and stderr), truncated to quota.maxLogBytes.
    id()
    Returns the container identifier.
    boolean
    Checks whether the container is currently running.
    void
    Forces immediate container termination using docker kill.
    void
    Forces removal of the container using docker rm -f.
    void
    stop(long timeoutSeconds)
    Gracefully stops the container with a timeout.
    int
    waitFor(long timeoutSeconds)
    Waits for the container to finish execution within the specified timeout.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DockerContainer

      public DockerContainer(String dockerExecutable, String containerId, ResourceQuota quota)
      Creates a new Docker container reference.
      Parameters:
      dockerExecutable - path or name of the Docker CLI executable
      containerId - full or short Docker container ID
      quota - resource quota containing limits for log size and timeouts
  • Method Details

    • id

      public String id()
      Returns the container identifier.
      Returns:
      container ID string
    • isRunning

      public boolean isRunning()
      Checks whether the container is currently running.
      Returns:
      true if running, false otherwise
    • waitFor

      public int waitFor(long timeoutSeconds)
      Waits for the container to finish execution within the specified timeout. If the execution times out, the container is gracefully stopped and killed.
      Parameters:
      timeoutSeconds - maximum duration to wait in seconds
      Returns:
      exit code of the process inside the container, or -1 on timeout or interruption
    • getLogs

      public String getLogs()
      Captures current container logs (stdout and stderr), truncated to quota.maxLogBytes.
      Returns:
      accumulated container logs
    • stop

      public void stop(long timeoutSeconds)
      Gracefully stops the container with a timeout. If it does not stop in time, forces termination.
      Parameters:
      timeoutSeconds - timeout for graceful stop
    • kill

      public void kill()
      Forces immediate container termination using docker kill.
    • remove

      public void remove()
      Forces removal of the container using docker rm -f.
    • close

      public void close()
      Guarantees container cleanup. If the container is still running, stops and forcibly removes it.
      Specified by:
      close in interface AutoCloseable