TestcontainerEx.Engine.Control (testcontainer_ex v0.7.1)

Copy Markdown View Source

Low-level container control operations via the Docker Engine API.

These functions talk directly to the Docker Engine API and work with raw container IDs. They are useful for fine-grained control beyond what the high-level TestcontainerEx API provides.

All functions accept an optional base_url parameter. When omitted, the URL is derived from CONTAINER_ENGINE_HOST, or defaults to http://d.

Usage

# Pause a container
:ok = TestcontainerEx.Engine.Control.pause("abc123")

# Get live resource stats
stats = TestcontainerEx.Engine.Control.stats("abc123")

# Upload a file
:ok = TestcontainerEx.Engine.Control.upload("abc123", "/app/config.yml", "config.yml")

# Download a file
{:ok, data} = TestcontainerEx.Engine.Control.download("abc123", "/app/data.json")

# Commit container to a new image
{:ok, image_id} = TestcontainerEx.Engine.Control.commit("abc123", "my-snapshot:v1")

Summary

Functions

Attaches to a container's streams.

Creates a container from a config map. Returns {:ok, container_id}.

Creates an exec instance inside a container.

Creates a container with a specific name.

Downloads a file from a container as binary data.

Downloads and extracts a single file from a container archive.

Exports a container filesystem as a tarball.

Returns detailed container inspection.

Inspects an exec instance.

Kills a running container with the given signal (default: SIGKILL).

Pauses a running container.

Removes a container. Options: :force, :remove_volumes.

Restarts a running container. Optional t parameter for timeout in seconds.

Returns whether the container is running.

Starts a stopped container.

Starts a previously created exec instance.

Returns container state (running, paused, etc.).

Returns live resource usage statistics.

Stops a running container. Optional t parameter for timeout in seconds.

Returns running processes inside the container.

Unpauses a paused container.

Updates container resources (memory, CPU, etc.).

Waits for a container to finish and returns its exit code.

Functions

attach(container_id, opts \\ [], base_url \\ nil)

@spec attach(String.t(), keyword(), String.t() | nil) ::
  {:ok, binary() | reference()} | {:error, term()}

Attaches to a container's streams.

Returns {:ok, reference()} for streaming mode.

commit(container_id, repo_tag, opts \\ [], base_url \\ nil)

@spec commit(String.t(), String.t(), keyword(), String.t() | nil) ::
  {:ok, String.t()} | {:error, term()}

Commits a container to a new image.

create(config, base_url \\ nil)

@spec create(map(), String.t() | nil) :: {:ok, String.t()} | {:error, term()}

Creates a container from a config map. Returns {:ok, container_id}.

create_exec(container_id, command, opts \\ [], base_url \\ nil)

@spec create_exec(String.t(), [String.t()], keyword(), String.t() | nil) ::
  {:ok, String.t()} | {:error, term()}

Creates an exec instance inside a container.

create_named(name, config, base_url \\ nil)

@spec create_named(String.t(), map(), String.t() | nil) ::
  {:ok, String.t()} | {:error, term()}

Creates a container with a specific name.

download(container_id, container_path, base_url \\ nil)

@spec download(String.t(), String.t(), String.t() | nil) ::
  {:ok, binary()} | {:error, term()}

Downloads a file from a container as binary data.

download_file(container_id, container_path, base_url \\ nil)

@spec download_file(String.t(), String.t(), String.t() | nil) ::
  {:ok, binary()} | {:error, term()}

Downloads and extracts a single file from a container archive.

export(container_id, base_url \\ nil)

@spec export(String.t(), String.t() | nil) :: {:ok, binary()} | {:error, term()}

Exports a container filesystem as a tarball.

inspect_container(container_id, base_url \\ nil)

@spec inspect_container(String.t(), String.t() | nil) ::
  {:ok, map()} | {:error, term()}

Returns detailed container inspection.

inspect_exec(exec_id, base_url \\ nil)

@spec inspect_exec(String.t(), String.t() | nil) :: {:ok, map()} | {:error, term()}

Inspects an exec instance.

