TemporaryContainer Class

A temporary container created from a ContainerDefinition. Dispose the instance to remove the container (unless ContainerDefinition.ReuseId is set).

public class TemporaryContainer : IAsyncDisposable
Implements

Properties

NameDescription
DefinitionGets the definition owned by this container. It is read-only: the container reads it at every step of its life.
IdGets the container id.
NameGets the container name.
RuntimeGets the container runtime in use.

Gets the definition owned by this container. It is read-only: the container reads it at every step of its life.

public ContainerDefinition Definition { get; }

Property value

ContainerDefinition

Gets the container id.

public string Id { get; }

Property value

string

Exceptions

InvalidOperationException
The container has not been created yet.

Gets the container name.

public string Name { get; }

Property value

string

Exceptions

InvalidOperationException
The container has not been created yet.

Gets the container runtime in use.

public ContainerRuntime Runtime { get; }

Property value

ContainerRuntime

Methods

NameDescription
CopyFromContainerAsync(string, string, CancellationToken)Copies a file or directory from the container to the host.
CopyToContainerAsync(string, string, CancellationToken)Copies a file or directory from the host into the container.
DeleteAsync(CancellationToken)Removes the container, and the image built for it. A later TemporaryContainer.StartAsync or TemporaryContainer.EnsureCreatedAsync creates a new container.
DisposeAsync()Removes the container, and the image built for it, unless ContainerDefinition.ReuseId is set. Cleanup is best-effort, bounded in time, and never throws.
EnsureCreatedAsync(CancellationToken)Creates the container if it does not exist yet, without starting it. When ContainerDefinition.ReuseId is set, an existing matching container is adopted instead.
ExecAsync(Action<ExecOptions>, CancellationToken)Executes a command inside the running container.
ExistsAsync(CancellationToken)Determines whether the container still exists.
GetHostAddress(int)Gets the host address at which the specified published container port is reached from this machine.
GetLogsAsync(ContainerLogsOptions, CancellationToken)Streams the container logs selected by options.
GetLogsAsync(CancellationToken)Streams the container logs, from the creation of the container, following new lines until the container stops or the enumeration is cancelled.
GetMappedPort(int)Gets the host port mapped to the specified container port.
InspectAsync(CancellationToken)Inspects the container and returns a snapshot of its current state.
KillAsync(CancellationToken)Sends a kill signal to the container.
OpenReadAsync(string, CancellationToken)Reads a file from the container.
PauseAsync(CancellationToken)Pauses the container.
RestartAsync(CancellationToken)Restarts the container and refreshes the published-port mapping.
StartAsync(CancellationToken)Creates the container if needed, starts it, refreshes the published-port mapping, and runs the registered wait strategies.
StopAsync(CancellationToken)Stops the container.
UnpauseAsync(CancellationToken)Resumes a paused container.
WaitUntilReadyAsync(CancellationToken)Runs the registered wait strategies, bounded by ContainerDefinition.StartupTimeout.
WriteFileAsync(string, Stream, CancellationToken)Writes a file to the container, creating or overwriting it.

CopyFromContainerAsync(string, string, CancellationToken)

TemporaryContainer.Files.cs

Copies a file or directory from the container to the host.

net10.0

public Task CopyFromContainerAsync(string source, string destination, CancellationToken cancellationToken = default)

net11.0

public Task CopyFromContainerAsync(string source, string destination, CancellationToken cancellationToken = default)

Parameters

source string
The source path inside the container.
destination string
The destination path on the host.
cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the copy finishes.

CopyToContainerAsync(string, string, CancellationToken)

TemporaryContainer.Files.cs

Copies a file or directory from the host into the container.

net10.0

public Task CopyToContainerAsync(string source, string destination, CancellationToken cancellationToken = default)

net11.0

public Task CopyToContainerAsync(string source, string destination, CancellationToken cancellationToken = default)

Parameters

source string
The source path on the host.
destination string
The destination path inside the container.
cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the copy finishes.

DeleteAsync(CancellationToken)

TemporaryContainer.Lifecycle.cs

Removes the container, and the image built for it. A later TemporaryContainer.StartAsync or TemporaryContainer.EnsureCreatedAsync creates a new container.

