Skip to content

[Enhancement]: Allow configuring ImageFromDockerfile to build without layer cache by default #12091

Description

@reda-alaoui

Module

Core

Proposal

ImageFromDockerfile should be configurable to build without the layer cache by default, through a configuration property read by TestcontainersConfiguration like the others:

# ~/.testcontainers.properties or classpath testcontainers.properties
imagefromdockerfile.nocache=true

or TESTCONTAINERS_IMAGEFROMDOCKERFILE_NOCACHE=true.

When set, ImageFromDockerfile.configure calls buildImageCmd.withNoCache(true) before running the user's buildImageCmdModifiers, so a call site can still turn the cache back on.

The default stays as it is today (cache on).

Limitations of the current implementation

With deleteOnExit (the default), an ImageFromDockerfile gets the session labels, and Ryuk removes it when the session ends.

Those labels are only on the final image. Its intermediate layers come from the daemon's build cache, so they are shared with every other session that builds the same Dockerfile on the same daemon.

When one session ends, Ryuk removes its image and the untagged parent layers go with it. If another session is building the same Dockerfile at that moment, its build can fail because the cached layers it is using no longer exist.

This happens on CI agents that run several builds of the same project on one Docker daemon.

The only way to avoid it today is to disable the cache on every ImageFromDockerfile:

new ImageFromDockerfile()
    .withBuildImageCmdModifier(cmd -> cmd.withNoCache(true))

This has to be repeated at every call site. If one is forgotten, you get a random failure that only shows up when builds run at the same time.

It also can't reach images built inside Testcontainers modules and libraries.

With the property, a CI environment sets it once, on the agent or in the project's testcontainers.properties, and every ImageFromDockerfile is covered.

I'm happy to contribute the change.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions