ContainerDefinition Class

Describes how a container should be created. Configure an instance and call ContainerDefinition.CreateContainer to obtain a runnable TemporaryContainer.

public class ContainerDefinition

Example

var definition = new ContainerDefinition(ImageSource.FromRegistry("redis:8"));
definition.Environment.Add("TZ", "UTC");
definition.Ports.Add(new ContainerPort(6379));
definition.WaitStrategies.Add(Wait.ForPort(6379));

await using var container = definition.CreateContainer();
await container.StartAsync();

Constructors

NameDescription
ContainerDefinition(ContainerDefinition)Initializes a new instance of the ContainerDefinition class by deep-copying another definition. The copy can be changed even when other belongs to a container.
ContainerDefinition(ImageSource)Initializes a new instance of the ContainerDefinition class.

ContainerDefinition overloads

ContainerDefinition(ContainerDefinition)

ContainerDefinition.cs

Initializes a new instance of the ContainerDefinition class by deep-copying another definition. The copy can be changed even when other belongs to a container.

public ContainerDefinition(ContainerDefinition other)
Parameters
other ContainerDefinition
The definition to copy.

ContainerDefinition(ImageSource)

ContainerDefinition.cs

Initializes a new instance of the ContainerDefinition class.

public ContainerDefinition(ImageSource image)
Parameters
image ImageSource
The image the container is created from.

Properties

