The main entry point into Testcontainers.
This is a GenServer that needs to be started before anything can happen.
Summary
Functions
Returns a specification to start this module under a supervisor.
Creates a Docker network.
Removes a Docker network.
Starts the Testcontainers application.
Starts a new container based on the provided configuration, applying any specified wait strategies.
Starts the Testcontainers application.
Stops a running container.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
Creates a Docker network.
Networks allow containers to communicate with each other using hostnames.
Use Container.with_network/2 to attach a container to a network.
Parameters
network_name: The name of the network to create.name: The name of the Testcontainers GenServer (defaults toTestcontainers).
Returns
{:ok, network_id}if the network is created successfully.{:ok, :already_exists}if the network already exists.{:error, reason}on failure.
Removes a Docker network.
Parameters
network_name: The name of the network to remove.name: The name of the Testcontainers GenServer (defaults toTestcontainers).
Returns
:okif the network is removed successfully.{:error, reason}on failure.
Starts the Testcontainers application.
This will NOT terminate when the calling process exits, for ex a task.
Starts a new container based on the provided configuration, applying any specified wait strategies.
This function performs several steps:
- Pulls the necessary Docker image.
- Creates and starts a container with the specified configuration.
- Registers the container with a reaper process for automatic cleanup, ensuring it is stopped and removed when the current process exits or in case of unforeseen failures.
Parameters
config: A%Container{}struct containing the configuration settings for the container, such as the image to use, environment variables, bound ports, and volume bindings.
Examples
iex> config = Testcontainers.MySqlContainer.new()
iex> {:ok, container} = Testcontainers.start_container(config)Returns
{:ok, container}if the container is successfully created, started, and passes all wait strategies.- An error tuple, such as
{:error, reason}, if there is a failure at any step in the process.
Notes
- The container is automatically registered with a reaper process, ensuring it is stopped and removed when the current process exits, or in the case of unforeseen failures.
- It's important to specify appropriate wait strategies to ensure the container is fully ready for interaction, especially for containers that may take some time to start up services internally.
Starts the Testcontainers application.
This will terminate when the calling process exits, for ex a task.
Stops a running container.
This sends a stop command to the specified container. The Docker daemon terminates the container process gracefully.
Parameters
container_id: The ID of the container to stop, as a string.
Returns
:okif the container stops successfully.{:error, reason}on failure.
Examples
:ok = Testcontainers.Connection.stop_container("my_container_id")