Skip to main content
The ContainerResource class represents a Docker container that can be run as part of your distributed application. It provides extensive configuration options for images, volumes, networking, and runtime behavior.

Class Definition

public class ContainerResource : Resource,
    IResourceWithEnvironment,
    IResourceWithArgs,
    IResourceWithEndpoints,
    IResourceWithWaitSupport,
    IResourceWithProbes
{
    public ContainerResource(string name, string? entrypoint = null);
    
    public string? Entrypoint { get; set; }
}

Constructor

name
string
required
Unique name for the container resource. Used for service discovery and references.
entrypoint
string
Optional custom entrypoint that overrides the container image’s default ENTRYPOINT.

Properties

Entrypoint
string?
Gets or sets the container entrypoint. null means use the default from the image.

Creating Containers

Basic Container

builder.AddContainer("redis", "redis");

From Dockerfile

builder.AddDockerfile("myapp", "./src/MyApp");

Image Configuration

Image Methods

WithImage(image, tag)
IResourceBuilder<ContainerResource>
Sets the container image and optional tag.Parameters:
  • image - Image name (e.g., “redis”, “postgres”)
  • tag - Optional tag (defaults to “latest”)
Example:
builder.AddContainer("app", "placeholder")
       .WithImage("myapp", "v2.0");
WithImageTag(tag)
IResourceBuilder<ContainerResource>
Changes the image tag without modifying the image name.Example:
builder.AddContainer("postgres", "postgres")
       .WithImageTag("16-alpine");
WithImageRegistry(registry)
IResourceBuilder<ContainerResource>
Sets the container registry to pull the image from.Example:
builder.AddContainer("app", "myapp")
       .WithImageRegistry("myregistry.azurecr.io");
WithImageSHA256(sha256)
IResourceBuilder<ContainerResource>
Pins the image to a specific SHA256 digest for immutability.Example:
builder.AddContainer("nginx", "nginx")
       .WithImageSHA256("a1b2c3d4e5f6...");

Storage Configuration

Volumes

Volumes are managed by Docker and persist data between container restarts.
builder.AddContainer("postgres", "postgres")
       .WithVolume("postgres-data", "/var/lib/postgresql/data");
WithVolume(name, target, isReadOnly)
IResourceBuilder<ContainerResource>
Adds a Docker volume to the container.Parameters:
  • name - Volume name (null creates anonymous volume)
  • target - Mount path inside container
  • isReadOnly - Whether the volume is read-only

Bind Mounts

Bind mounts map host directories/files into the container.
builder.AddContainer("nginx", "nginx")
       .WithBindMount("./nginx.conf", "/etc/nginx/nginx.conf", isReadOnly: true);
WithBindMount(source, target, isReadOnly)
IResourceBuilder<ContainerResource>
Mounts a host directory or file into the container.Parameters:
  • source - Host path (relative to app host directory or absolute)
  • target - Mount path inside container
  • isReadOnly - Whether the mount is read-only
Relative paths are resolved from the app host project directory (builder.AppHostDirectory).

Runtime Configuration

Entrypoint and Command

builder.AddContainer("app", "alpine")
       .WithEntrypoint("/bin/sh");

Container Lifetime

builder.AddContainer("redis", "redis")
       .WithLifetime(ContainerLifetime.Session);
// Container is removed when app stops
WithLifetime(lifetime)
IResourceBuilder<ContainerResource>
Sets the container lifetime behavior.Values:
  • ContainerLifetime.Session - Container is removed when app stops (default)
  • ContainerLifetime.Persistent - Container persists across app restarts
Persistent containers are useful for databases but can accumulate over time. Clean them up manually when no longer needed.

Image Pull Policy

builder.AddContainer("app", "myapp")
       .WithImagePullPolicy(ImagePullPolicy.Always);
WithImagePullPolicy(policy)
IResourceBuilder<ContainerResource>
Controls when the container runtime pulls the image.Values:
  • ImagePullPolicy.Always - Always pull the image
  • ImagePullPolicy.IfNotPresent - Pull only if not cached locally
  • ImagePullPolicy.Never - Never pull, fail if not cached

Container Runtime Arguments

Pass arguments directly to the container runtime (docker run or podman run).
builder.AddContainer("ml-app", "tensorflow-gpu")
       .WithContainerRuntimeArgs("--gpus", "all");
WithContainerRuntimeArgs(args)
IResourceBuilder<ContainerResource>
Adds arguments to the container runtime command (not the container entrypoint).Parameters:
  • args - String arguments passed to docker/podman run
These are advanced arguments passed to the container runtime. Use with caution as they can affect security and stability.

Networking

Endpoints

builder.AddContainer("api", "api-image")
       .WithHttpEndpoint(port: 8080, name: "http");
See IResourceBuilder - Endpoints for detailed endpoint configuration.

Complete Examples

PostgreSQL Database

Production PostgreSQL
var postgres = builder.AddContainer("postgres", "postgres", "16-alpine")
    .WithEnvironment("POSTGRES_USER", "admin")
    .WithEnvironment("POSTGRES_PASSWORD", builder.AddParameter("db-password", secret: true))
    .WithEnvironment("POSTGRES_DB", "myapp")
    .WithVolume("postgres-data", "/var/lib/postgresql/data")
    .WithBindMount("./db/init", "/docker-entrypoint-initdb.d", isReadOnly: true)
    .WithEndpoint(targetPort: 5432, port: 5432, name: "postgres")
    .WithLifetime(ContainerLifetime.Persistent);

Nginx Reverse Proxy

Custom Nginx
builder.AddContainer("nginx", "nginx", "alpine")
    .WithBindMount("./nginx/nginx.conf", "/etc/nginx/nginx.conf", isReadOnly: true)
    .WithBindMount("./nginx/conf.d", "/etc/nginx/conf.d", isReadOnly: true)
    .WithBindMount("./static", "/usr/share/nginx/html", isReadOnly: true)
    .WithHttpEndpoint(port: 80, name: "http")
    .WithHttpsEndpoint(port: 443, name: "https");

Custom Application Container

Full Configuration
var apiKey = builder.AddParameter("api-key", secret: true);
var cache = builder.AddRedis("cache");

builder.AddDockerfile("myapp", "./src/MyApp")
    .WithEnvironment("ASPNETCORE_ENVIRONMENT", "Production")
    .WithEnvironment("API_KEY", apiKey)
    .WithEnvironment("REDIS_HOST", cache.GetEndpoint("tcp"))
    .WithHttpsEndpoint(port: 8443)
    .WithVolume("app-data", "/app/data")
    .WithBindMount("./config", "/app/config", isReadOnly: true)
    .WithReference(cache)
    .WithImagePullPolicy(ImagePullPolicy.Always)
    .WithLifetime(ContainerLifetime.Session);

Build docs developers (and LLMs) love