简体   繁体   中英

How to use locally produced NuGet in a docker image?

I have a use case where I have a .nupkg file already generated locally on my machine. I want to use this local nuget file in the docker image. Below is how existing docker file looks like -

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /app

RUN mkdir -p /nuget
ADD nuget /nuget/

COPY src/*.csproj .
RUN dotnet restore

COPY src/ .
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS runtime
WORKDIR /app
COPY --from=build /app/out ./
ENTRYPOINT ["dotnet", "EdgeService.dll"]

So in the RUN dotnet restore step it fetches a particular nuget package from nuget.org repository and builds the image. I want a mechanism by which it could use a .nupkg file which I have on my local machine instead of it fetching from nuget server.

Is there some change I could do in the .csproj file or the docker file to make sure that it happens? Is it necessary to setup a local nuget server in this case?

Yes you need a nuget server running somewhere. Local is great, until you want to set up CI and then you need a hosted one. I used a hosted docker container with a nuget server in it before later migrating to GitHub packages.

After a while of running this set up however I found rebuilding packages for every change a pain so I rearchiteced my solution to not need private NuGet packages at all.

Below are the changes I did to get it working -

Dockerfile changes -

RUN mkdir -p /nuget
ADD Nuget /tmp/Nuget

COPY src/*.csproj .
RUN dotnet restore

Changes in the .csproj file -

<RestoreSources>$(RestoreSources);https://api.nuget.org/v3/index.json;/tmp/Nuget</RestoreSources>

And in the Nuget folder I copied the .nupkg file which I wanted to be referenced by the docker image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM