DocumentationEssayer Aspire
DocumentationEssayer

Migrate from Docker Compose to Aspire

Ce contenu n’est pas encore disponible dans votre langue.

This guide helps you understand how to migrate applications from Docker Compose to Aspire, highlighting the key conceptual differences and providing accurate, practical examples for common migration scenarios.

While Docker Compose and Aspire might seem similar at first glance, they serve different purposes and operate at different levels of abstraction.

Docker ComposeAspire
Primary purposeContainer orchestrationDevelopment-time orchestration and app composition
ScopeContainer-focusedMulti-resource (containers, .NET projects, cloud resources)
ConfigurationYAML-basedC#-based, strongly typed
Target environmentAny Docker runtimeDevelopment and cloud deployment
Service discoveryDNS-based container discoveryBuilt-in service discovery with environment variables
Development experienceManual container managementIntegrated tooling, dashboard, and telemetry

When migrating from Docker Compose to Aspire, consider these conceptual differences:

  • From YAML to C# — Configuration moves from declarative YAML to imperative, strongly-typed C# code
  • From containers to resources — Aspire manages not just containers, but .NET projects, executables, parameters, and cloud resources
  • From manual networking to service discovery — Aspire automatically configures service discovery and connection strings
  • From development gaps to integrated experience — Aspire provides dashboard, telemetry, and debugging integration
  • Startup orchestration differs — Docker Compose depends_on controls startup order, while Aspire WithReference only configures service discovery; use WaitFor for startup ordering

For detailed API mappings, see Docker Compose to Aspire AppHost API reference.

This section demonstrates practical migration scenarios you’ll likely encounter when moving from Docker Compose to Aspire. Each pattern shows a complete Docker Compose example alongside its accurate Aspire equivalent.

This example shows a typical three-tier application with a frontend, API, and database.

Docker Compose example:

compose.yaml
version: '3.8'
services:
frontend:
build: ./frontend
ports:
- "3000:3000"
depends_on:
api:
condition: service_healthy
environment:
- API_URL=http://api:5000
api:
build: ./api
ports:
- "5000:5000"
depends_on:
database:
condition: service_healthy
environment:
- ConnectionStrings__DefaultConnection=Host=database;Database=myapp;Username=postgres;Password=secret
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:5000/health"]
interval: 10s
timeout: 3s
retries: 3
database:
image: postgres:15
environment:
- POSTGRES_DB=myapp
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=secret
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 3s
retries: 3
volumes:
postgres_data:

Aspire equivalent:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
// Add PostgreSQL with explicit version and persistent storage
const
const database: PostgresDatabaseResource
database
= (await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addPostgres(name: string, options?: {
userName?: string | ParameterResource;
password?: string | ParameterResource;
port?: number;
}): PostgresServerResource (+1 overload)

Adds a PostgreSQL resource to the application model. A container is used for local development.

addPostgres
("postgres")
.
ContainerResource.withImageTag(tag: string): PostgresServerResource

Allows overriding the image tag on a container.

withImageTag
("15")
.
PostgresServerResource.withDataVolume(options?: {
name?: string;
isReadOnly?: boolean;
} | undefined): PostgresServerResource (+1 overload)

Adds a named volume for the data folder to a PostgreSQL container resource.

withDataVolume
())
.
PostgresServerResource.addDatabase(name: string, options?: {
databaseName?: string;
} | undefined): PostgresDatabaseResource (+1 overload)

Adds a PostgreSQL database to the application model.

addDatabase
("myapp");
// Add the API project with proper dependencies
const
const api: ProjectResource
api
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("api", "./MyApp.Api/MyApp.Api.csproj")
.
ProjectResource.withHttpEndpoint(options?: {
port?: number;
targetPort?: number;
name?: string;
env?: string;
isProxied?: boolean;
} | undefined): ProjectResource (+1 overload)

Adds an HTTP endpoint

