A .NET application can have a small amount of source code and still produce a surprisingly large Docker image. One reason is the NuGet packages used by the application.
When developers add a package, they usually focus on the functionality it provides. A logging library, database provider, JSON serializer, reporting component, or image-processing package may solve an immediate requirement. However, that package can also introduce additional dependencies, managed assemblies, native libraries, and other runtime files.
The impact becomes more noticeable when an application uses many packages or depends on libraries with large dependency trees.
The challenge is that the package list does not directly tell developers how much space each package contributes to the final Docker image. A package might contain several assemblies, while another might bring native binaries for multiple platforms. Some dependencies are required only during development and never reach the production image.
To reduce image size effectively, developers need to trace the NuGet dependency graph, inspect the published application, and connect those files to the final Docker image.
Understand How NuGet Packages Reach a Docker Image
When a project references a NuGet package, NuGet resolves its dependencies during restore.
For example, consider the following project file:
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer"
Version="10.0.0" />
<PackageReference Include="Serilog.AspNetCore"
Version="10.0.0" />
<PackageReference Include="AutoMapper"
Version="15.0.0" />
</ItemGroup>
These are direct package references. However, the application may also receive additional packages because the referenced libraries depend on them.
These are called transitive dependencies.
For example, a database provider may depend on other Entity Framework Core packages and supporting libraries. A logging integration may bring additional logging abstractions and extensions.
The resulting dependency graph can become considerably larger than the original PackageReference list.
NuGet resolves this graph during restore and records the selected dependencies and assets in the project.assets.json file under the project's obj directory.
It is important to understand that restoring a package does not automatically mean every file inside that package will be copied into the final Docker image. Only assets selected for the project and publishing scenario are relevant to the published output.
List Direct and Transitive NuGet Dependencies
Start by identifying every package used by the project.
For SDK versions that support the newer command syntax, run:
dotnet package list --include-transitive
For older SDK command syntax, use:
dotnet list package --include-transitive
The command displays direct and transitive package references.
This helps identify packages that are not explicitly declared in the project file but are still included through other dependencies.
For example, the output may contain:
Top-level Package:
Microsoft.EntityFrameworkCore.SqlServer
Transitive Packages:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Relational
Microsoft.Data.SqlClient
Microsoft.Extensions.DependencyInjection
The exact dependencies depend on the package versions and target framework.
Do not immediately remove transitive packages because they appear unnecessary. Another library may require them at runtime.
The first objective is to understand the dependency graph, not to modify it.
Find Out Why a Package Is Included
Listing dependencies tells you which packages are present. It does not always explain which package introduced a particular dependency.
The dotnet nuget why command helps trace that relationship.
For example:
dotnet nuget why MyApplication.csproj Microsoft.Data.SqlClient
This displays the dependency path that causes the selected package to be included.
It is especially useful when several direct dependencies share the same transitive package.
You can also investigate a particular target framework:
dotnet nuget why MyApplication.csproj Microsoft.Data.SqlClient \
--framework net10.0
This makes it easier to understand why a dependency appears in one target framework but not another.
For multi-targeted applications, inspect each framework separately. Runtime-specific dependencies may also differ depending on the selected runtime identifier.
This investigation helps answer an important question: is the package required by application code, or is it included indirectly by another library?
Inspect the Published Output to Find Large Files
The package dependency graph identifies what the application references. The published output shows what actually reaches the deployment stage.
Publish the application:
dotnet publish MyApplication.csproj \
-c Release \
-o ./artifacts/publish
Now inspect the output directory.
On Linux or macOS, use:
du -ah ./artifacts/publish | sort -h
To display the largest files first:
du -ah ./artifacts/publish | sort -hr | head -20
On Windows PowerShell, use:
Get-ChildItem .\artifacts\publish -Recurse -File |
Sort-Object Length -Descending |
Select-Object -First 20 FullName,
@{Name="SizeMB";Expression={
[math]::Round($_.Length / 1MB, 2)
}}
These commands help identify large DLLs, native libraries, configuration files, and other published assets.
For example, the output might reveal:
MyApplication.dll
Some.Reporting.Library.dll
Microsoft.Data.SqlClient.dll
Some.Native.Dependency.so
A large file is worth investigating, but its size alone does not prove that it should be removed.
The file may be required for a production feature. The important task is to connect the file to its NuGet package and determine whether the application actually needs that functionality.
Understand Runtime Assets and Native Libraries
Not all NuGet packages contain only managed .NET assemblies.
Some packages include native libraries that support specific operating systems and processor architectures.
For example, an image-processing package might provide native binaries for Linux and Windows. A database driver or machine-learning library may also include platform-specific components.
NuGet uses runtime-specific assets to select the appropriate files for the target environment.
A package can contain assets under directories such as:
runtimes/
linux-x64/
native/
linux-arm64/
native/
win-x64/
native/
When publishing for a particular runtime identifier, the SDK selects compatible assets according to NuGet's asset-selection rules.
This means the contents of a Linux container may differ from those of a Windows container built from the same project.
For example:
dotnet publish MyApplication.csproj \
-c Release \
-r linux-x64 \
--self-contained false \
-o ./artifacts/linux-publish
The resulting output is intended for Linux x64.
Inspecting this directory is more useful than measuring the entire NuGet global package cache, because the cache contains downloaded package contents that may never be deployed.
Separate Package Cache Size From Docker Image Size
One common investigation mistake is measuring the NuGet package cache and assuming it represents the application image size.
NuGet normally stores downloaded packages in a global packages folder. On many systems, this is located under the user's NuGet directory.
You can find its location using:
dotnet nuget locals global-packages --list
The package cache may contain multiple versions, documentation, build tools, analyzers, and assets for different platforms.
However, these files are not automatically copied into a correctly structured multi-stage Docker image.
Consider this Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:11.0 AS build
WORKDIR /source
COPY MyApplication.csproj ./
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release \
-o /app/publish \
--no-restore
FROM mcr.microsoft.com/dotnet/aspnet:11.0 AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "MyApplication.dll"]
The build stage contains the SDK and restored NuGet packages. The final stage receives only the published application files.
This separation is important because the size of the global NuGet cache is not the same as the size of the final application image.
When investigating a large image, measure the published output and inspect the final image rather than relying only on the package cache.
Compare Docker Image Layers
After inspecting the published files, investigate the Docker image itself.
Build the application image:
docker build -t myapp:latest .
Check the image size:
docker images myapp
Then inspect its layers:
docker history myapp:latest
The history output shows the Dockerfile instructions and their associated layer sizes.
For example, a large COPY --from=build layer may indicate that the published application contributes significantly to the image.
However, Docker layer sizes do not automatically identify individual NuGet packages. The published files may be grouped into a single layer.
To understand the contribution of individual dependencies, combine the published-directory inspection with the package dependency graph.
For more detailed image analysis, use Docker image inspection tools or a container image analyzer that can report files by layer.
Also remember that the image size displayed by Docker is not necessarily the same as the compressed size transferred to a registry. Storage, transfer, and runtime disk usage are different measurements.
Identify Packages That Are Safe to Remove
After identifying large dependencies, review whether the application actually uses them.
For example, a project may contain:
<ItemGroup>
<PackageReference Include="Some.Reporting.Library"
Version="5.0.0" />
<PackageReference Include="Some.Legacy.Json.Library"
Version="3.0.0" />
</ItemGroup>
If the reporting feature has been removed, the first package may no longer be necessary.
Before removing it, check:
Whether application code references its types.
Whether another project depends on it.
Whether it is loaded dynamically.
Whether it is required by a runtime feature.
Whether it introduces native assets or additional dependencies.
Remove unused direct references from the project file and rebuild.
Do not manually delete DLLs from the publish directory to reduce image size. A package may be loaded dynamically or required by another component even when there are no obvious direct references.
The correct solution is to update the dependency graph and publish the application again.
Use Package Asset Controls Carefully
NuGet PackageReference supports asset controls that determine which package assets flow into a project.
For example:
<PackageReference Include="Some.Package"
Version="2.0.0"
ExcludeAssets="analyzers" />
This excludes analyzer assets from that reference.
Other controls include IncludeAssets and PrivateAssets.
These settings have different purposes:
Setting | Purpose |
|---|
IncludeAssets | Specifies which asset categories are consumed by the project |
ExcludeAssets | Specifies which asset categories are excluded |
PrivateAssets | Controls which assets are exposed to parent projects |
These options should not be used as shortcuts for removing runtime dependencies.
For example, excluding runtime assets from a package that provides required runtime functionality can cause application failures.
Use asset controls only when you understand the package structure and know that the excluded assets are unnecessary for the intended build and deployment.
Common Mistakes to Avoid
Mistake | Why it creates problems | Better approach |
|---|
Measuring the NuGet cache as the image size | The cache contains files that may never be published | Inspect the publish directory and final image |
Looking only at direct dependencies | Large transitive dependencies remain hidden | Use --include-transitive |
Removing packages based only on file size | Required runtime functionality may break | Trace package usage and dependency relationships |
Ignoring native assets | Native libraries can contribute significantly to image size | Inspect runtime-specific published files |
Comparing different runtime targets | Published assets may differ by platform | Compare equivalent runtime identifiers |
Deleting DLLs manually | Dynamic loading or dependent libraries may fail | Remove or replace the responsible package reference |
Assuming one Docker layer identifies one package | Multiple published files may be copied together | Combine layer inspection with package and file analysis |
Best Practices for Production Applications
Review direct and transitive dependencies regularly rather than waiting for image sizes to become a problem.
Use dotnet nuget why to understand why large transitive packages are included.
Inspect Release publishing output for large managed assemblies and native files.
Publish for the actual deployment runtime identifier when platform-specific assets are involved.
Keep SDK and runtime stages separate so that the NuGet cache and build tools do not enter the final image.
Remove unused packages from project files instead of deleting published files manually.
Use package asset controls only when their effect on build and runtime behavior is understood.
Measure the final image after every significant dependency change.
Test application functionality after removing or replacing a dependency.
Review dependency updates for both size and security implications.
Summary
NuGet packages can increase Docker image size through managed assemblies, transitive dependencies, native libraries, and other runtime assets. However, the number of packages in a project does not directly indicate how much space they contribute to the final image.
A reliable investigation starts with dotnet package list --include-transitive and dotnet nuget why. Developers should then inspect the published output, identify large files, and compare Docker image layers.
The most important distinction is between downloaded NuGet packages and files that actually reach the production container. A properly configured multi-stage build keeps the SDK and package cache out of the final image.
By tracing dependencies instead of removing packages blindly, developers can reduce unnecessary image content without breaking application functionality or introducing production issues.
Join the conversation! Your thoughts help the community grow.