TestcontainerEx.Container.Lifecycle (testcontainer_ex v0.6.0)

Copy Markdown View Source

Orchestrates container lifecycle: create, start, wait, pull, copy.

This module is the bridge between the GenServer state and the Docker API. All functions require a connection and return {:ok, container} or {:error, reason}.

Summary

Functions

Executes a command inside a container.

Inspects a container by ID.

Monitors a container until a predicate succeeds or the timeout elapses.

Creates and starts a container, applying wait strategies.

Creates and starts multiple containers.

Stops a container by ID.

Stops multiple containers.

Functions

container_logs(container_id, conn, opts \\ [])

@spec container_logs(String.t(), Req.Request.t(), keyword()) ::
  {:ok, map()} | {:ok, reference() | pid()} | {:error, term()}

Fetches container logs.

exec(container_id, command, conn)

@spec exec(String.t(), [String.t()], Req.Request.t()) ::
  {:ok, String.t()} | {:error, term()}

Executes a command inside a container.

inspect_container(container_id, conn)

@spec inspect_container(String.t(), Req.Request.t()) ::
  {:ok, TestcontainerEx.Container.Config.t()} | {:error, term()}

Inspects a container by ID.

monitor_container(container_id, predicate, conn, opts \\ [])

@spec monitor_container(
  String.t(),
  (TestcontainerEx.Container.Config.t() -> {:ok, term()} | {:error, term()}),
  Req.Request.t(),
  keyword()
) :: {:ok, term()} | {:error, term()}

Monitors a container until a predicate succeeds or the timeout elapses.

resolve_pull_policy(config, properties)

start_container(builder, conn, state)

@spec start_container(struct(), Req.Request.t(), map()) ::
  {:ok, TestcontainerEx.Container.Config.t()} | {:error, term()}

Creates and starts a container, applying wait strategies.

Steps:

  1. Build config with labels (via BuilderHelper)
  2. Check for reusable container or create new one
  3. Pull image if needed
  4. Copy files into container
  5. Start container
  6. Apply wait strategies
  7. Call after_start hook

start_containers(builders, conn, state)

@spec start_containers([struct()], Req.Request.t(), map()) ::
  {:ok, [TestcontainerEx.Container.Config.t()]} | {:error, [term()]}

Creates and starts multiple containers.

Returns {:ok, containers} when every container starts. If one or more containers fail, returns {:error, results} where results keeps the input order and contains either {:ok, container} or {:error, reason}.

stop_container(container_id, conn)

@spec stop_container(String.t(), Req.Request.t()) :: :ok | {:error, term()}

Stops a container by ID.

stop_containers(container_ids, conn)

@spec stop_containers([String.t()], Req.Request.t()) ::
  {:ok, [term()]} | {:error, [term()]}

Stops multiple containers.