testcontainers-cpp 0.2.0
Native C++20 Testcontainers over the Docker Engine API
Loading...
Searching...
No Matches
testcontainers::GenericBuildableImage Class Reference

A reusable, copyable description of an image to BUILD from a Dockerfile and a build context. More...

#include <testcontainers/GenericBuildableImage.hpp>

Public Member Functions

 GenericBuildableImage (std::string_view name, std::string_view tag="latest")
 Construct a buildable image with the given name and tag.
GenericBuildableImagewith_dockerfile (std::filesystem::path source_path)
 Add a Dockerfile from the host filesystem, placed at Dockerfile in the build context.
GenericBuildableImagewith_dockerfile_string (std::string content)
 Add a Dockerfile from an inline string, placed at Dockerfile in the build context.
GenericBuildableImagewith_file (std::filesystem::path source_path, std::string target)
 Add a host file or directory to the build context at target (a directory is added recursively, each regular file under target/<relpath>; empty directories are not shipped).
GenericBuildableImagewith_data (std::string data, std::string target)
 Add in-memory bytes to the build context at target. data may be binary.
GenericBuildableImagewith_build_arg (std::string key, std::string value)
 Add a build argument (buildargs=), e.g. with_build_arg("VERSION", "1.2").
GenericBuildableImagewith_target (std::string stage)
 Build only up to a named stage of a multi-stage Dockerfile (target=).
GenericBuildableImagewith_no_cache (bool no_cache=true)
 Disable the build cache (nocache=).
GenericBuildableImagewith_pull (bool pull=true)
 Always attempt to pull a newer version of the base image (pull=).
