Skip to content

Support executing commands inside container resourcesΒ #10301

@DamianEdwards

Description

@DamianEdwards

Add support for executing commands in container resources, i.e. equivalent of docker exec.

In the dashboard

All container resources get a new command like "Execute command..." that when selected pops a dialog using the interaction service that prompts for the command to execute (required) and the working directory in the container to execute it in (optional, defaults to root). Once accepted the command is executed in the resource's container (via existing DCP exec functionality that needs to be hooked up to Aspire.Hosting I believe). Logs from the execution should be output to the resource's logs.

The command should only be enabled when the container is running (unhealthy is fine).

It would be great if we could auto-navigate to the resource logs page once the command prompt is submitted so that the execution and output of the command is immediately visible to the user.

In Aspire.Hosting

A new API is added to allow easily adding a pre-defined exec command to any container resource:

builder.AddContainer("mycontainer",
    .WithExecCommand("sh -c \"hello from inside the container!\"", "Echo hello");

It should be based on the API signature of the WithCommand and WithHttpCommand methods, accepting required info as parameters and then a CommandOptions or if suitable a custom derived ExecCommandOptions for all the other info (icon, etc.), e.g.:

public static IResourceBuilder<T> WithExecCommand<T>(this IResourceBuilder builder, string command, string displayName, string? workingDir = null, string? commandName = null, ExecCommandOptions? commandOptions = null)
    where T : ContainerResource;

Additionally, an API that exposes the actual capability to perform the container exec should be publicly available so that any custom command or code otherwise running in the AppHost can perform a container exec when it needs to, e.g. ContainerExecService.

In the aspire CLI

The Aspire CLI's exec command will be updated to support executing commands in containers. This should feel similar to using the exec command for executing a command in the context of a project resource, but this time rather than simply getting the configuration of the resource applied to the command, the command itself will be executed inside the container:

$ aspire exec --resource mycontainer sh -c "hello from inside the container!"

The command should start the AppHost, ensure the target container resource is running (along with all of its dependencies) before executing the command and streaming the output back to the terminal, and exit using the exit code of the command inside the container.

It should be possible to set the working directory inside the container via a -w/--workdir option, e.g.:

$ aspire exec --resource mycontainer --workdir /some/dir ls -r

Metadata

Metadata

Assignees

Labels

area-app-modelIssues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplicationarea-clipod-e2e

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions