Overview
TheDockerBuilder class provides functionality to build Docker images from a Dockerfile and publish them to a container registry. It uses the Docker Engine API client to interact with Docker.
Constructor
DockerBuilder instance. The Docker API client is lazily initialized on the first build() or publish() call.
Methods
build()
Builds a Docker image from a Dockerfile in the specified directory.The tag to assign to the built image (e.g.,
myapp:latest or username/myapp:v1.0)The build context path containing the Dockerfile. Defaults to the current directory.
Behavior
During the build process:- Initializes the Docker API client (if not already initialized) with automatic version detection
- Calls the Docker API to build the image with UTF-8 encoding
- Streams build output in real-time
- Processes each line of output from the Docker daemon
- Logs build progress at debug level
- Raises an exception if the build fails
Error Handling
If the Docker daemon returns an error during the build process, the method:- Logs the error message at error level
- Raises an
Exceptionwith the message:"Image build failed: <error_message>"
Example
publish()
Pushes a Docker image to a container registry.The image tag to push to the registry (e.g.,
username/myapp:latest)Behavior
During the publish process:- Initializes the Docker API client (if not already initialized) with automatic version detection
- Pushes the image to the registry with streaming enabled
- Processes the push progress stream in real-time
- Logs push status and progress at debug level
- Raises an exception if the push fails
Error Handling
If the Docker daemon returns an error during the push process, the method:- Logs the error message at error level
- Raises an
Exceptionwith the message:"Image build failed: <error_message>"
docker login or Docker config).
Example
Complete Usage Example
Output Processing
TheDockerBuilder processes streaming output from the Docker daemon and handles several types of responses:
- Stream output: Build logs (e.g., step execution output)
- Status updates: Push progress with percentage completion
- Auxiliary data: Final push completion information
- Error messages: Failures during build or push operations
logging module:
- Normal progress: logged at
DEBUGlevel - JSON decode errors: logged at
WARNINGlevel - Build/push failures: logged at
ERRORlevel and raised as exceptions
Dependencies
TheDockerBuilder requires:
dockerpackage withAPIClientsupport- Access to Docker daemon (via socket or TCP)
- Proper permissions to build and push images