dotnet-publish
Package .NET application for deployment
TLDR
Compile a .NET project in release mode
Publish the .NET Core runtime with your application for the specified runtime
Package the application into a platform-specific single-file executable
Trim unused libraries to reduce the deployment size of an application
Compile a .NET project without restoring dependencies
Specify the output directory
SYNOPSIS
dotnet publish [PROJECT | SOLUTION] [options]
The PROJECT | SOLUTION argument can be a path to a project file, a solution file, or a directory containing a .NET project or solution. If omitted, the command searches for a project or solution file in the current directory.
PARAMETERS
--configuration | -c
Sets the build configuration. Defaults to 'Debug' or 'Release' based on environment variables.
--framework | -f
Targets a specific framework, e.g., 'net8.0'.
--runtime | -r
Publishes the application for a specific runtime (OS and CPU architecture), e.g., 'linux-x64', 'win-x64', 'osx-arm64'.
--output | -o
Specifies the path for the output directory. If not specified, the output is placed in a default 'bin/[configuration]/[framework]/publish/' folder.
--self-contained [true|false]
Publishes the .NET runtime with the application, making it fully portable. Requires a Runtime Identifier (RID). Defaults to 'true' if an RID is specified, otherwise 'false'.
--no-build
Does not build the project before publishing. The 'restore' step is skipped if 'no-build' is also specified.
--no-restore
Does not restore NuGet packages before publishing.
--verbosity
Sets the verbosity level of the command. Allowed levels are 'q[uiet]', 'm[inimal]', 'n[ormal]', 'd[etailed]', and 'diag[nostic]'.
DESCRIPTION
The dotnet publish command compiles the application, reads through its dependencies specified in the project file, and publishes the resulting set of files to a directory. This command is crucial for preparing a .NET application for deployment across various environments, including Linux, Windows, macOS, or Docker containers.
Unlike dotnet build, which primarily compiles the source code, dotnet publish goes a step further by collecting all the necessary components for deployment: the application's executable, its dependencies (libraries), configuration files, and static assets. The output can be either framework-dependent, requiring the target machine to have the .NET runtime installed, or self-contained, where the .NET runtime and framework libraries are bundled with the application, making it fully portable.
CAVEATS
When publishing self-contained applications, the resulting deployment package can be significantly larger due to the bundled .NET runtime and framework libraries. Ensure sufficient disk space on the target system.
Cross-platform publishing requires specifying a correct Runtime Identifier (RID) for the target environment. Publishing for one platform (e.g., Windows) and trying to run on another (e.g., Linux) without the correct RID will fail unless it's a framework-dependent deployment with the correct runtime installed on the target.
DEPLOYMENT TYPES
dotnet publish supports two primary deployment types:
- Framework-Dependent Deployment (FDD): Creates a smaller deployment where the application relies on a globally installed .NET runtime on the target machine.
- Self-Contained Deployment (SCD): Includes the .NET runtime and framework libraries with the application, making it fully portable but larger in size. This is often preferred for environments where the .NET runtime might not be pre-installed or where specific runtime versions are required.
RUNTIME IDENTIFIERS (RIDS)
RIDs are crucial for targeting specific operating systems and CPU architectures (e.g., linux-x64, win-arm64, osx-x64). When using the --runtime option with dotnet publish, you are instructing the CLI to package your application specifically for that target environment, including its native dependencies. This is essential for self-contained deployments and for any scenario involving platform-specific native code.
PUBLISHING FOR DOCKER
dotnet publish is widely used in Docker build processes. Typically, a multi-stage Dockerfile will use a build stage to publish the application, often using a specific RID (e.g., linux-x64), and then copy the published output into a smaller runtime image. This ensures that the final Docker image contains only the necessary application files and runtime components, minimizing image size.
HISTORY
The dotnet publish command has been a fundamental part of the .NET CLI (Command Line Interface) since the early days of .NET Core, designed to facilitate cross-platform application deployment. It has evolved alongside the .NET ecosystem, adapting to new runtime versions, deployment models (like AOT compilation), and target environments, consistently serving as the primary method for preparing .NET applications for distribution.
SEE ALSO
dotnet(1)