pub struct GenericBuildableImage { /* private fields */ }Expand description
A generic implementation of BuildableImage for building custom Docker images.
GenericBuildableImage provides a fluent interface for constructing Docker images from
Dockerfiles and build contexts. It supports adding files and directories from the filesystem,
embedding data directly, and customizing the build process.
§Build Context Management
The build context is managed through a BuildContextBuilder that collects all files
and data needed for the Docker build. Files are automatically packaged into a TAR archive
that gets sent to the Docker daemon.
§Example: Basic Image Build
use testcontainers::{GenericBuildableImage, runners::AsyncBuilder};
#[tokio::test]
async fn test_hello() -> anyhow::Result<()> {
let image = GenericBuildableImage::new("hello-world", "my-tag")
.with_dockerfile_string(
r#"FROM alpine:latest
COPY hello.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/hello.sh
CMD ["/usr/local/bin/hello.sh"]"#
)
.with_data(
"#!/bin/sh\necho 'Hello from custom image!'",
"./hello.sh"
)
.build_image().await?;
// start container
// use it
}§Example: Multi-File Build Context
use testcontainers::{GenericBuildableImage, runners::AsyncBuilder};
#[tokio::test]
async fn test_webapp() -> anyhow::Result<()> {
let image = GenericBuildableImage::new("web-app", "1.0")
.with_dockerfile("./Dockerfile")
.with_file("./package.json", "./package.json")
.with_file("./src", "./src")
.with_data(vec![0x00, 0x01, 0x02], "./data.dat")
.build_image().await?;
// start container
// use it
}Implementations§
Source§impl GenericBuildableImage
impl GenericBuildableImage
Sourcepub fn new(name: impl Into<String>, tag: impl Into<String>) -> Self
pub fn new(name: impl Into<String>, tag: impl Into<String>) -> Self
Creates a new buildable image with the specified name and tag.
§Arguments
name- The name for the Docker image (e.g., “my-app”, “registry.com/service”)tag- The tag for the image (e.g., “latest”, “1.0”, “dev”)
Sourcepub fn with_dockerfile(self, source: impl Into<PathBuf>) -> Self
pub fn with_dockerfile(self, source: impl Into<PathBuf>) -> Self
Adds a Dockerfile from the filesystem to the build context.
§Arguments
source- Path to the Dockerfile on the local filesystem
Sourcepub fn with_dockerfile_string(self, content: impl Into<String>) -> Self
pub fn with_dockerfile_string(self, content: impl Into<String>) -> Self
Adds a Dockerfile from a string to the build context.
This is useful for generating Dockerfiles programmatically or embedding simple Dockerfiles directly in test code.
§Arguments
content- The complete Dockerfile content as a string
Sourcepub fn with_file(
self,
source: impl Into<PathBuf>,
target: impl Into<String>,
) -> Self
pub fn with_file( self, source: impl Into<PathBuf>, target: impl Into<String>, ) -> Self
Adds a file or directory from the filesystem to the build context.
Be aware, that if you don’t add the Dockerfile with the specific with_dockerfile()
or with_dockerfile_string() functions it has to be named Dockerfilein the build
context. Containerfile won’t be recognized!
§Arguments
source- Path to the file or directory on the local filesystemtarget- Path where the file should be placed in the build context
Sourcepub fn with_data(
self,
data: impl Into<Vec<u8>>,
target: impl Into<String>,
) -> Self
pub fn with_data( self, data: impl Into<Vec<u8>>, target: impl Into<String>, ) -> Self
Adds data directly to the build context as a file.
This method allows you to embed file content directly without requiring files to exist on the filesystem. Useful for generated content, templates, or small configuration files.
§Arguments
data- The file content as bytestarget- Path where the file should be placed in the build context
Trait Implementations§
Source§impl BuildableImage for GenericBuildableImage
impl BuildableImage for GenericBuildableImage
Source§type Built = GenericImage
type Built = GenericImage
Image that this buildable image produces after building.Source§fn build_context(&self) -> CopyToContainerCollection
fn build_context(&self) -> CopyToContainerCollection
Source§fn descriptor(&self) -> String
fn descriptor(&self) -> String
Auto Trait Implementations§
impl Freeze for GenericBuildableImage
impl RefUnwindSafe for GenericBuildableImage
impl Send for GenericBuildableImage
impl Sync for GenericBuildableImage
impl Unpin for GenericBuildableImage
impl UnsafeUnpin for GenericBuildableImage
impl UnwindSafe for GenericBuildableImage
Blanket Implementations§
Source§impl<T> AsyncBuilder<T> for Twhere
T: BuildableImage + Send,
impl<T> AsyncBuilder<T> for Twhere
T: BuildableImage + Send,
fn build_image<'async_trait>(
self,
) -> Pin<Box<dyn Future<Output = Result<<T as BuildableImage>::Built, TestcontainersError>> + Send + 'async_trait>>where
T: 'async_trait,
fn build_image_with<'async_trait>(
self,
options: BuildImageOptions,
) -> Pin<Box<dyn Future<Output = Result<<T as BuildableImage>::Built, TestcontainersError>> + Send + 'async_trait>>where
T: 'async_trait,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SyncBuilder<T> for Twhere
T: BuildableImage + Send,
impl<T> SyncBuilder<T> for Twhere
T: BuildableImage + Send,
Source§fn build_image(
self,
) -> Result<<T as BuildableImage>::Built, TestcontainersError>
fn build_image( self, ) -> Result<<T as BuildableImage>::Built, TestcontainersError>
blocking only.Source§fn build_image_with(
self,
options: BuildImageOptions,
) -> Result<<T as BuildableImage>::Built, TestcontainersError>
fn build_image_with( self, options: BuildImageOptions, ) -> Result<<T as BuildableImage>::Built, TestcontainersError>
blocking only.