kill(container_id, signal \\ "SIGKILL", base_url \\ nil)

@spec kill(String.t(), String.t(), String.t() | nil) :: :ok | {:error, term()}

Kills a running container with the given signal (default: SIGKILL).

logs(container_id, opts \\ [], base_url \\ nil)

@spec logs(String.t(), keyword(), String.t() | nil) ::
  {:ok, String.t()} | {:error, term()}

Fetches container logs.

Options:

  • :stdout — include stdout (default: true)
  • :stderr — include stderr (default: true)
  • :timestamps — include timestamps (default: false)
  • :follow — stream logs (default: false)
  • :tail — number of lines from end (default: "all")
  • :since — UNIX timestamp for logs since
  • :until — UNIX timestamp for logs until

pause(container_id, base_url \\ nil)

@spec pause(String.t(), String.t() | nil) :: :ok | {:error, term()}

Pauses a running container.

remove(container_id, opts \\ [], base_url \\ nil)

@spec remove(String.t(), keyword(), String.t() | nil) :: :ok | {:error, term()}

Removes a container. Options: :force, :remove_volumes.

rename(container_id, new_name, base_url \\ nil)

@spec rename(String.t(), String.t(), String.t() | nil) :: :ok | {:error, term()}

Renames a container.

resize(container_id, width, height, base_url \\ nil)

@spec resize(String.t(), integer(), integer(), String.t() | nil) ::
  :ok | {:error, term()}

Resizes a container TTY.

resize_exec(exec_id, width, height, base_url \\ nil)

@spec resize_exec(String.t(), integer(), integer(), String.t() | nil) ::
  :ok | {:error, term()}

Resizes an exec TTY.

restart(container_id, timeout \\ nil, base_url \\ nil)

@spec restart(String.t(), integer() | nil, String.t() | nil) :: :ok | {:error, term()}

Restarts a running container. Optional t parameter for timeout in seconds.

running?(container_id, base_url \\ "")

@spec running?(String.t(), String.t() | nil) :: boolean()

Returns whether the container is running.

start(container_id, base_url \\ nil)

@spec start(String.t(), String.t() | nil) :: :ok | {:error, term()}

Starts a stopped container.

start_exec(exec_id, opts \\ [], base_url \\ nil)

@spec start_exec(String.t(), keyword(), String.t() | nil) ::
  {:ok, String.t()} | {:error, term()}

Starts a previously created exec instance.

state(container_id, base_url \\ "")

@spec state(String.t(), String.t() | nil) :: {:ok, map()} | {:error, term()}

Returns container state (running, paused, etc.).

stats(container_id, opts \\ [], base_url \\ nil)

@spec stats(String.t(), keyword(), String.t() | nil) ::
  {:ok, map()} | {:error, term()}

Returns live resource usage statistics.

Options:

  • :stream — stream stats (default: true)

stop(container_id, timeout \\ nil, base_url \\ nil)

@spec stop(String.t(), integer() | nil, String.t() | nil) :: :ok | {:error, term()}

Stops a running container. Optional t parameter for timeout in seconds.

top(container_id, ps_args \\ "-ef", base_url \\ nil)

Returns running processes inside the container.

unpause(container_id, base_url \\ nil)

@spec unpause(String.t(), String.t() | nil) :: :ok | {:error, term()}

Unpauses a paused container.

update(container_id, opts \\ [], base_url \\ nil)

@spec update(String.t(), keyword(), String.t() | nil) :: :ok | {:error, term()}

Updates container resources (memory, CPU, etc.).

upload(container_id, container_path, source, base_url \\ nil)

@spec upload(String.t(), String.t(), String.t() | binary(), String.t() | nil) ::
  :ok | {:error, term()}

Uploads a file to a container.

The source can be:

  • A file path (string) — the file is read and uploaded
  • A binary — uploaded directly as the file contents

wait(container_id, base_url \\ nil)

@spec wait(String.t(), String.t() | nil) :: {:ok, integer()} | {:error, term()}

Waits for a container to finish and returns its exit code.