withHttpEndpoint
({
port?: number | undefined
port
: 5000 })
.
ProjectResource.withHttpHealthCheck(path?: string, statusCode?: number, endpointName?: string): ProjectResource (+1 overload)

Adds a health check to the resource which is mapped to a specific endpoint.

withHttpHealthCheck
("/health")
.
ProjectResource.withReference(source: EndpointReference | string | uri, connectionName?: string, optional?: boolean, name?: string): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const database: PostgresDatabaseResource
database
, "DefaultConnection")
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const database: PostgresDatabaseResource
database
);
// Add the frontend project with dependencies
const
const frontend: ProjectResource
frontend
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addProject(name: string, projectPath: string, options?: {
launchProfileOrOptions?: ProjectResourceOptions;
}): ProjectResource (+1 overload)

Adds a .NET project resource

addProject
("frontend", "./MyApp.Frontend/MyApp.Frontend.csproj")
.
ProjectResource.withHttpEndpoint(options?: {
port?: number;
targetPort?: number;
name?: string;
env?: string;
isProxied?: boolean;
} | undefined): ProjectResource (+1 overload)

Adds an HTTP endpoint

withHttpEndpoint
({
port?: number | undefined
port
: 3000 })
.
ProjectResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ProjectResource (+1 overload)

Adds a reference to another resource

withReference
(
const api: ProjectResource
api
)
.
ProjectResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ProjectResource

Sets an environment variable

withEnvironment
("API_URL",
const api: ProjectResource
api
.
ProjectResource.getEndpoint(name: string): EndpointReference

Gets an endpoint reference

getEndpoint
("http"))
.
ProjectResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ProjectResource

Waits for another resource to be ready

