Trebejo.Docker (Trebejo v2.0.0)

Copy Markdown View Source

Docker / Podman container lifecycle management.

Detects the available container runtime (Docker or Podman) and provides a unified interface for container and compose operations.

All command execution is routed through Trebejo.SafeCommand.execute/3 with arg lists — never interpolated into shell strings — to prevent shell injection. Runtime is auto-detected via Apero.Proc.command_exists?/1; environment variable CONTAINER_RUNTIME (docker | podman) overrides the detection.

Summary

Functions

Builds images.

Stops and removes services.

Executes a command in a compose service container.

Shows logs for compose services.

Lists running compose services.

Pulls images for compose services.

Restarts services.

Starts services defined in docker-compose.yml.

Executes a command inside a running container.

Returns true if running inside a container.

Lists networks.

Lists running containers.

Pulls a Docker image.

Removes a container.

Creates and starts a new container.

Detects the available container runtime.

Returns the runtime binary name as a string.

Starts an existing container.

Returns the state of a container.

Stops a running container.

Lists volumes.

Types

container_name()

@type container_name() :: String.t()

image()

@type image() :: String.t()

runtime()

@type runtime() :: :docker | :podman

Functions

compose_build(opts \\ [])

@spec compose_build(keyword()) :: {:ok, binary()} | {:error, binary()}

Builds images.

compose_down(opts \\ [])

@spec compose_down(keyword()) :: {:ok, binary()} | {:error, binary()}

Stops and removes services.

compose_exec(service, command, opts \\ [])

@spec compose_exec(binary(), [binary()], keyword()) ::
  {:ok, binary()} | {:error, binary()}

Executes a command in a compose service container.

compose_logs(opts \\ [])

@spec compose_logs(keyword()) :: {:ok, binary()} | {:error, binary()}

Shows logs for compose services.

compose_ps(opts \\ [])

@spec compose_ps(keyword()) :: {:ok, binary()} | {:error, binary()}

Lists running compose services.

compose_pull(opts \\ [])

@spec compose_pull(keyword()) :: {:ok, binary()} | {:error, binary()}

Pulls images for compose services.

compose_restart(opts \\ [])

@spec compose_restart(keyword()) :: {:ok, binary()} | {:error, binary()}

Restarts services.

compose_up(opts \\ [])

@spec compose_up(keyword()) :: {:ok, binary()} | {:error, binary()}

Starts services defined in docker-compose.yml.

exec(container, cmd, opts \\ [])

@spec exec(container_name(), [String.t()], keyword()) ::
  {:ok, String.t()} | {:error, String.t()}

Executes a command inside a running container.

in_container?()

@spec in_container?() :: boolean()

Returns true if running inside a container.

network_create(name, opts \\ [])

@spec network_create(
  binary(),
  keyword()
) :: {:ok, binary()} | {:error, binary()}

Creates a network.

network_list(opts \\ [])

@spec network_list(keyword()) :: {:ok, binary()} | {:error, binary()}

Lists networks.

network_remove(name, opts \\ [])

@spec network_remove(
  binary(),
  keyword()
) :: {:ok, binary()} | {:error, binary()}

Removes a network.

ps(opts \\ [])

@spec ps(keyword()) :: {:ok, String.t()} | {:error, String.t()}

Lists running containers.

pull(image)

@spec pull(image()) :: :ok | {:error, String.t()}

Pulls a Docker image.

rm(container, opts \\ [])

@spec rm(
  container_name(),
  keyword()
) :: :ok | {:error, String.t()}

Removes a container.

run(opts)

@spec run(keyword()) :: {:ok, container_name()} | {:error, String.t()}

Creates and starts a new container.

runtime()

@spec runtime() :: runtime() | :none

Detects the available container runtime.

runtime_binary()

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

Returns the runtime binary name as a string.

start(container)

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

Starts an existing container.

state(container)

@spec state(container_name()) :: :running | :stopped | :missing | {:error, String.t()}

Returns the state of a container.

stop(container, timeout \\ 10)

@spec stop(container_name(), pos_integer()) :: :ok | {:error, String.t()}

Stops a running container.

volume_create(name, opts \\ [])

@spec volume_create(
  binary(),
  keyword()
) :: {:ok, binary()} | {:error, binary()}

Creates a volume.

volume_list(opts \\ [])

@spec volume_list(keyword()) :: {:ok, binary()} | {:error, binary()}

Lists volumes.

volume_remove(name, opts \\ [])

@spec volume_remove(
  binary(),
  keyword()
) :: {:ok, binary()} | {:error, binary()}

Removes a volume.