Host external executables in Aspire
이 콘텐츠는 아직 번역되지 않았습니다.
In Aspire, you can host external executable applications alongside your projects using the AddExecutable method. This capability is useful when you need to integrate executable applications or tools into your distributed application, such as Node.js applications, Python scripts, or specialized CLI tools.
When to use executable resources
Section titled “When to use executable resources”Use executable resources when you need to:
- Run applications or tools directly on the host instead of in a container.
- Integrate command-line tools or utilities into your application.
- Run external processes that other resources depend on.
- Develop with tools that provide local development servers.
Common examples include:
- Frontend development servers: Tools like Vercel CLI or webpack dev server.
- Language-specific applications: Node.js apps, Python scripts, or Go applications.
- Database tools: Migration utilities or database seeders.
- Build tools: Asset processors or code generators.
Basic usage
Section titled “Basic usage”The AddExecutable method requires a resource name, the executable path, and optionally command-line arguments and a working directory:
var builder = DistributedApplication.CreateBuilder(args);
// Basic executable without argumentsvar nodeApp = builder.AddExecutable("frontend", "node", ".", "server.js");
// Executable with command-line argumentsvar pythonApp = builder.AddExecutable( "api", "python", ".", "-m", "uvicorn", "main:app", "--reload", "--host", "0.0.0.0", "--port", "8000");
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
// Basic executable without argumentsconst const nodeApp: ExecutableResource
nodeApp = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("frontend", "node", ".", ["server.js"]);
// Executable with command-line argumentsconst const pythonApp: ExecutableResource
pythonApp = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("api", "python", ".", ["-m", "uvicorn", "main:app", "--reload", "--host", "0.0.0.0", "--port", "8000"]);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();This code demonstrates setting up a basic executable resource. The first example runs a Node.js server script, while the second starts a Python application using Uvicorn with specific configuration options passed as arguments directly to the AddExecutable method.
Resource dependencies and environment configuration
Section titled “Resource dependencies and environment configuration”You can provide command-line arguments directly in the AddExecutable call and configure environment variables for resource dependencies. Executable resources can reference other resources and access their connection information.
Arguments in the AddExecutable call
Section titled “Arguments in the AddExecutable call”var builder = DistributedApplication.CreateBuilder(args);
// Arguments provided directly in AddExecutablevar app = builder.AddExecutable( "vercel-dev", "vercel", ".", "dev", "--listen", "3000");import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
// Arguments provided directly in addExecutableconst const app: ExecutableResource
app = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("vercel-dev", "vercel", ".", ["dev", "--listen", "3000"]);Resource dependencies with environment variables
Section titled “Resource dependencies with environment variables”For arguments that depend on other resources, use environment variables:
var builder = DistributedApplication.CreateBuilder(args);
var redis = builder.AddRedis("cache");var postgres = builder.AddPostgres("postgres").AddDatabase("appdb");
var app = builder.AddExecutable("worker", "python", ".", "worker.py") .WithReference(redis) // Provides ConnectionStrings__cache .WithReference(postgres); // Provides ConnectionStrings__appdbimport { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
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 resource
addRedis("cache");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 server resource
addPostgres("postgres")).PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("appdb");
const const app: ExecutableResource
app = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("worker", "python", ".", ["worker.py"]) .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ExecutableResource (+1 overload)
Adds a reference to another resource
withReference(const redis: RedisResource
redis) // Provides ConnectionStrings__cache .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ExecutableResource (+1 overload)
Adds a reference to another resource
withReference(const postgres: PostgresDatabaseResource
postgres); // Provides ConnectionStrings__appdbWhen one resource depends on another, WithReference passes along environment variables containing the dependent resource’s connection details. For example, the worker executable’s reference to redis and postgres provides it with the ConnectionStrings__cache and ConnectionStrings__appdb environment variables, which contain connection strings to these resources.
Access specific endpoint information
Section titled “Access specific endpoint information”For more control over how connection information is passed to your executable:
var builder = DistributedApplication.CreateBuilder(args);
var redis = builder.AddRedis("cache");
var app = builder.AddExecutable("app", "node", ".", "app.js") .WithReference(redis) .WithEnvironment(context => { // Provide individual connection details context.EnvironmentVariables["REDIS_HOST"] = redis.Resource.PrimaryEndpoint.Property(EndpointProperty.Host); context.EnvironmentVariables["REDIS_PORT"] = redis.Resource.PrimaryEndpoint.Property(EndpointProperty.Port); });import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
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 resource
addRedis("cache");
const const app: ExecutableResource
app = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("app", "node", ".", ["app.js"]) .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): ExecutableResource (+1 overload)
Adds a reference to another resource
withReference(const redis: RedisResource
redis) .ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ExecutableResource
Sets an environment variable on the resource
withEnvironment("REDIS_HOST", const redis: RedisResource
redis.RedisResource.host: PropertyAccessor<EndpointReferenceExpression>
Gets the Host property
host) .ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ExecutableResource
Sets an environment variable on the resource
withEnvironment("REDIS_PORT", const redis: RedisResource
redis.RedisResource.port: PropertyAccessor<EndpointReferenceExpression>
Gets the Port property
port);Practical example: Vercel CLI
Section titled “Practical example: Vercel CLI”Here’s a complete example using the Vercel CLI to host a frontend application with a backend API:
var builder = DistributedApplication.CreateBuilder(args);
// Backend APIvar api = builder.AddProject<Projects.Api>("api") .WithExternalHttpEndpoints();
// Frontend with Vercel CLIvar frontend = builder.AddExecutable( "vercel-dev", "vercel", ".", "dev", "--listen", "3000") .WithEnvironment("API_URL", api.GetEndpoint("http")) .WithHttpEndpoint(port: 3000, name: "http");
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
// Backend APIconst const api: ProjectResource
api = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addProject(name: string, projectPath: string, launchProfileName?: string): ProjectResource (+1 overload)
Adds a .NET project resource
addProject("api", "./Api/Api.csproj", "https") .ProjectResource.withExternalHttpEndpoints(): ProjectResource
Makes HTTP endpoints externally accessible
withExternalHttpEndpoints();
// Frontend with Vercel CLIconst const frontend: ExecutableResource
frontend = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("vercel-dev", "vercel", ".", ["dev", "--listen", "3000"]) .ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ExecutableResource
Sets an environment variable on the resource
withEnvironment("API_URL", const api: ProjectResource
api.ProjectResource.getEndpoint(name: string): EndpointReference
Gets an endpoint reference
getEndpoint("http")) .ExecutableResource.withHttpEndpoint(options?: { port?: number; targetPort?: number; name?: string; env?: string; isProxied?: boolean;} | undefined): ExecutableResource (+1 overload)
Adds an HTTP endpoint
withHttpEndpoint({ port?: number | undefined
port: 3000, name?: string | undefined
name: "http" });
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();Configure endpoints
Section titled “Configure endpoints”Executable resources can expose HTTP endpoints that other resources can reference:
var builder = DistributedApplication.CreateBuilder(args);
var frontend = builder.AddExecutable( "webpack-dev", "npx", ".", "webpack", "serve", "--port", "8080", "--host", "0.0.0.0") .WithHttpEndpoint(port: 8080, name: "http");
// Another service can reference the frontendvar e2eTests = builder.AddExecutable("playwright", "npx", ".", "playwright", "test") .WithEnvironment("BASE_URL", frontend.GetEndpoint("http"));import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const frontend: ExecutableResource
frontend = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("webpack-dev", "npx", ".", ["webpack", "serve", "--port", "8080", "--host", "0.0.0.0"]) .ExecutableResource.withHttpEndpoint(options?: { port?: number; targetPort?: number; name?: string; env?: string; isProxied?: boolean;} | undefined): ExecutableResource (+1 overload)
Adds an HTTP endpoint
withHttpEndpoint({ port?: number | undefined
port: 8080, name?: string | undefined
name: "http" });
// Another service can reference the frontendconst const e2eTests: ExecutableResource
e2eTests = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("playwright", "npx", ".", ["playwright", "test"]) .ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ExecutableResource
Sets an environment variable on the resource
withEnvironment("BASE_URL", const frontend: ExecutableResource
frontend.ExecutableResource.getEndpoint(name: string): EndpointReference
Gets an endpoint reference
getEndpoint("http"));Environment configuration
Section titled “Environment configuration”Configure environment variables for your executable:
var builder = DistributedApplication.CreateBuilder(args);
var app = builder.AddExecutable( "api", "uvicorn", ".", "main:app", "--reload", "--host", "0.0.0.0") .WithEnvironment("DEBUG", "true") .WithEnvironment("LOG_LEVEL", "info") .WithEnvironment(context => { // Dynamic environment variables context.EnvironmentVariables["START_TIME"] = DateTimeOffset.UtcNow.ToString(); });import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const app: ExecutableResource
app = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("api", "uvicorn", ".", ["main:app", "--reload", "--host", "0.0.0.0"]) .ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ExecutableResource
Sets an environment variable on the resource
withEnvironment("DEBUG", "true") .ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ExecutableResource
Sets an environment variable on the resource
withEnvironment("LOG_LEVEL", "info") .ExecutableResource.withEnvironment(name: string, value: string | IResourceWithConnectionString | IValueProvider): ExecutableResource
Sets an environment variable on the resource
withEnvironment("START_TIME", new var Date: DateConstructornew () => Date (+3 overloads)
Date().Date.toISOString(): string
Returns a date as a string value in ISO format.
toISOString());Publishing with PublishAsDockerFile
Section titled “Publishing with PublishAsDockerFile”For production deployment, executable resources need to be containerized. Use the PublishAsDockerFile method to specify how the executable should be packaged:
var builder = DistributedApplication.CreateBuilder(args);
var app = builder.AddExecutable( "frontend", "npm", ".", "start", "--port", "3000") .PublishAsDockerFile();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const app: ExecutableResource
app = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("frontend", "npm", ".", ["start", "--port", "3000"]) .ExecutableResource.publishAsDockerFile(): ExecutableResource
Publishes the executable as a Docker container
publishAsDockerFile();When you call PublishAsDockerFile(), Aspire generates a Dockerfile during the publish process. You can customize this by providing your own Dockerfile:
Custom Dockerfile for publishing
Section titled “Custom Dockerfile for publishing”Create a Dockerfile in your executable’s working directory:
FROM node:22-alpineWORKDIR /appCOPY package*.json ./RUN npm ci --only=productionCOPY . .EXPOSE 3000CMD ["npm", "start"]Then reference it in your AppHost:
var builder = DistributedApplication.CreateBuilder(args);
var app = builder.AddExecutable("frontend", "npm", ".", "start") .PublishAsDockerFile([new DockerfileBuildArg("NODE_ENV", "production")]);import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const app: ExecutableResource
app = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addExecutable(name: string, command: string, workingDirectory: string, args: string[]): ExecutableResource
Adds an executable resource
addExecutable("frontend", "npm", ".", ["start"]) .ExecutableResource.publishAsDockerFile(): ExecutableResource
Publishes the executable as a Docker container
publishAsDockerFile();Best practices
Section titled “Best practices”When working with executable resources:
- Use explicit paths: For better reliability, use full paths to executables when possible.
- Handle dependencies: Use
WithReferenceto establish proper dependency relationships. - Configure explicit start: Use
WithExplicitStart()for executables that shouldn’t start automatically. - Prepare for deployment: Always use
PublishAsDockerFile()for production scenarios. - Environment isolation: Use environment variables rather than command-line arguments for sensitive configuration.
- Resource naming: Use descriptive names that clearly identify the executable’s purpose.