waitFor
(
const api: ProjectResource
api
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Key differences explained:

  • Build vs. project — Docker Compose build: services become AddProject<T>() for .NET apps, which runs them directly instead of in containers
  • Ports — Both examples explicitly map ports (3000 and 5000)
  • Startup order — Docker Compose uses depends_on with health conditions; Aspire uses WaitFor() for startup ordering
  • Service discoveryWithReference() only configures service discovery and connection strings; it doesn’t control startup order
  • Connection strings — By default, WithReference(database) provides ConnectionStrings__myapp using the resource name from AddDatabase(). To match a different name like DefaultConnection, use a named reference: .WithReference(database, "DefaultConnection")
  • VolumesWithDataVolume() must be called explicitly to add persistent storage; it’s not automatic
  • Image versionsWithImageTag("15") pins PostgreSQL to version 15

This example shows a mix of existing container images and a Dockerfile-built service being orchestrated.

Docker Compose example:

compose.yaml
version: '3.8'
services:
web:
build: .
ports:
- "8080:8080"
depends_on:
redis:
condition: service_started
postgres:
condition: service_healthy
environment:
- REDIS_URL=redis://redis:6379
- DATABASE_URL=postgresql://postgres:secret@postgres:5432/main
redis:
image: redis:7
ports:
- "6379:6379"
postgres:
image: postgres:15
environment:
POSTGRES_PASSWORD: secret
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
volumes:
postgres_data:

Aspire equivalent:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
// Add backing services with explicit versions
const
const redis: RedisResource
redis
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addRedis(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): RedisResource (+1 overload)

Adds a Redis container to the application model.

addRedis
("redis")
.
ContainerResource.withImageTag(tag: string): RedisResource

Allows overriding the image tag on a container.

withImageTag
("7")
.
RedisResource.withHostPort(port: number | null): RedisResource

Configures the host port that the Redis resource is exposed on instead of using randomly assigned port.

withHostPort
(6379);
const
const postgres: PostgresDatabaseResource
postgres
= (await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addPostgres(name: string, options?: {
userName?: string | ParameterResource;
password?: string | ParameterResource;
port?: number;
}): PostgresServerResource (+1 overload)

Adds a PostgreSQL resource to the application model. A container is used for local development.

addPostgres
("postgres")
.
ContainerResource.withImageTag(tag: string): PostgresServerResource

Allows overriding the image tag on a container.

withImageTag
("15")
.
PostgresServerResource.withDataVolume(options?: {
name?: string;
isReadOnly?: boolean;
} | undefined): PostgresServerResource (+1 overload)

Adds a named volume for the data folder to a PostgreSQL container resource.

withDataVolume
())
.
PostgresServerResource.addDatabase(name: string, options?: {
databaseName?: string;
} | undefined): PostgresDatabaseResource (+1 overload)

Adds a PostgreSQL database to the application model.

addDatabase
("main");
// Build the web app from a Dockerfile (matches Docker Compose "build: .")
const
const web: ContainerResource
web
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addDockerfile(name: string, contextPath: string, options?: {
dockerfilePath?: string;
stage?: string;
}): ContainerResource (+1 overload)

Adds a Dockerfile to the application model that can be treated like a container resource.

addDockerfile
("web", ".")
.
ContainerResource.withHttpEndpoint(options?: {
port?: number;
targetPort?: number;
name?: string;
env?: string;
isProxied?: boolean;
} | undefined): ContainerResource (+1 overload)

Adds an HTTP endpoint

withHttpEndpoint
({
port?: number | undefined
port
: 8080,
targetPort?: number | undefined
targetPort
: 8080 })
.
ContainerResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ContainerResource (+1 overload)

Adds a reference to another resource

withReference
(
const redis: RedisResource
redis
)
.
ContainerResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ContainerResource (+1 overload)

Adds a reference to another resource

withReference
(
const postgres: PostgresDatabaseResource
postgres
)
.
ContainerResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ContainerResource

Waits for another resource to be ready

waitFor
(
const redis: RedisResource
redis
)
.
ContainerResource.waitFor(dependency: IResource | IResourceWithConnectionString, waitBehavior?: WaitBehavior): ContainerResource

Waits for another resource to be ready

waitFor
(
const postgres: PostgresDatabaseResource
postgres
);
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Key differences explained:

  • Image versions — Explicitly specified with WithImageTag() to match Docker Compose
  • Dockerfile builds — Docker Compose build: . maps to AddDockerfile("web", "."), which builds a container image from a Dockerfile. Use AddContainer() for pre-built images that use image: in Docker Compose
  • PortsWithHostPort() maps to a static host port; without it, Aspire assigns a random port
  • VolumesWithDataVolume() must be called explicitly to add persistent storage
  • Startup orderingWaitFor() controls startup order, similar to Docker Compose depends_on with conditions
  • Connection stringsWithReference() provides Aspire-format connection strings (ConnectionStrings__*), not URL-format variables

This example shows different approaches to configuration management.

Docker Compose approach:

compose.yaml
services:
app:
image: myapp:latest
environment:
- DATABASE_URL=postgresql://user:pass@db:5432/myapp
- REDIS_URL=redis://cache:6379
- API_KEY=${API_KEY}
- LOG_LEVEL=info

Aspire approach:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
// Add external parameter for secrets
const
const apiKey: ParameterResource
apiKey
=
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addParameter(name: string, options?: {
value?: string;
publishValueAsDefault?: boolean;
secret?: boolean;
}): ParameterResource (+1 overload)

Adds a parameter resource

addParameter
("apiKey", {
secret?: boolean | undefined
secret
: true });
const
const database: PostgresDatabaseResource
database
= (await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addPostgres(name: string, options?: {
userName?: string | ParameterResource;
password?: string | ParameterResource;
port?: number;
}): PostgresServerResource (+1 overload)

Adds a PostgreSQL resource to the application model. A container is used for local development.

addPostgres
("db"))
.
PostgresServerResource.addDatabase(name: string, options?: {
databaseName?: string;
} | undefined): PostgresDatabaseResource (+1 overload)

Adds a PostgreSQL database to the application model.

addDatabase
("myapp");
const
const cache: RedisResource
cache
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addRedis(name: string, options?: {
port?: number;
password?: string | ParameterResource;
}): RedisResource (+1 overload)

Adds a Redis container to the application model.

addRedis
("cache");
const
const app: ContainerResource
app
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addContainer(name: string, image: AddContainerOptions): ContainerResource

Adds a container resource to the application.

addContainer
("app", {
AddContainerOptions.image?: string | undefined
image
: "myapp",
AddContainerOptions.tag?: string | undefined
tag
: "latest" })
.
ContainerResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ContainerResource (+1 overload)

Adds a reference to another resource

withReference
(
const database: PostgresDatabaseResource
database
)
.
ContainerResource.withReference(source: EndpointReference | string | uri, options?: {
connectionName?: string;
optional?: boolean;
name?: string;
} | undefined): ContainerResource (+1 overload)

Adds a reference to another resource

withReference
(
const cache: RedisResource
cache
)
.
ContainerResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ContainerResource

Sets an environment variable

withEnvironment
("API_KEY",
const apiKey: ParameterResource
apiKey
)
.
ContainerResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ContainerResource

Sets an environment variable

withEnvironment
("LOG_LEVEL", "info");
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();
apphost.mts
const dbPassword = builder.addParameter("dbPassword", { secret: true });
const db = (await builder.addPostgres("db", { password: dbPassword }))
.addDatabase("myapp");
const app = await builder.addContainer("app", "myapp:latest")
.withReference(db)
.withEnvironment("DATABASE_URL",
builder.createReferenceExpression`postgresql://postgres:${dbPassword}@db:5432/myapp`)
.withEnvironment("REDIS_URL", "redis://cache:6379");

Docker Compose example:

compose.yaml
version: '3.8'
services:
app:
image: myapp:latest
volumes:
- app_data:/data
- ./config:/app/config:ro
worker:
image: myworker:latest
volumes:
- app_data:/shared
volumes:
app_data:

Aspire equivalent:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const app: ContainerResource
app
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addContainer(name: string, image: AddContainerOptions): ContainerResource

Adds a container resource to the application.

addContainer
("app", {
AddContainerOptions.image?: string | undefined
image
: "myapp",
AddContainerOptions.tag?: string | undefined
tag
: "latest" })
.
ContainerResource.withVolume(target: string, options?: {
name?: string;
isReadOnly?: boolean;
} | undefined): ContainerResource (+1 overload)

Adds a volume to a container resource.

withVolume
("/data", {
name?: string | undefined
name
: "app-data",
isReadOnly?: boolean | undefined
isReadOnly
: true })
.
ContainerResource.withBindMount(source: string, target: string, options?: {
isReadOnly?: boolean;
} | undefined): ContainerResource (+1 overload)

Adds a bind mount to a container resource.

withBindMount
("./config", "/app/config", {
isReadOnly?: boolean | undefined
isReadOnly
: true });
const
const worker: ContainerResource
worker
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addContainer(name: string, image: AddContainerOptions): ContainerResource

Adds a container resource to the application.

addContainer
("worker", {
AddContainerOptions.image?: string | undefined
image
: "myworker",
AddContainerOptions.tag?: string | undefined
tag
: "latest" })
.
ContainerResource.withVolume(target: string, options?: {
name?: string;
isReadOnly?: boolean;
} | undefined): ContainerResource (+1 overload)

Adds a volume to a container resource.

withVolume
("/shared", {
name?: string | undefined
name
: "app-data" });
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Key differences:

  • Named volumes — Created with AddVolume() and shared between containers
  • Bind mounts — Use WithBindMount() for host directory access

Docker Compose supports custom networks to isolate groups of services from each other:

compose.yaml
services:
proxy:
build: ./proxy
networks:
- frontend
app:
build: ./app
networks:
- frontend
- backend
db:
image: postgres
networks:
- backend
networks:
frontend:
backend:

Aspire doesn’t have an equivalent for custom network isolation. Instead, Aspire automatically creates a shared container network for all container resources and uses service discovery to manage inter-service communication. All containers in an Aspire AppHost can reach each other by resource name. .NET projects and executables run on the host and access containers through injected host/port endpoints.

Successfully migrating from Docker Compose to Aspire requires a systematic approach.

  1. Before migrating, inventory your Docker Compose setup:

    • Services — Identify all services including databases, caches, APIs, and web applications
    • Dependencies — Map out service dependencies from depends_on declarations
    • Data persistence — Catalog all volumes and bind mounts used for data storage
    • Environment variables — List all configuration variables and secrets
    • Health checks — Document any custom health check commands
    • Image versions — Note specific versions used in production
  2. Start by creating a new Aspire project:

    Terminal window
    aspire new aspire-starter -o MyApp
  3. Migrate services one by one, starting with backing services:

    • Add backing services like PostgreSQL, Redis with specific versions using WithImageTag()
    • Add persistent storage using WithDataVolume() where needed
    • Convert .NET applications to project references with AddProject<T>() for better integration
    • Convert Dockerfile-built containers using AddDockerfile() to match build: directives
    • Convert pre-built images using AddContainer() to match image: directives
    • Configure dependencies with WithReference() for service discovery
    • Add startup ordering with WaitFor() to match depends_on behavior
    • Set up environment variables — Note that connection string formats will differ
    • Migrate health checks — Use WithHttpHealthCheck() or WithHealthCheck() for custom checks
  4. For persistent data:

    • Use WithDataVolume() for automatic volume management with integrations
    • Use WithVolume() for named volumes that need to persist data
    • Use WithBindMount() for host directory mounts when you need direct access to host files
    • Start the Aspire AppHost and verify all services start correctly
    • Check the dashboard to confirm service health and connectivity status
    • Validate that inter-service communication works as expected
    • Verify connection strings — If your app expects specific URL formats, you may need to adjust environment variables

Aspire generates .NET-style connection strings (ConnectionStrings__*) rather than URL formats like postgresql:// or redis://.

Solution: If your application expects specific URL formats, construct them manually using WithEnvironment():

apphost.mts
const dbPassword = builder.addParameter("dbPassword", { secret: true });
const postgres = (await builder.addPostgres("db", { password: dbPassword }))
.addDatabase("myapp");
const app = await builder.addContainer("app", "myapp:latest")
.withReference(postgres)
.withEnvironment("DATABASE_URL",
builder.createReferenceExpression`postgresql://postgres:${dbPassword}@db:5432/myapp`);

WithReference() only configures service discovery, not startup ordering.

Solution: Use WaitFor() to ensure dependencies are ready:

apphost.mts
const api = await builder.addProject("api", "./Api/Api.csproj", "https")
.withReference(database) // Service discovery
.waitFor(database); // Startup ordering
  • Use absolute paths for bind mounts to avoid path resolution issues
  • Ensure the host directory exists and has proper permissions
  • Use WithDataVolume() for database integrations — this must be called explicitly

Aspire automatically assigns random ports by default.

Solution: Use WithHostPort() or WithHttpEndpoint(port:) for static port mapping:

apphost.mts
const redis = await builder.addRedis("cache")
.withHostPort(6379);

Docker Compose health checks use shell commands. Aspire integrations (like PostgreSQL and Redis) include built-in health checks automatically. For custom health checks, Aspire offers different approaches depending on the resource type.

Solution: For resources with HTTP endpoints, use WithHttpHealthCheck():

apphost.mts
const api = await builder.addProject("api", "./Api/Api.csproj", "https")
.withHttpHealthCheck("/health");

For custom container health checks that need shell commands (like RabbitMQ), register a custom health check and associate it with the resource:

apphost.mts
const rabbit = await builder.addContainer("rabbitmq", "rabbitmq", "4.1.4-management-alpine")
.withHealthCheck("rabbitmq-health");
// WaitFor uses the registered health check to determine readiness
const app = await builder.addProject("app", "./App/App.csproj", "https")
.waitFor(rabbit);

After migrating to Aspire: