Class ContainerService

java.lang.Object
com.ammann.servicemanager.service.ContainerService

@ApplicationScoped public class ContainerService extends Object
Core service for Docker container lifecycle management.

Provides operations to list, start, stop, restart, and update containers as well as to retrieve and stream container logs. All mutating operations (stop, restart, update) enforce the configured container blacklist before proceeding.

The update operation performs a full container replacement: it pulls the latest image, removes the existing container, recreates it with the original configuration (environment variables, labels, ports, volumes, networks, health check), and starts the new container.

  • Constructor Details

    • ContainerService

      public ContainerService()
  • Method Details

    • listContainers

      public List<ContainerInfoDTO> listContainers(boolean showAll)
      Lists Docker containers, optionally including stopped containers.
      Parameters:
      showAll - if true, includes containers in all states; otherwise only running
      Returns:
      a list of container summary DTOs
    • restartContainer

      public void restartContainer(String containerId)
      Restarts a container after verifying it is not blacklisted.
      Parameters:
      containerId - the Docker container identifier
      Throws:
      ServiceBlacklistedException - if the container is blacklisted
    • stopContainer

      public void stopContainer(String containerId)
      Stops a container after verifying it is not blacklisted.
      Parameters:
      containerId - the Docker container identifier
      Throws:
      ServiceBlacklistedException - if the container is blacklisted
    • startContainer

      public void startContainer(String containerId)
      Starts a stopped container.
      Parameters:
      containerId - the Docker container identifier
    • checkForUpdate

      public boolean checkForUpdate(String imageName)
      Checks whether a newer version of the given image is available in the remote registry.

      Compares the local image identifier with the identifier obtained after pulling the latest tag. Returns true if the identifiers differ, indicating an update is available.

      Parameters:
      imageName - the image reference to check (including tag)
      Returns:
      true if a newer image is available in the registry
    • updateContainer

      public void updateContainer(String containerId)
      Performs a full container update by pulling the latest image and recreating the container.

      The update sequence is: pull latest image, stop the existing container, capture its network connections, remove it, create a new container preserving the original configuration (environment, labels, ports, volumes, entrypoint, health check, host config), reconnect additional networks, and start the new container.

      Parameters:
      containerId - the Docker container identifier
      Throws:
      ServiceBlacklistedException - if the container is blacklisted
      RuntimeException - if the update is interrupted or fails
    • streamContainerLogs

      public io.smallrye.mutiny.Multi<String> streamContainerLogs(String containerId, boolean follow)
      Streams container log output as a reactive Multi.

      Attaches to the container's stdout and stderr with timestamps enabled. The underlying Docker log callback is closed automatically when the stream terminates.

      Parameters:
      containerId - the Docker container identifier
      follow - if true, the stream follows new output (similar to tail -f)
      Returns:
      a reactive stream of log lines
    • getContainerLogs

      public String getContainerLogs(String containerId, int tailLines)
      Retrieves historical container logs (non-streaming).
      Parameters:
      containerId - the Docker container identifier
      tailLines - the maximum number of trailing log lines to return
      Returns:
      the concatenated log output, or an error message if retrieval is interrupted