net10.0

public Task DeleteAsync(CancellationToken cancellationToken = default)

net11.0

public Task DeleteAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the container is removed.

DisposeAsync()

TemporaryContainer.cs

Removes the container, and the image built for it, unless ContainerDefinition.ReuseId is set. Cleanup is best-effort, bounded in time, and never throws.

net10.0

public ValueTask DisposeAsync()

net11.0

public ValueTask DisposeAsync()

Returns

ValueTask A task that completes once cleanup finishes.

EnsureCreatedAsync(CancellationToken)

TemporaryContainer.cs

Creates the container if it does not exist yet, without starting it. When ContainerDefinition.ReuseId is set, an existing matching container is adopted instead.

net10.0

public Task EnsureCreatedAsync(CancellationToken cancellationToken = default)

net11.0

public Task EnsureCreatedAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the container exists.

Exceptions

InvalidOperationException
The container found through ContainerDefinition.ReuseId was created from a different definition.

ExecAsync(Action<ExecOptions>, CancellationToken)

TemporaryContainer.Exec.cs

Executes a command inside the running container.

net10.0

public Task<ExecResult> ExecAsync(Action<ExecOptions> configure, CancellationToken cancellationToken = default)

net11.0

public Task<ExecResult> ExecAsync(Action<ExecOptions> configure, CancellationToken cancellationToken = default)

Parameters

configure Action<ExecOptions>
A callback used to configure the command and its options.
cancellationToken CancellationToken
A cancellation token.

Returns

Task<ExecResult> The exit code and captured output of the command. Call ExecResult.EnsureSuccess to throw when the command failed.

Exceptions

TimeoutException
The command did not exit within ExecOptions.Timeout.

ExistsAsync(CancellationToken)

TemporaryContainer.Lifecycle.cs

Determines whether the container still exists.

net10.0

public Task<bool> ExistsAsync(CancellationToken cancellationToken = default)

net11.0

public Task<bool> ExistsAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task<bool> true if the container exists; otherwise, false.

GetHostAddress(int)

TemporaryContainer.cs

Gets the host address at which the specified published container port is reached from this machine.

public string GetHostAddress(int containerPort)

Parameters

containerPort int
The container port.

Returns

