testcontainers_gleam/container
Docker container configuration and builder API.
Use new to create a container definition, then chain with_* functions
to configure it before passing it to testcontainers_gleam.start_container.
Example
import testcontainers_gleam
import testcontainers_gleam/container
let c =
container.new("redis:7.4-alpine")
|> container.with_exposed_port(6379)
|> container.with_environment("REDIS_PASSWORD", "secret")
let assert Ok(running) = testcontainers_gleam.start_container(c)
Types
External type wrapping the Elixir %Testcontainers.Container{} struct.
Built with new and configured with with_* functions.
After starting, use container_id and mapped_port to query the
running container.
pub type Container
Image pull policy for a container.
pub type PullPolicy {
AlwaysPull
NeverPull
}
Constructors
-
AlwaysPullPull the image every time, even if it exists locally.
-
NeverPullNever pull the image; fail if it is not available locally.
Values
pub fn container_id(container: Container) -> String
Get the container ID of a running container.
Only meaningful after the container has been started.
Panics
Panics if called on a container that has not been started.
pub fn mapped_port(
container: Container,
port: Int,
) -> Result(Int, Nil)
Get the host port mapped to an exposed container port.
Returns Ok(port) if the mapping exists, or Error(Nil) if the
port was not exposed or has not been mapped yet.
pub fn new(image: String) -> Container
Create a new container definition for the given Docker image.
The returned container has default configuration; use the with_*
functions to customize it before starting.
Example
container.new("postgres:16-alpine")
pub fn with_auth(
container: Container,
username: String,
password: String,
) -> Container
Set registry authentication credentials for pulling the image.
pub fn with_auto_remove(
container: Container,
auto_remove: Bool,
) -> Container
Set whether Docker should automatically remove the container when it exits.
pub fn with_bind_mount(
container: Container,
host_src: String,
dest: String,
options: String,
) -> Container
Mount a host path into the container.
The options string controls mount flags (e.g. "ro" or "rw").
pub fn with_bind_volume(
container: Container,
volume: String,
dest: String,
read_only: Bool,
) -> Container
Mount a named Docker volume into the container.
pub fn with_check_image(
container: Container,
pattern: String,
) -> Container
Set a check image pattern to verify the image name.
pub fn with_cmd(
container: Container,
cmd: List(String),
) -> Container
Set the command to run inside the container.
pub fn with_environment(
container: Container,
key: String,
value: String,
) -> Container
Set an environment variable on the container.
pub fn with_exposed_port(
container: Container,
port: Int,
) -> Container
Expose a single port from the container.
The port will be mapped to a random host port. Use mapped_port
after starting to get the assigned host port.
pub fn with_exposed_ports(
container: Container,
ports: List(Int),
) -> Container
Expose multiple ports from the container.
pub fn with_fixed_port(
container: Container,
port: Int,
host_port: Int,
) -> Container
Expose a container port and bind it to a fixed host_port.
pub fn with_label(
container: Container,
key: String,
value: String,
) -> Container
Add a label to the container.
pub fn with_network_mode(
container: Container,
mode: String,
) -> Container
Set the Docker network mode (e.g. "bridge", "host", "none").
pub fn with_pull_policy(
container: Container,
policy: PullPolicy,
) -> Container
Set the image pull policy. Defaults to AlwaysPull.
pub fn with_reuse(container: Container, reuse: Bool) -> Container
Enable or disable container reuse across test runs.
pub fn with_waiting_strategy(
container: Container,
strategy: wait_strategy.WaitStrategy,
) -> Container
Add a wait strategy to determine when the container is ready.
See testcontainers_gleam/wait_strategy for available strategies.