Skip to main content

GenericBuildableImage

Struct GenericBuildableImage 

Source
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

Source

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”)
Source

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
Source

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
Source

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 filesystem
  • target - Path where the file should be placed in the build context
Source

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 bytes
  • target - Path where the file should be placed in the build context

Trait Implementations§

Source§

impl BuildableImage for GenericBuildableImage

Source§

type Built = GenericImage

The type of Image that this buildable image produces after building.
Source§

fn build_context(&self) -> CopyToContainerCollection

Returns the build context containing all files and data needed to build the image. Read more
Source§

fn descriptor(&self) -> String

Returns the image descriptor (name:tag) that will be assigned to the built image and be passed down to the container for running. Read more
Source§

fn into_image(self) -> Self::Built

Consumes this buildable image and converts it into a runnable Image. Read more
Source§

impl Debug for GenericBuildableImage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsyncBuilder<T> for T
where T: BuildableImage + Send,

Source§

fn build_image<'async_trait>( self, ) -> Pin<Box<dyn Future<Output = Result<<T as BuildableImage>::Built, TestcontainersError>> + Send + 'async_trait>>
where T: 'async_trait,

Source§

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> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> IntoResult<T> for T

Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> SyncBuilder<T> for T
where T: BuildableImage + Send,

Source§

fn build_image( self, ) -> Result<<T as BuildableImage>::Built, TestcontainersError>

Available on crate feature blocking only.
Source§

fn build_image_with( self, options: BuildImageOptions, ) -> Result<<T as BuildableImage>::Built, TestcontainersError>

Available on crate feature blocking only.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more