GenericBuildableImagewith_build_log_consumer (docker::BuildLogConsumer consumer)
 Stream the build output live: consumer is called from build() with each decoded output line (step banners and the steps' own stdout/stderr) as the daemon emits it, instead of the output staying invisible until the build ends.
const std::string & name () const noexcept
const std::string & tag () const noexcept
std::string descriptor () const
 The image reference the build produces (<name>:<tag>).
const std::vector< CopyToContainer > & build_context () const noexcept
const std::vector< std::pair< std::string, std::string > > & build_args () const noexcept
const std::string & target () const noexcept
bool no_cache () const noexcept
bool pull () const noexcept
const docker::BuildLogConsumerbuild_log_consumer () const noexcept
GenericImage build () const
 Build the image (tagged <name>:<tag>) from the configured Dockerfile and build context, and return a runnable GenericImage for it.

Detailed Description

A reusable, copyable description of an image to BUILD from a Dockerfile and a build context.

Construct it with a name and tag, add the Dockerfile (from a host path or an inline string) plus any context files/data, then call build() to build the image (tagged <name>:<tag>) and get back a runnable GenericImage:

.with_dockerfile_string("FROM alpine:3.20\n...")
.with_data("hi", "/hello.txt")
.build();
A RAII handle to a running container, normally obtained from GenericImage::start().
Definition Container.hpp:44
GenericBuildableImage(std::string_view name, std::string_view tag="latest")
Construct a buildable image with the given name and tag.
Definition GenericBuildableImage.hpp:44
A reusable, copyable description of a container to run: image reference, exposed ports,...
Definition GenericImage.hpp:48
Container start() const
Create, start, and wait for a container from this image, returning a RAII handle that removes the con...
GenericImage & with_wait(WaitFor w)
Definition GenericImage.hpp:184
WaitFor exit()
Wait until the container stops (with any exit code).
Definition WaitFor.hpp:109

The build context is a list of CopyToContainer entries (the same value type used for copy-to-container): each is either a host file/dir (host_file) or in-memory bytes (content), placed at a target path within the context. The Dockerfile is simply the entry targeting Dockerfile (Docker's default).

The with_* builders mutate in place and return *this by reference, so a named config can be configured incrementally — no consume-self, no use-after-move (mirrors GenericImage).

Public on purpose: the header pulls in only std + value types. The filesystem walk and tar packing happen in the .cpp so no libarchive/Boost leaks here.

Constructor & Destructor Documentation

◆ GenericBuildableImage()

testcontainers::GenericBuildableImage::GenericBuildableImage ( std::string_view name,
std::string_view tag = "latest" )
inlineexplicit

Construct a buildable image with the given name and tag.

Member Function Documentation

◆ with_dockerfile()

GenericBuildableImage & testcontainers::GenericBuildableImage::with_dockerfile ( std::filesystem::path source_path)
inline

Add a Dockerfile from the host filesystem, placed at Dockerfile in the build context.

◆ with_dockerfile_string()

GenericBuildableImage & testcontainers::GenericBuildableImage::with_dockerfile_string ( std::string content)
inline

Add a Dockerfile from an inline string, placed at Dockerfile in the build context.

Useful for generating a Dockerfile programmatically or embedding one in a test.

◆ with_file()

GenericBuildableImage & testcontainers::GenericBuildableImage::with_file ( std::filesystem::path source_path,
std::string target )
inline

Add a host file or directory to the build context at target (a directory is added recursively, each regular file under target/<relpath>; empty directories are not shipped).

A .dockerignore at the directory's root filters the walk with docker build semantics; for a directory mapped to the context root (target empty) the .dockerignore itself always ships, and so does its Dockerfile — unless one was added via with_dockerfile* or with_data, which always takes precedence. File bytes are read only while the build uploads, so large contexts are never held in memory. The Dockerfile must be named Dockerfile unless added via with_dockerfile*.

◆ with_data()

GenericBuildableImage & testcontainers::GenericBuildableImage::with_data ( std::string data,
std::string target )
inline

Add in-memory bytes to the build context at target. data may be binary.

◆ with_build_arg()

GenericBuildableImage & testcontainers::GenericBuildableImage::with_build_arg ( std::string key,
std::string value )
inline

Add a build argument (buildargs=), e.g. with_build_arg("VERSION", "1.2").

◆ with_target()

GenericBuildableImage & testcontainers::GenericBuildableImage::with_target ( std::string stage)
inline

Build only up to a named stage of a multi-stage Dockerfile (target=).

◆ with_no_cache()

GenericBuildableImage & testcontainers::GenericBuildableImage::with_no_cache ( bool no_cache = true)
inline

Disable the build cache (nocache=).

◆ with_pull()

GenericBuildableImage & testcontainers::GenericBuildableImage::with_pull ( bool pull = true)
inline

Always attempt to pull a newer version of the base image (pull=).

◆ with_build_log_consumer()

GenericBuildableImage & testcontainers::GenericBuildableImage::with_build_log_consumer ( docker::BuildLogConsumer consumer)
inline

Stream the build output live: consumer is called from build() with each decoded output line (step banners and the steps' own stdout/stderr) as the daemon emits it, instead of the output staying invisible until the build ends.

Errors are still thrown as DockerError either way.

◆ name()

const std::string & testcontainers::GenericBuildableImage::name ( ) const
inlinenoexcept

◆ tag()

const std::string & testcontainers::GenericBuildableImage::tag ( ) const
inlinenoexcept

◆ descriptor()

std::string testcontainers::GenericBuildableImage::descriptor ( ) const
inline

The image reference the build produces (<name>:<tag>).

◆ build_context()

const std::vector< CopyToContainer > & testcontainers::GenericBuildableImage::build_context ( ) const
inlinenoexcept

◆ build_args()

const std::vector< std::pair< std::string, std::string > > & testcontainers::GenericBuildableImage::build_args ( ) const
inlinenoexcept

◆ target()

const std::string & testcontainers::GenericBuildableImage::target ( ) const
inlinenoexcept

◆ no_cache()

bool testcontainers::GenericBuildableImage::no_cache ( ) const
inlinenoexcept

◆ pull()

bool testcontainers::GenericBuildableImage::pull ( ) const
inlinenoexcept

◆ build_log_consumer()

const docker::BuildLogConsumer & testcontainers::GenericBuildableImage::build_log_consumer ( ) const
inlinenoexcept

◆ build()

GenericImage testcontainers::GenericBuildableImage::build ( ) const

Build the image (tagged <name>:<tag>) from the configured Dockerfile and build context, and return a runnable GenericImage for it.

Throws DockerError on failure (including a build error embedded in the daemon's streamed output, which Docker reports with an HTTP 200).

The built image is session-scoped, like every testcontainers resource: it carries the testcontainers labels, and the Ryuk reaper removes it shortly after this process exits (the base image and pulled layers are untouched). build() starts that reaper on first use, so it can also throw for a reaper-startup failure, before any build work. For an image that must outlive the test run, disable the reaper (TESTCONTAINERS_RYUK_DISABLED=1).