string The host name or IP address to connect to, together with TemporaryContainer.GetMappedPort. An IPv6 address is returned without brackets.

  • A port bound to a specific address (ContainerPort.HostIp) is reached on that address.
  • A port bound to every interface is reached on the host of a daemon that runs on another machine (a tcp:// or ssh:// endpoint), and on the loopback address otherwise.

Exceptions

InvalidOperationException
The port mapping is not available (the container has not been started) or the port is not published.

GetLogsAsync overloads

GetLogsAsync(ContainerLogsOptions, CancellationToken)

TemporaryContainer.Logs.cs

Streams the container logs selected by options.

public IAsyncEnumerable<LogEntry> GetLogsAsync(ContainerLogsOptions options, CancellationToken cancellationToken = default)
Parameters
options ContainerLogsOptions
The entries to return: a time range, the last entries, the streams, and whether to follow the new entries.
cancellationToken CancellationToken
A cancellation token that stops following the logs.
Returns

IAsyncEnumerable<LogEntry> An asynchronous sequence of log entries.

Exceptions
ArgumentException
Both ContainerLogsOptions.IncludeStandardOutput and ContainerLogsOptions.IncludeStandardError are false.
NotSupportedException
ContainerLogsOptions.Since or ContainerLogsOptions.Until is set, and the runtime does not record timestamps.

GetLogsAsync(CancellationToken)

TemporaryContainer.Logs.cs

Streams the container logs, from the creation of the container, following new lines until the container stops or the enumeration is cancelled.

public IAsyncEnumerable<LogEntry> GetLogsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationToken CancellationToken
A cancellation token that stops following the logs.
Returns

IAsyncEnumerable<LogEntry> An asynchronous sequence of log entries.

GetMappedPort(int)

TemporaryContainer.cs

Gets the host port mapped to the specified container port.

public int GetMappedPort(int containerPort)

Parameters

containerPort int
The container port.

Returns

int The host port.

Exceptions

InvalidOperationException
The port mapping is not available (the container has not been started) or the port is not published.

InspectAsync(CancellationToken)

TemporaryContainer.Inspect.cs

Inspects the container and returns a snapshot of its current state.

net10.0

public Task<ContainerInfo> InspectAsync(CancellationToken cancellationToken = default)

net11.0

public Task<ContainerInfo> InspectAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task<ContainerInfo> The container information.

KillAsync(CancellationToken)

TemporaryContainer.Lifecycle.cs

Sends a kill signal to the container.

net10.0

public Task KillAsync(CancellationToken cancellationToken = default)

net11.0

public Task KillAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the container is killed.

OpenReadAsync(string, CancellationToken)

TemporaryContainer.Files.cs

Reads a file from the container.

net10.0

public Task<Stream> OpenReadAsync(string path, CancellationToken cancellationToken = default)

net11.0

public Task<Stream> OpenReadAsync(string path, CancellationToken cancellationToken = default)

Parameters

path string
The path of the file inside the container.
cancellationToken CancellationToken
A cancellation token.

Returns

Task<Stream> A readable stream positioned at the beginning of the file content. Dispose it once read.

Remarks

The file is not loaded in memory. The Docker Engine API runtime reads it from the daemon as the stream is read, so the stream cannot seek, but its Stream.Length is known; copy it to a MemoryStream when you need to seek. The CLI runtimes copy it to a temporary file first, deleted when the stream is disposed.

PauseAsync(CancellationToken)

TemporaryContainer.Lifecycle.cs

Pauses the container.

net10.0

public Task PauseAsync(CancellationToken cancellationToken = default)

net11.0

public Task PauseAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the container is paused.

Exceptions

NotSupportedException
The runtime does not support pausing containers.

RestartAsync(CancellationToken)

TemporaryContainer.Lifecycle.cs

Restarts the container and refreshes the published-port mapping.

net10.0

public Task RestartAsync(CancellationToken cancellationToken = default)

net11.0

public Task RestartAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the container has restarted.

StartAsync(CancellationToken)

TemporaryContainer.cs

Creates the container if needed, starts it, refreshes the published-port mapping, and runs the registered wait strategies.

net10.0

public Task StartAsync(CancellationToken cancellationToken = default)

net11.0

public Task StartAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the container is started and ready.

StopAsync(CancellationToken)

TemporaryContainer.Lifecycle.cs

Stops the container.

net10.0

public Task StopAsync(CancellationToken cancellationToken = default)

net11.0

public Task StopAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the container has stopped.

UnpauseAsync(CancellationToken)

TemporaryContainer.Lifecycle.cs

Resumes a paused container.

net10.0

public Task UnpauseAsync(CancellationToken cancellationToken = default)

net11.0

public Task UnpauseAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the container is resumed.

Exceptions

NotSupportedException
The runtime does not support pausing containers.

WaitUntilReadyAsync(CancellationToken)

TemporaryContainer.cs

Runs the registered wait strategies, bounded by ContainerDefinition.StartupTimeout.

net10.0

public Task WaitUntilReadyAsync(CancellationToken cancellationToken = default)

net11.0

public Task WaitUntilReadyAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once every wait strategy has completed.

Exceptions

TimeoutException
The container was not ready within ContainerDefinition.StartupTimeout.

WriteFileAsync(string, Stream, CancellationToken)

TemporaryContainer.Files.cs

Writes a file to the container, creating or overwriting it.

net10.0

public Task WriteFileAsync(string path, Stream content, CancellationToken cancellationToken = default)

net11.0

public Task WriteFileAsync(string path, Stream content, CancellationToken cancellationToken = default)

Parameters

path string
The destination path inside the container.
content Stream
The content to write. It is read from its current position to its end, and is not disposed.
cancellationToken CancellationToken
A cancellation token.

Returns

Task A task that completes once the file is written.

Remarks

The content is not loaded in memory. The Docker Engine API runtime sends a seekable stream as it reads it, and copies any other stream to a temporary file first, since the archive it sends declares the length of the file before its content.