How to use the docker build – progress= plain command?

124    Asked by JaneFisher in Devops , Asked on Jul 15, 2024

 I am currently working on a particular task that involves multiple stages and dependencies. Recently, I have noticed that my docker builds are failing intermittently. The error messages provided by default are not very detailed and I have suspected that there might be issues and problems with the specific stages or even in dependencies. How can I use the docker build – progress= plain or even docker build – progress== tty command to get a more detailed output during the time of the building process? 

Answered by Donna Chapman

In the context of DevOps, you can get more detailed output during the time of building the docker by using the docker build – progress= plain or even docker build – progress== tty command. Here are the steps given:-

Using docker build – progress= plain
Docker build –progress=plain -t your_image_name .

This option would provide you with a more verbose output in a plain text format which would show you each step of the building process explicitly.

Using docker build – progress== tty

Docker build –progress=tty -t your_image_name .

This option would Mimic a terminal-like progress output which can be more informative and easier so that you can interpret for the complex builds.

Interpreting the verbose output

Step-by-step progress

You should look for detailed logs for each build step, including which stage of the docker file is being processed.

Dependencies issues

You should check for warnings or even errors that are related to dependencies not being found or installed correctly.

Build failures

You can identify any specific command or even stages where the build process fails and you can investigate the corresponding error message.

Here is an example of Java-based code given of how you can interact with the docker by using the docker java API. This example would focus on building a docker image and capturing verbose output during the time building the process:-

Import com.github.dockerjava.api.DockerClient;
Import com.github.dockerjava.api.command.BuildImageCmd;
Import com.github.dockerjava.api.model.BuildResponseItem;
Import com.github.dockerjava.core.DockerClientBuilder;
Import com.github.dockerjava.core.command.BuildImageResultCallback;
Import com.github.dockerjava.core.command.LogContainerResultCallback;
Import com.github.dockerjava.core.command.WaitContainerResultCallback;
Import com.github.dockerjava.core.command.PullImageResultCallback;
Import org.apache.commons.io.IOUtils;
Import java.io.File;
Import java.io.FileInputStream;
Import java.io.IOException;
Public class DockerBuildExample {
    Public static void main(String[] args) throws IOException {
        // Initialize Docker client
        DockerClient dockerClient = DockerClientBuilder.getInstance().build();
        // Dockerfile path
        File dockerfile = new File(“path/to/your/Dockerfile”);
        // Image name and tag
        String imageName = “your_image_name”;
        String imageTag = “latest”;
        // Read Dockerfile content
        FileInputStream dockerfileInputStream = new FileInputStream(dockerfile);
        String dockerfileContent = IOUtils.toString(dockerfileInputStream, “UTF-8”);
        dockerfileInputStream.close();
        // Build Docker image
        BuildImageCmd buildImageCmd = dockerClient.buildImageCmd()
                .withDockerfile(dockerfileContent.getBytes())
                .withTags(new String[]{imageName + “:” + imageTag})
                .withProgressMessageHandler(message -> {
                    // Handle progress messages if needed
                    System.out.println(“Build progress: “ + message);
                });
        // Execute build and capture verbose output
        buildImageCmd.exec(new BuildImageResultCallback() {
            @Override
            Public void onNext(BuildResponseItem item) {
                // Handle each build step output
                System.out.println(“Build step: “ + item.getStream());
            }
        }).awaitImageId();
        // Print completion message
        System.out.println(“Docker image build completed.”);
        // Optionally, you can pull the built image to verify
        dockerClient.pullImageCmd(imageName)
                .withTag(imageTag)
                .exec(new PullImageResultCallback())
                .awaitSuccess();
        // Clean up: Remove the built image (if needed)
        dockerClient.removeImageCmd(imageName + “:” + imageTag).exec();
    }
}
Here is a python based approach given:-
Import docker
# Initialize Docker client
Client = docker.from_env()
# Path to Dockerfile’s directory
Dockerfile_path = ‘/path/to/your/dockerfile_directory’
# Image name and tag
Image_name = ‘your_image_name:latest’
# Build Docker image with verbose output
Print(“Building Docker image…”)
Image, logs = client.images.build(path=dockerfile_path, tag=image_name, rm=True, nocache=True)
# Print build logs
For log in logs:
    If ‘stream’ in log:
        Print(log[‘stream’].strip())
Print(f”Image ‘{image_name}’ built successfully.”)


Your Answer

Interviews

Parent Categories