Class DockerService
java.lang.Object
io.micronaut.maven.services.DockerService
Provides methods to work with Docker images.
- Since:
- 1.1
- Author:
- Álvaro Sánchez-Mariscal
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordThe exit code and the output of a container. -
Constructor Summary
ConstructorsConstructorDescriptionDockerService(org.apache.maven.project.MavenProject mavenProject, JibConfigurationService jibConfigurationService) -
Method Summary
Modifier and TypeMethodDescriptionintWaits for a container to exit.buildImage(com.github.dockerjava.api.command.BuildImageCmd builder) Builds the Docker image from the givenBuildImageCmdbuilder.com.github.dockerjava.api.command.BuildImageCmdcom.github.dockerjava.api.command.BuildImageCmdbuildImageCmd(String dockerfileName) voidcopyFileFromContainer(String containerId, String containerPath, Path target) Copies a regular file out of a container, which may have exited.copyFromContainer(String imageId, String containerPath) Copies a file from the specified container path in the given image ID, into a temporal location.Creates an unprivileged container from the given image, without starting it.intexecInContainer(String containerId, int timeoutSeconds, Consumer<String> output, String... command) Runs a command in a running container and waits for it.com.github.dockerjava.api.model.AuthConfiggetAuthConfigFor(String dockerImage, String username, String password) com.github.dockerjava.api.command.InspectImageResponseinspectImage(String image) Loads the given Dockerfile as classpath resource and copies it into a temporary location in the target directory.loadDockerfileAsResource(String name, String targetFileName) Loads the given Dockerfile as classpath resource and copies it into a temporary location in the target directory.voidlogContainerOutput(String containerId) Logs the output of a container.com.github.dockerjava.api.command.PushImageCmdpushImageCmd(String imageName) voidremoveContainer(String containerId) Removes a container, stopping it if it is running.voidremoveImage(String image) Removes an image reference without force: a tag, or the ID of an image that at most one tag refers to.runAndCaptureOutput(String imageId, int timeoutSeconds, List<String> entrypoint) Runs an image with the given entrypoint, waits for it and returns its output.voidrunPrivilegedImageAndWait(String imageId, Integer timeoutSeconds, String checkpointNetworkName, String... binds) Creates a container based on a given image, and runs it.voidsignalContainer(String containerId, String signal) Sends a signal to the main process of a running container.voidstartAndWait(String containerId, String imageId, Integer timeoutSeconds) Starts a container and waits for it to exit with status 0.voidstartContainer(String containerId) Starts a container.
-
Constructor Details
-
DockerService
@Inject public DockerService(org.apache.maven.project.MavenProject mavenProject, JibConfigurationService jibConfigurationService)
-
-
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
BuildImageCmdby loading the given Dockerfile as classpath resource. - Throws:
IOException
-
buildImageCmd
- Returns:
- a default
BuildImageCmd.
-
buildImage
Builds the Docker image from the givenBuildImageCmdbuilder.- 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 usetimeoutSeconds- the timeout in seconds for the container to finish executioncheckpointNetworkName- the name of the network to use for the containerbinds- 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 usenetworkName- the name of the network to use for the container, if anyenvironment- 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 containerimageId- the image of the container, for the error messagetimeoutSeconds- 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
Starts a container.- Parameters:
containerId- the container- Since:
- 5.1.0
-
awaitExit
Waits for a container to exit.- Parameters:
containerId- the containertimeoutSeconds- 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
Sends a signal to the main process of a running container.- Parameters:
containerId- the containersignal- the signal, for exampleSIGTERM- 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 containertimeoutSeconds- the timeout in secondsoutput- receives the output of the command, line by linecommand- 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 usetimeoutSeconds- the timeout in secondsentrypoint- 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
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 containercontainerPath- the absolute path of the file in the containertarget- the file to write- Throws:
IOException- if the file cannot be copied- Since:
- 5.1.0
-
removeContainer
Removes a container, stopping it if it is running.- Parameters:
containerId- the container- Since:
- 5.1.0
-
removeImage
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
- Parameters:
image- the image name or ID- Returns:
- the image details
- Since:
- 5.1.0
-
getDaemonPlatform
- Returns:
- the platform of the Docker daemon, as
os/architecturewith Go architecture names such aslinux/amd64orlinux/arm64 - Throws:
IllegalStateException- if the Docker daemon is not reachable- Since:
- 5.1.0
-
copyFromContainer
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
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
- Parameters:
imageName- the image name- Returns:
- a
PushImageCmdfrom the given image name.
-
getAuthConfigFor
public com.github.dockerjava.api.model.AuthConfig getAuthConfigFor(String dockerImage, String username, String password) - Parameters:
dockerImage- the image nameusername- the usernamepassword- the password- Returns:
- an
AuthConfigobject for the given image, username and password.
-