DockerContainer is the main class for managing Docker containers in tests.
It provides a fluent API to configure, start, stop, and interact with containers.
Example:
container = Testcontainers::DockerContainer.new("redis:latest")
.with_exposed_port(6379)
.with_env("REDIS_PASSWORD", "secret")
container.start
host = container.host
port = container.mapped_port(6379)
# ... run tests ...
container.stop
container.remove
Constructors
Initializes a new DockerContainer.
- image: The Docker image to use (e.g. "redis:latest")
SourceInstance methods
container_id
The container ID once created
Sourcedead?
Returns whether the container is dead.
SourceExecutes a command in the container.
Returns the output from the command.
Sourceexists?
Returns whether the container exists.
Sourceexited?
Returns whether the container is stopped/exited.
Sourcefirst_mapped_port
Returns the first mapped port.
SourceReturns an environment variable value from the container config.
Sourcehealthy?
Returns whether the container is healthy.
Sourcehost
Returns the container's host address.
Sourceinfo
Returns the container's full inspect data.
Sourcekill(signal :
String = "
SIGKILL") : self
Kills the container with the specified signal.
SourceReturns the container's logs.
SourceReturns the mapped host port for the given container port.
Sourcename
Container configuration properties
SourceContainer configuration properties
Sourcepaused?
Returns whether the container is paused.
Sourceremove(force :
Bool = false, volumes :
Bool = false) : self
Removes/deletes the container.
Sourcerestarting?
Returns whether the container is restarting.
Sourcerunning?
Returns whether the container is running.
Sourcestart
Starts the container.
This will:
- Pull the image if not present
- Create the container
- Start the container
- Execute the wait strategy (if any)
Returns self for method chaining.
Sourcestatus
Returns the container's status string.
Possible values: "created", "running", "paused", "restarting", "removing", "exited", "dead"
Sourcestop!
Stops the container forcefully.
Sourcesupports_healthcheck?
Returns whether the container supports healthchecks.
Sourceuse
Starts the container, yields it to a block, then stops and removes it.
Sourcewait_for
Wait strategy: a Proc that receives the container and waits for readiness
Sourcewait_for=(wait_for :
Proc(
DockerContainer,
Nil) |
Nil)
Wait strategy: a Proc that receives the container and waits for readiness
SourceWaits for the container to be healthy.
SourceWaits for an HTTP endpoint to respond with the expected status code.
SourceWaits for the container's logs to match the given regex.
SourceWaits for a TCP port to be open.
Sourcewait_for_user_defined?
Returns whether the wait strategy was explicitly set by the user.
SourceSets the command to run in the container from an array.
Sourcewith_command(*parts :
String) : self
Sets the command to run in the container.
SourceSets the entrypoint for the container from an array.
Sourcewith_entrypoint(*parts :
String) : self
Sets the entrypoint for the container.
SourceSets a single environment variable.
SourceSets environment variables from a Hash.
SourceSets environment variables from an array of "KEY=VALUE" strings.
SourceAdds a single exposed port to the container.
The port will be mapped to a random host port.
SourceAdds multiple exposed ports from an array.
SourceAdds multiple exposed ports to the container.
Sourcewith_filesystem_bind(host_path :
String, container_path :
String, mode :
String = "rw") : self
Adds a filesystem bind mount.
SourceAdds multiple filesystem binds.
Sourcewith_fixed_exposed_port(container_port :
Int32 |
String, host_port :
Int32) : self
Adds a fixed port mapping (container_port -> host_port).
SourceConfigures a healthcheck for the container.
Options:
- test: Command to run (string or array)
- interval: Seconds between checks (default: 30)
- timeout: Seconds before check is considered hung (default: 30)
- retries: Number of retries before unhealthy (default: 3)
- shell: Whether to use CMD-SHELL (default: false)
SourceAdds labels to the container.
Sourcewith_wait_for
Sets a custom wait strategy block.
The block receives the container instance and should block until the container is ready.
Sourcewith_wait_for_healthcheck(timeout :
Int32 = 60, interval :
Float64 = 0.5) : self
Sets the wait strategy to wait for the healthcheck.
Sourcewith_wait_for_http(path :
String = "/", container_port :
Int32 = 80, timeout :
Int32 = 60, interval :
Float64 = 0.5, status :
Int32 = 200, https :
Bool = false) : self
Sets the wait strategy to wait for an HTTP endpoint.
Sourcewith_wait_for_logs(matcher :
Regex, timeout :
Int32 = 60, interval :
Float64 = 0.5) : self
Sets the wait strategy to wait for a specific log message.
Sourcewith_wait_for_tcp_port(port :
Int32, timeout :
Int32 = 60, interval :
Float64 = 0.5) : self
Sets the wait strategy to wait for a TCP port.
Sourcewith_working_dir(working_dir :
String) : self
Sets the working directory inside the container.
Source