TestcontainerEx.Container.Config (testcontainer_ex v0.8.0)

Copy Markdown View Source

Container configuration struct and builder functions.

This module is pure data — no side effects, no Docker API calls. Use TestcontainerEx.Container.Builder for protocol-based build pipelines.

Summary

Types

t()

Container configuration.

Functions

Formats a list of exposed ports into a human-readable string.

Sets the log consumer level for streaming container logs to Logger.

Sets a correlation / request ID for tracing this container start.

Types

t()

@type t() :: %TestcontainerEx.Container.Config{
  auth: String.t() | nil,
  auto_remove: boolean(),
  bind_mounts: [map()],
  bind_volumes: [map()],
  check_image: Regex.t() | nil,
  cmd: [String.t()] | nil,
  container_id: String.t() | nil,
  copy_to: [map()],
  environment: %{required(atom() | String.t()) => String.t()},
  exposed_ports: [{integer(), integer() | nil}],
  force_reuse: boolean(),
  hostname: String.t() | nil,
  image: String.t(),
  ip_address: String.t() | nil,
  labels: %{required(String.t()) => String.t()},
  log_consumer: Logger.level() | nil,
  name: String.t() | nil,
  network: String.t() | nil,
  network_mode: String.t() | nil,
  privileged: boolean(),
  pull_policy: TestcontainerEx.PullPolicy.t() | nil,
  request_id: String.t() | nil,
  reuse: boolean(),
  wait_strategies: [struct()]
}

Container configuration.

Fields:

  • :image — Docker image reference (e.g. "postgres:15-alpine").
  • :cmd — override the image's default command.
  • :environment — map of environment variables.
  • :auth — optional Base64-encoded auth token for private registries.
  • :exposed_ports — list of {container_port, host_port | nil} tuples.

  • :ip_address — resolved IP address of the running container.
  • :wait_strategies — list of strategies determining when the container is ready.
  • :privileged — run the container in privileged mode.
  • :bind_mounts — list of bind mount specifications.
  • :bind_volumes — list of named volume mount specifications.
  • :copy_to — list of files to copy into the container at startup.
  • :labels — map of Docker labels to attach to the container.
  • :auto_remove — automatically remove the container when it stops.
  • :container_id — ID of the running container (set after start).
  • :check_image — optional regex to validate the image name.
  • :network_mode — Docker network mode (e.g. "host", "bridge").
  • :network — network name to connect the container to.
  • :hostname — container hostname.
  • :name — optional stable container name.
  • :reuse — when true, an existing container with the same config may be reused.
  • :force_reuse — force reuse even if the container is stale.
  • :pull_policy — controls when the image is pulled; see TestcontainerEx.PullPolicy.
  • :log_consumer — optional Logger level to stream container logs to (:debug, :info, etc.).
  • :request_id — correlation ID for tracing a start_container call.

Functions

format_ports(exposed_ports)

@spec format_ports([{integer() | String.t(), integer() | nil}]) :: String.t()

Formats a list of exposed ports into a human-readable string.

Examples

iex> format_ports([{8080, 32768}, {9090, nil}])
"8080->32768, 9090->auto"

is_os(name)

(macro)

is_valid_image(check_image)

(macro)

mapped_port(container, port)

@spec mapped_port(t(), integer()) :: integer() | nil

new(image)

@spec new(String.t()) :: t()

os_type()

@spec os_type() :: :linux | :macos | :windows | :unknown

running_in_container?(dockerenv_path \\ "/.dockerenv", cgroup_path \\ "/proc/1/cgroup", k8s_secrets_path \\ "/var/run/secrets/kubernetes.io", containerenv_path \\ "/.containerenv")

@spec running_in_container?(String.t(), String.t(), String.t(), String.t()) ::
  boolean()

Returns true when running inside a container (Docker, Podman, Kubernetes).

Accepts optional overrides for the .dockerenv path, cgroup path, Kubernetes secrets path, and Podman containerenv path.

valid_image(config)

@spec valid_image(t()) :: {:ok, t()} | {:error, TestcontainerEx.Error.t()}

valid_image!(config)

@spec valid_image!(t()) :: t()

with_auth(config, username, password)

@spec with_auth(t(), String.t(), String.t()) :: t()

with_auto_remove(config, auto_remove)

@spec with_auto_remove(t(), boolean()) :: t()

with_bind_mount(config, host_src, container_dest, options \\ "ro")

@spec with_bind_mount(t(), String.t(), String.t(), String.t()) :: t()

with_bind_volume(config, volume, container_dest, read_only \\ false)

@spec with_bind_volume(t(), String.t(), String.t(), boolean()) :: t()

with_check_image(config, check_image)

@spec with_check_image(t(), String.t() | Regex.t()) :: t()

with_cmd(config, cmd)

@spec with_cmd(t(), [String.t()]) :: t()

with_copy_to(config, target, source)

@spec with_copy_to(t(), String.t(), String.t()) :: t()

with_environment(config, key, value)

@spec with_environment(t(), atom() | String.t(), String.t()) :: t()

with_exposed_port(config, port)

@spec with_exposed_port(t(), integer()) :: t()

with_exposed_ports(config, ports)

@spec with_exposed_ports(t(), [integer()]) :: t()

with_fixed_port(config, port, host_port \\ nil)

@spec with_fixed_port(t(), integer(), integer() | nil) :: t()

with_force_reuse(config, force_reuse)

@spec with_force_reuse(t(), boolean()) :: t()

with_hostname(config, hostname)

@spec with_hostname(t(), String.t()) :: t()

with_label(config, key, value)

@spec with_label(t(), String.t(), String.t()) :: t()

with_log_consumer(config, level)

@spec with_log_consumer(t(), Logger.level()) :: t()

Sets the log consumer level for streaming container logs to Logger.

When set, container stdout/stderr will be piped through Logger at the specified level after the container starts.

with_name(config, name)

@spec with_name(t(), String.t()) :: t()

with_network(config, network_name)

@spec with_network(t(), String.t()) :: t()

with_network_mode(config, mode)

@spec with_network_mode(t(), String.t()) :: t()

with_privileged(config, privileged)

@spec with_privileged(t(), boolean()) :: t()

with_pull_policy(config, policy)

@spec with_pull_policy(
  t(),
  struct()
) :: t()

with_request_id(config, request_id)

@spec with_request_id(t(), String.t()) :: t()

Sets a correlation / request ID for tracing this container start.

If not set, one will be generated automatically by start_container/1.

with_reuse(config, reuse)

@spec with_reuse(t(), boolean()) :: t()

with_waiting_strategies(config, wait_fns)

@spec with_waiting_strategies(t(), [struct()]) :: t()

with_waiting_strategy(config, wait_fn)

@spec with_waiting_strategy(
  t(),
  struct()
) :: t()