NameDescription
AdditionalCreateArgumentsGets the arguments added to the create command of the CLI runtimes (docker, podman, Apple's container, wslc), right before the image. Use it for an option the definition does not have.
CapAddGets the Linux capabilities added to the container, such as NET_ADMIN.
CapDropGets the Linux capabilities dropped from the container, such as ALL.
CommandGets the command tokens passed to the container.
DockerApiCreateRequestCustomizerGets or sets a callback that changes the JSON body of the POST /containers/create request the Docker Engine API runtime sends, right before it is sent. Use it for an option the definition does not have.
EntrypointGets the entrypoint override tokens.
EnvironmentGets the environment variables.
ExposedPortsGets the ports the container exposes without publishing them to the host. See ContainerDefinition.Ports for the published ports.
ExtraHostsGets the entries added to the /etc/hosts file of the container. Use ContainerExtraHost.ForHostGateway to reach the host from the container on Linux.
HealthcheckGets or sets the healthcheck that replaces the one the image declares. null keeps the healthcheck of the image; ContainerHealthcheck.Disabled disables it.
HostnameGets or sets the container hostname.
ImageGets or sets the image the container is created from.
IsReadOnlyGets a value indicating whether the definition belongs to a container, in which case it can no longer be changed.
LabelsGets the labels.
LoggingGets the logging options.
MountsGets the mounts.
NameGets or sets the container name. When null, the container is named after ContainerDefinition.ReuseId when it is set, and gets a random name otherwise.
NetworkGets the network options.
PlatformGets or sets the platform of the image, such as linux/amd64 or linux/arm64. It selects the variant of a multi-platform image that is pulled and run, and the platform a Dockerfile is built for. When null, the runtime picks the platform of the machine.
PortsGets the published ports.
PrivilegedGets or sets a value indicating whether the container runs in privileged mode, with every capability and access to the devices of the host.
PullPolicyGets or sets the image pull policy.
ResourcesGets the resource limits.
ReuseIdGets or sets an identifier used to reuse an existing container across runs. When set, the container is not removed on dispose.
RuntimeGets or sets the container runtime to use.
StartupTimeoutGets or sets the maximum time TemporaryContainer.WaitUntilReadyAsync waits for all wait strategies to complete.
StopSignalGets or sets the signal sent to the container to stop it, such as SIGINT. null keeps the one of the image, or SIGTERM.
StopTimeoutGets or sets the time the runtime waits for the container to stop before killing it. null keeps the default of the runtime (10 seconds for docker). The value is rounded down to whole seconds.
UserGets or sets the user the container runs as.
WaitStrategiesGets the wait strategies run when the container starts.
WorkingDirectoryGets or sets the working directory inside the container.

AdditionalCreateArguments

ContainerDefinition.cs

Gets the arguments added to the create command of the CLI runtimes (docker, podman, Apple's container, wslc), right before the image. Use it for an option the definition does not have.

public IList<string> AdditionalCreateArguments { get; }

Property value

IList<string>

Remarks

The Docker Engine API runtime ignores these arguments; use ContainerDefinition.DockerApiCreateRequestCustomizer instead. The library does not check them: an argument the runtime does not know makes the creation fail.

Gets the Linux capabilities added to the container, such as NET_ADMIN.

public IList<string> CapAdd { get; }

Property value

IList<string>

Gets the Linux capabilities dropped from the container, such as ALL.

public IList<string> CapDrop { get; }

Property value

IList<string>

Gets the command tokens passed to the container.

public ContainerCommandCollection Command { get; }

DockerApiCreateRequestCustomizer

ContainerDefinition.cs

Gets or sets a callback that changes the JSON body of the POST /containers/create request the Docker Engine API runtime sends, right before it is sent. Use it for an option the definition does not have.

public Action<JsonObject>? DockerApiCreateRequestCustomizer { get; set; }

Property value

Action<JsonObject>?

Remarks

The CLI runtimes ignore the callback; use ContainerDefinition.AdditionalCreateArguments instead.

The callback is not part of the configuration a container reused through ContainerDefinition.ReuseId is matched against, since what it does cannot be compared: change the ContainerDefinition.ReuseId when you change what it does.

Gets the entrypoint override tokens.

public ContainerCommandCollection Entrypoint { get; }

Gets the environment variables.

public ContainerEnvironmentCollection Environment { get; }

Gets the ports the container exposes without publishing them to the host. See ContainerDefinition.Ports for the published ports.

public IList<ContainerExposedPort> ExposedPorts { get; }

Property value

IList<ContainerExposedPort>

Remarks

Apple's container runtime does not support exposing a port.

Gets the entries added to the /etc/hosts file of the container. Use ContainerExtraHost.ForHostGateway to reach the host from the container on Linux.

public IList<ContainerExtraHost> ExtraHosts { get; }

Property value

IList<ContainerExtraHost>

Remarks

Apple's container runtime does not support extra hosts.

Gets or sets the healthcheck that replaces the one the image declares. null keeps the healthcheck of the image; ContainerHealthcheck.Disabled disables it.

public ContainerHealthcheck? Healthcheck { get; set; }

Property value

ContainerHealthcheck?

Remarks

Apple's container runtime has no healthcheck.

Gets or sets the container hostname.

public string? Hostname { get; set; }

Property value

string?

Gets or sets the image the container is created from.

public ImageSource Image { get; set; }

Property value

ImageSource

Gets a value indicating whether the definition belongs to a container, in which case it can no longer be changed.

public bool IsReadOnly { get; }

Property value

bool

Gets the labels.

public ContainerLabelCollection Labels { get; }

Gets the logging options.

public ContainerLoggingOptions Logging { get; }

Gets the mounts.

public ContainerMountCollection Mounts { get; }

Gets or sets the container name. When null, the container is named after ContainerDefinition.ReuseId when it is set, and gets a random name otherwise.

public string? Name { get; set; }

Property value

string?

Gets the network options.

public ContainerNetworkOptions Network { get; }

Gets or sets the platform of the image, such as linux/amd64 or linux/arm64. It selects the variant of a multi-platform image that is pulled and run, and the platform a Dockerfile is built for. When null, the runtime picks the platform of the machine.

public string? Platform { get; set; }

Property value

string?

Remarks

Running an image of another architecture requires emulation, such as Rosetta on Apple Silicon or QEMU on Linux.

Gets the published ports.

public ContainerPortCollection Ports { get; }

Gets or sets a value indicating whether the container runs in privileged mode, with every capability and access to the devices of the host.

public bool Privileged { get; set; }

Property value

bool

Remarks

Apple's container runtime does not support privileged containers.

Gets or sets the image pull policy.

public PullPolicy PullPolicy { get; set; }

Property value

PullPolicy

Gets the resource limits.

public ContainerResourceOptions Resources { get; }

Gets or sets an identifier used to reuse an existing container across runs. When set, the container is not removed on dispose.

public string? ReuseId { get; set; }

Property value

string?

Remarks

A container is only adopted when it was created from the same definition. The credentials generated by the database helpers are the exception: every process generates its own, so the ones the container was created with are read back from it.

Gets or sets the container runtime to use.

public ContainerRuntime Runtime { get; set; }

Property value

ContainerRuntime

Gets or sets the maximum time TemporaryContainer.WaitUntilReadyAsync waits for all wait strategies to complete.

public TimeSpan StartupTimeout { get; set; }

Property value

TimeSpan

Gets or sets the signal sent to the container to stop it, such as SIGINT. null keeps the one of the image, or SIGTERM.

public string? StopSignal { get; set; }

Property value

string?

Remarks

Apple's container runtime does not support it.

Gets or sets the time the runtime waits for the container to stop before killing it. null keeps the default of the runtime (10 seconds for docker). The value is rounded down to whole seconds.

public TimeSpan? StopTimeout { get; set; }

Property value

TimeSpan?

Remarks

Apple's container runtime does not support it.

Gets or sets the user the container runs as.

public string? User { get; set; }

Property value

string?

Gets the wait strategies run when the container starts.

public ContainerWaitStrategyCollection WaitStrategies { get; }

WorkingDirectory

ContainerDefinition.cs

Gets or sets the working directory inside the container.

public string? WorkingDirectory { get; set; }

Property value

string?

Methods

NameDescription
CreateContainer()Creates a TemporaryContainer from a deep copy of this definition. Later changes to this definition do not affect the returned container, and the copy the container owns cannot be changed.

CreateContainer()

ContainerDefinition.cs

Creates a TemporaryContainer from a deep copy of this definition. Later changes to this definition do not affect the returned container, and the copy the container owns cannot be changed.

public virtual TemporaryContainer CreateContainer()

Returns

TemporaryContainer A new container.