Skip to content

dockerClient.build doesn't throw exception on failure #897

@SumOys

Description

@SumOys

I was on version 2025-01-19T00-00-00-groovy-4 but I updated to 2025-07-26T19-45-00-groovy-4 and still have this issue.

When trying to build a Docker Image like the following:

    static buildCIContainer(String name) {

        // Timestamped tag
        def timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"))
        def tag = "${name}-ci:${timestamp}"

        log.info("Building Container: $tag")
        def tarStream = TarArchiver.createTarDirectoryStream("${dockerfileDirectory}/${name}")
        def dockerClient = new DockerClientImpl()

        // Gentoo Container might take a few hours
        // but nothing should take longer than a day
        def timeout = ChronoUnit.DAYS.getDuration()

        dockerClient.build(
            new StreamCallback<BuildInfo>() {
                @Override
                void onNext(BuildInfo buildInfo) {
                    // Log build progress
                    if (buildInfo.stream != null) {
                        log.info(buildInfo.stream.trim())
                    }
                }
            },
            timeout,
            tag,
            tarStream,
        )

        log.info("Built Docker Container $tag")
        saveContainerImageId(name, tag)
        return tag
    }

If I have invalid statement in my Dockerfile and the build fails, execution continues. I dug through the code and the StreamCallback interface does have a default implementation of the onFailure callback that should throw an exception:

public interface StreamCallback<T> {
    default void onStarting(Cancellable cancellable) {
    }

    void onNext(T var1);

    default void onFailed(Exception e) {
        throw new RuntimeException(e);
    }

    default void onFinished() {
    }
}

but even when I modify my build command to explicitly override that callback, I never see this message in my logs or the script exit with the error exit code:

        dockerClient.build(
            new StreamCallback<BuildInfo>() {
                @Override
                void onNext(BuildInfo buildInfo) {
                    // Log build progress
                    if (buildInfo.stream != null) {
                        log.info(buildInfo.stream.trim())
                    }
                }

                @Override
                void onFailed(Exception e) {
                    log.error("Build failure", e)
                    System.exit(2)
                }
            },
            timeout,
            tag,
            tarStream,
        )

Is there an issue where the dockerClient implementation isn't firing that callback on a build failure?

(Source here: https://gitlab.com/djsumdog/turbulent)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions