Class DockerService

java.lang.Object
io.micronaut.maven.services.DockerService

@Singleton public class DockerService extends Object
Provides methods to work with Docker images.
Since:
1.1
Author:
Álvaro Sánchez-Mariscal
  • Constructor Details

  • Method Details

    • buildImageCmd

      public com.github.dockerjava.api.command.BuildImageCmd buildImageCmd(String dockerfileName) throws IOException
      Parameters:
      dockerfileName - the name of the Dockerfile to load
      Returns:
      the BuildImageCmd by loading the given Dockerfile as classpath resource.
      Throws:
      IOException
    • buildImageCmd

      public com.github.dockerjava.api.command.BuildImageCmd buildImageCmd()
      Returns:
      a default BuildImageCmd.
    • buildImage

      public String buildImage(com.github.dockerjava.api.command.BuildImageCmd builder)
      Builds the Docker image from the given BuildImageCmd builder.
      Parameters:
      builder - The builder to use.
      Returns:
      The resulting image ID.
    • runPrivilegedImageAndWait

      public void runPrivilegedImageAndWait(String imageId, Integer timeoutSeconds, String checkpointNetworkName, String... binds) throws IOException
      Creates a container based on a given image, and runs it.
      Parameters:
      imageId - the image to use
      timeoutSeconds - the timeout in seconds for the container to finish execution
      checkpointNetworkName - the name of the network to use for the container
      binds - the bind mounts to use
      Throws:
      IOException
    • createContainer

      @Internal public String createContainer(String imageId, String networkName, Map<String,String> environment)
      Creates an unprivileged container from the given image, without starting it.
      Parameters:
      imageId - the image to use
      networkName - the name of the network to use for the container, if any
      environment - the environment variables to set, on top of the image ones
      Returns:
      the container ID
      Since:
      5.1.0
    • startAndWait

      @Internal public void startAndWait(String containerId, String imageId, Integer timeoutSeconds) throws IOException
      Starts a container and waits for it to exit with status 0. Otherwise, logs its output and fails.
      Parameters:
      containerId - the container
      imageId - the image of the container, for the error message
      timeoutSeconds - the timeout in seconds for the container to finish execution
      Throws:
      IOException - if the container does not exit with status 0 within the timeout
      Since:
      5.1.0
    • startContainer

      @Internal public void startContainer(String containerId)
      Starts a container.
      Parameters:
      containerId - the container
      Since:
      5.1.0
    • awaitExit

      @Internal public int awaitExit(String containerId, int timeoutSeconds) throws IOException
      Waits for a container to exit.
      Parameters:
      containerId - the container
      timeoutSeconds - the timeout in seconds
      Returns:
      the exit code of the container
      Throws:
      IOException - if the container does not exit within the timeout
      Since:
      5.1.0
    • signalContainer

      @Internal public void signalContainer(String containerId, String signal)
      Sends a signal to the main process of a running container.
      Parameters:
      containerId - the container
      signal - the signal, for example SIGTERM
      Since:
      5.1.0
    • execInContainer

      @Internal public int execInContainer(String containerId, int timeoutSeconds, Consumer<String> output, String... command) throws IOException
      Runs a command in a running container and waits for it.
      Parameters:
      containerId - the container
      timeoutSeconds - the timeout in seconds
      output - receives the output of the command, line by line
      command - the command and its arguments
      Returns:
      the exit code of the command
      Throws:
      IOException - if the command does not finish within the timeout
      Since:
      5.1.0
    • runAndCaptureOutput

      @Internal public DockerService.ContainerOutput runAndCaptureOutput(String imageId, int timeoutSeconds, List<String> entrypoint) throws IOException
      Runs an image with the given entrypoint, waits for it and returns its output.
      Parameters:
      imageId - the image to use
      timeoutSeconds - the timeout in seconds
      entrypoint - the entrypoint, which replaces the image entrypoint and command
      Returns:
      the exit code and the output of the container
      Throws:
      IOException - if the container does not exit within the timeout
      Since:
      5.1.0
    • logContainerOutput

      @Internal public void logContainerOutput(String containerId)
      Logs the output of a container.
      Parameters:
      containerId - the container
      Since:
      5.1.0
    • copyFileFromContainer

      @Internal public void copyFileFromContainer(String containerId, String containerPath, Path target) throws IOException
      Copies a regular file out of a container, which may have exited.
      Parameters:
      containerId - the container
      containerPath - the absolute path of the file in the container
      target - the file to write
      Throws:
      IOException - if the file cannot be copied
      Since:
      5.1.0
    • removeContainer

      @Internal public void removeContainer(String containerId)
      Removes a container, stopping it if it is running.
      Parameters:
      containerId - the container
      Since:
      5.1.0
    • removeImage

      @Internal public void removeImage(String image)
      Removes an image reference without force: a tag, or the ID of an image that at most one tag refers to. Docker deletes the image when no tag refers to it anymore, and fails instead of removing an image that other tags refer to or that a container uses.
      Parameters:
      image - the tag or the image ID
      Since:
      5.1.0
    • inspectImage

      @Internal public com.github.dockerjava.api.command.InspectImageResponse inspectImage(String image)
      Parameters:
      image - the image name or ID
      Returns:
      the image details
      Since:
      5.1.0
    • getDaemonPlatform

      @Internal public String getDaemonPlatform()
      Returns:
      the platform of the Docker daemon, as os/architecture with Go architecture names such as linux/amd64 or linux/arm64
      Throws:
      IllegalStateException - if the Docker daemon is not reachable
      Since:
      5.1.0
    • copyFromContainer

      public File copyFromContainer(String imageId, String containerPath)
      Copies a file from the specified container path in the given image ID, into a temporal location.
      Parameters:
      imageId - The image ID.
      containerPath - The container path.
      Returns:
      The temporal file.
    • loadDockerfileAsResource

      Loads the given Dockerfile as classpath resource and copies it into a temporary location in the target directory.
      Parameters:
      name - the name of the Dockerfile.
      Returns:
      the file where the Dockerfile was copied to.
      Throws:
      IOException
    • loadDockerfileAsResource

      public File loadDockerfileAsResource(String name, String targetFileName) throws IOException
      Loads the given Dockerfile as classpath resource and copies it into a temporary location in the target directory.
      Parameters:
      name - the name of the Dockerfile.
      targetFileName - the name of the file to copy the Dockerfile to.
      Returns:
      the file where the Dockerfile was copied to.
      Throws:
      IOException
    • pushImageCmd

      public com.github.dockerjava.api.command.PushImageCmd pushImageCmd(String imageName)
      Parameters:
      imageName - the image name
      Returns:
      a PushImageCmd from the given image name.
    • getAuthConfigFor

      public com.github.dockerjava.api.model.AuthConfig getAuthConfigFor(String dockerImage, String username, String password)
      Parameters:
      dockerImage - the image name
      username - the username
      password - the password
      Returns:
      an AuthConfig object for the given image, username and password.