.NET › .NET CLI, SDK & Project Structure (csproj)

What is the difference between dotnet build, dotnet publish, and dotnet run?

Advanced

Answer

dotnet build compiles the project into assemblies in the output directory (bin/Debug or bin/Release), suitable for local development but not necessarily deployment-ready. dotnet publish produces a self-contained, deployment-ready output including all dependencies (and optionally the runtime itself for self-contained deployments) in a separate publish folder. dotnet run builds (if needed) and immediately executes the application, primarily for local development iteration.
dotnet build -c Release
dotnet publish -c Release -o ./publish --self-contained true -r linux-x64
dotnet run --project src/MyApi
Real-world example A CI/CD pipeline uses `dotnet publish` to produce the exact artifact deployed to production, while developers use `dotnet run` dozens of times a day during local iteration for fast feedback.

Common follow-ups: What's the difference between framework-dependent and self-contained deployment?;How does dotnet publish handle trimming and AOT compilation?