Local NuGet feed
This tutorial shows how to create and use a local NuGet feed when your private feed is unavailable or unreliable.
This is useful when your packages are hosted on a private feed (for example, Azure DevOps), but your build machine cannot always connect to it.
Goal
Use a local folder as a fallback NuGet source so restore and build can continue even if the private feed is temporarily unreachable.
Prerequisites
- Access to your private NuGet feed.
- The required
.nupkgpackage files. - A local folder, for example
C:\dev\local-nuget-feed.
Steps
- Open your private NuGet feed (Azure DevOps in this example).
- Download the packages your solution needs.
- Copy the downloaded
.nupkgfiles to your local folder, for exampleC:\dev\local-nuget-feed. - Update your
nuget.configfile to include the local source.
Use this configuration:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!-- remove any machine-wide sources with <clear/> -->
<clear />
<!-- also get packages from the NuGet Gallery -->
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
<add key="local" value="C:\dev\local-nuget-feed" allowInsecureConnections="True" />
</packageSources>
<activePackageSource>
<add key="All" value="(Aggregate source)" />
</activePackageSource>
</configuration>
Validate
Run restore on the machine with connection issues:
dotnet restore
If restore succeeds, your local feed is configured correctly.
Use This Pattern In A Docker Image Build
You can use the same approach in Docker by copying your local .nupkg files into the image and pointing nuget.config to that folder.
- Create a folder in your repository, for example
local-nuget-feed, and place the required.nupkgfiles there. - Keep a
nuget.configfile that includes bothnuget.organd the local folder source. - In your Dockerfile, copy both the local feed folder and
nuget.configbefore runningdotnet restore.
Example Dockerfile snippet:
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
# Copy local packages and NuGet configuration first
COPY local-nuget-feed/ /opt/local-nuget-feed/
COPY nuget.config ./nuget.config
# Copy project files and restore using the local feed path in nuget.config
COPY . .
RUN dotnet restore --configfile ./nuget.config
RUN dotnet publish -c Release -o /app/out
In this scenario, set the local source in nuget.config to /opt/local-nuget-feed.
Notes
- If you do not want to store
.nupkgfiles in the repository, generate or copy them intolocal-nuget-feedas part of your CI pipeline beforedocker build. - This pattern improves reliability for container builds in environments with unstable access to private feeds.
Multi-Stage Docker Example (Smaller Runtime Image)
This version keeps SDK tools and local package files in build stages only, so the final runtime image is smaller.
# Restore stage
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS restore
WORKDIR /src
COPY local-nuget-feed/ /opt/local-nuget-feed/
COPY nuget.config ./nuget.config
COPY . .
RUN dotnet restore --configfile ./nuget.config
# Publish stage
FROM restore AS publish
RUN dotnet publish -c Release -o /app/out --no-restore
# Runtime stage
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
COPY --from=publish /app/out .
ENTRYPOINT ["dotnet", "YourApp.dll"]
Replace YourApp.dll with your real application assembly name.
Why This Helps
- Better reliability: restore still works from the local NuGet source.
- Smaller image: the final stage does not include the SDK or local
.nupkgfiles. - Faster deployments: smaller runtime layers are typically quicker to push and pull.