public class JkDockerBuild
extends java.lang.Object
This class facilitates the programmatic construction of a Docker build context directory
and the execution of the docker build
command using that context.
docker build
command is executed using the generated build context directory.
This operation requires Docker CLI (e.g., Docker Desktop) installed on the host machine.
The default Dockerfile template is as follows:
FROM ${baseImage}
${addNonRootUser}
Users can customize this template by adding steps to the dockerFileTemplate
field.
Custom tokens (e.g., ${myToken}) can also be defined, and their resolution logic specified
using the #addTokenResolver(String, Supplier<String>)
method.
Files can be imported into the build context using:
#addFsOperation()
dockerFileTemplate#addCopy(fileSystemPath, containerPath)
Once the Docker build is configured, the image can be generated using the
buildImage(String imageName)
method. The resulting image will be automatically
pushed to the Docker registry.
Intermediate steps such as dockerFileTemplate.render()
,
renderDockerFile()
, or generateContextDir()
can be used
to aid in the configuration of the Docker build process.
This class can be extended to provide a specific default template and additional convenience methods.
Modifier and Type | Class and Description |
---|---|
static class |
JkDockerBuild.AddUserStatement |
class |
JkDockerBuild.DockerfileTemplate |
static class |
JkDockerBuild.NonRootMode
Defines the modes for creating a nonroot user in a Docker image.
|
Modifier and Type | Field and Description |
---|---|
JkDockerBuild.DockerfileTemplate |
dockerfileTemplate |
Modifier | Constructor and Description |
---|---|
protected |
JkDockerBuild() |
Modifier and Type | Method and Description |
---|---|
JkDockerBuild |
addFsOperation(java.util.function.Consumer<java.nio.file.Path> fsOperation)
Adds an operation to be executed immediately after the build context directory is created.
|
JkDockerBuild |
addTokenResolver(java.lang.String token,
java.util.function.Supplier<java.lang.Object> valueSupplier)
Specifies how to resolve the given token.
|
void |
buildImage(java.nio.file.Path buildContextDir,
java.lang.String imageName)
Builds the Docker image with the specified image name.
|
java.nio.file.Path |
buildImageInTemp(java.lang.String imageName)
Similar to
buildImage(Path, String) , but without requiring a context directory to be specified. |
protected java.lang.String |
computeImportedFileRelativePath(java.nio.file.Path fileToCopy,
java.lang.String targetDirRelPath) |
void |
generateContextDir(java.nio.file.Path buildContextDir)
Generates the build context directory without building the Docker image.
|
java.lang.String |
getBaseImage() |
java.util.List<java.lang.Integer> |
getExposedPorts()
Returns an unmodifiable list of exposed ports in the Docker image.
|
protected java.lang.String |
getPortMappingArgs()
Returns a string representation of the command line arguments for port mapping in the Docker image.
|
boolean |
hasNonRootUserCreation()
Checks whether the image includes a step to create a non-root user.
|
static JkDockerBuild |
of()
Creates a
instannce. |
java.lang.String |
renderDockerfile()
Render the final Dockerfile.
|
java.lang.String |
renderInfo()
Renders a human-readable string representation of the information used
to build the image.
|
JkDockerBuild |
setAddUserStatement(JkDockerBuild.AddUserStatement addUserTemplate) |
JkDockerBuild |
setAddUserStatement(java.lang.String addUserTemplate)
Sets the Docker statement for adding a 'nonroot' user.
The placeholders "${UID}" and "${GID}" will be replaced with the user ID and group ID configured for this instance, respectively. |
JkDockerBuild |
setBaseImage(java.lang.String baseImage)
Sets the base image name to use a s the base image in the docker file.
|
JkDockerBuild |
setExposedPorts(java.lang.Integer... ports)
Adds a port to be exposed by the container.
|
JkDockerBuild |
setGroupId(int groupId)
Sets the group ID for the 'nonroot' user.
|
JkDockerBuild |
setNonRootUserCreationMode(JkDockerBuild.NonRootMode nonRootUserCreationMode) |
JkDockerBuild |
setUserId(int userId)
Sets the user ID for the 'nonroot' user.
|
protected static java.lang.String |
toDoubleQuotedArgs(java.util.List<java.lang.String> args) |
protected static java.lang.String |
toDoubleQuotedArgs(java.lang.String... args)
Transform a list as [a,b,c] in a string as "a", "b", "c"
|
public final JkDockerBuild.DockerfileTemplate dockerfileTemplate
public static JkDockerBuild of()
instannce.
public final JkDockerBuild setBaseImage(java.lang.String baseImage)
public java.lang.String getBaseImage()
public final JkDockerBuild setNonRootUserCreationMode(JkDockerBuild.NonRootMode nonRootUserCreationMode)
public final boolean hasNonRootUserCreation()
public final JkDockerBuild setAddUserStatement(java.lang.String addUserTemplate)
public final JkDockerBuild setAddUserStatement(JkDockerBuild.AddUserStatement addUserTemplate)
setAddUserStatement(String)
public final JkDockerBuild setUserId(int userId)
userId
- the user ID to assign.public final JkDockerBuild setGroupId(int groupId)
groupId
- the group ID to assign.public final JkDockerBuild setExposedPorts(java.lang.Integer... ports)
public final java.util.List<java.lang.Integer> getExposedPorts()
protected final java.lang.String getPortMappingArgs()
public JkDockerBuild addTokenResolver(java.lang.String token, java.util.function.Supplier<java.lang.Object> valueSupplier)
Object.toString()
method.public JkDockerBuild addFsOperation(java.util.function.Consumer<java.nio.file.Path> fsOperation)
This is typically used to import files or directories into the context directory.
fsOperation
- a Consumer
that accepts the path to the build context directory.public java.lang.String renderDockerfile()
public void buildImage(java.nio.file.Path buildContextDir, java.lang.String imageName)
buildContextDir
- the directory where the Dockerfile will be generated and from which COPY paths will be resolved.imageName
- the name of the image to build. It may include a tag name.public java.nio.file.Path buildImageInTemp(java.lang.String imageName)
buildImage(Path, String)
, but without requiring a context directory to be specified.A temporary context directory will be created in a random location.
public void generateContextDir(java.nio.file.Path buildContextDir)
This method is automatically called when buildImage(Path, String)
is invoked.
public java.lang.String renderInfo()
protected java.lang.String computeImportedFileRelativePath(java.nio.file.Path fileToCopy, java.lang.String targetDirRelPath)
protected static java.lang.String toDoubleQuotedArgs(java.lang.String... args)
protected static java.lang.String toDoubleQuotedArgs(java.util.List<java.lang.String> args)
toDoubleQuotedArgs(String...)