dotnet publish --self-contained: RID Builds
dotnet publish --self-contained true -r <rid> produces an app that bundles the .NET runtime so the target machine needs nothing installed.
A self-contained publish is the portable build. It requires a runtime identifier and a restore that targeted that RID.
What it does
With --self-contained true and a runtime identifier (-r), publish copies the .NET runtime into the output so the app runs on a machine with no .NET installed. Without a RID, self-contained is not possible; with --self-contained false, only the app and dependencies ship (framework-dependent).
Common usage
dotnet publish -c Release -r linux-x64 --self-contained true -o ./out
dotnet publish -c Release -r win-x64 --self-contained true \
-p:PublishSingleFile=true -o ./out
dotnet publish -c Release -r linux-musl-x64 --self-contained true -o ./outOptions
| Flag | What it does |
|---|---|
| -r, --runtime <rid> | Runtime identifier, e.g. linux-x64, win-x64, osx-arm64 |
| --self-contained true | Bundle the .NET runtime into the output |
| -p:PublishSingleFile=true | Produce one executable instead of a folder of files |
| -p:PublishTrimmed=true | Trim unused framework assemblies to shrink size |
| -p:InvariantGlobalization=true | Drop ICU data for a smaller, culture-invariant build |
In CI
Pick the RID for your deploy target: linux-musl-x64 for Alpine containers, linux-x64 for glibc, win-x64/osx-arm64 otherwise. If you restore in a separate step, restore with the same -r or publish will fail because no assets exist for that RID. Trimming shrinks the image but can break reflection-heavy code, so test the trimmed output.
Common errors in CI
NETSDK1047 ("Assets file ... doesn't have a target for ... Ensure that restore has run and that you have included ... in the TargetFrameworks ... You may also need to include '<rid>' in your project's RuntimeIdentifiers") means restore did not target the RID; add -r to restore or remove --no-restore. NETSDK1031 relates to self-contained without a RID. A trimmed app throwing MissingMethodException at runtime means trimming removed code reached only by reflection.