Search code examples
c#asp.netazuredotnet-aspire

.NET Aspire - How to execute command from Aspire App Host inside container


in .NET Aspire I'm trying to run a command into the docker container but I don't know how to do it

for example run mongod --port 27000 command

and this is my Aspire AppHost program.cs file

var builder = DistributedApplication.CreateBuilder(args);

var mongo = builder.AddMongoDB("mymongo").AddDatabase("mydb");

var apiService = builder.AddProject<Projects.AspireApp3_ApiService>("apiservice")
    .WithReference(mongo);

builder. Build().Run();

Solution

  • you can use WithArgs() to execute your command like this

    var mongo = builder.AddMongoDB("mymongo")
    .WithArgs("mongod", "--port", "27000")
    .AddDatabase("mydb");
    

    and the result is

    enter image description here

    and You can also use it for AddProject like this

    var apiService = builder.AddProject<Projects.AspireApp3_ApiService>("apiservice")
        .WithArgs("dotnet","--help")
        .WithReference(mongo);