I am trying to start my .net core web api on container tech. using docker.
Environments=Windows 10,Visual Studio
Docker version:
Clien
You can add the private nuget through dotnet command, without the need to link to nuget.config file.
COPY *.csproj ./
RUN dotnet nuget add source <source-value-of-nuget> -n <name>
RUN dotnet restore
In order for a dotnet
command running inside a container to find your custom feeds, the nuget.config
file must also be copied to the container.
To do this, add a nuget.config
file with your private feed to your project folder and add an additional COPY
step that copies this file to the container.
Example (Dockerfile):
WORKDIR ...
COPY NuGet.Config /
COPY ... ...
for those who have landed here as they were using private repositories or custom nuget feeds and RUN dotnet restore is failing ,then here is what you can do :
Applicable especially if : your NuGet.Config contains the private repo endpoint and credentials , then
1) copy your system's NuGet.Config in project folder at same root level where .csproject is.
2) now in docker file put these statements just before you try to restore package:
COPY ./NuGet.Config ./
3) after that , append the config file location in dotnet restore command like this :
RUN dotnet restore <CS_project_name>.csproj --configfile ./NuGet.Config
4) Now do rest of the thing which you wanted to do .
5) just at the end before entry point or before copying to other container(in case of multistage build) , it is good idea to remove NuGet.Config,as we don’t want that to be available in pod/container to be seen
RUN rm ./NuGet.Config
Stumbled across this while looking for another nuget question/answer...
To restore from within docker, as mentioned in this answer copy a nuget.config file to the container first, I'd suggest you place this in /root/.nuget/NuGet
folder and use a multistage Dockerfile, that way, you don't need to worry about removing it.
If it helps, this is my setup...
--nuget.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="PrivateFeed" value="https://private.nuget.feed.net/" />
</packageSources>
<packageSourceCredentials>
<PrivateFeed>
<add key="Username" value="username" />
<add key="ClearTextPassword" value="plaintext_password" />
</PrivateFeed>
</packageSourceCredentials>
</configuration>
Multi-stage Dockerfile...
FROM mcr.microsoft.com/dotnet/core/sdk:3.1.201 as sdk-build
WORKDIR /app
# Skip extraction of XML documentation for nuget packages
ENV NUGET_XMLDOC_MODE=skip
# We'll copy the nuget config file to the right place
# which should mean it doesn't get copied to our deployed containers
# and we can just call 'dotnet restore' with specifying a config file
COPY ./Nuget.Config /root/.nuget/NuGet/NuGet.Config
# Make use of cached layers, by only doing a restore when the config changes
COPY ./*.sln ./**/*.csproj ./
# Then within a RUN command to copy them to the right folders.
RUN for file in $(ls *.csproj); do mkdir -p ${file%.*}/ && mv $file ${file%.*}/; done \
&& dotnet restore
# Copy the source code
COPY . .
# Now do a build/publish here
RUN dotnet publish "./project/project.csproj" --output /app/output
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.2
WORKDIR /app
COPY --from=sdk-build /app/output ./
ENTRYPOINT ["./project"]
Copying the Nuget.Config
to the solution or project folder will work if your 'private nuget feed' is accessible via a url.
But if the private feed is a local folder used as a nuget source, this approach will still fail with an error that the folder is outside the build context or simply because the Windows path does not get resolved by the Docker build process.
e.g. if you Nuget.Config
has something like this:
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="My Local" value="C:\dev\nuget-packages" />
</packageSources>
the Docker context will not resolve C:\dev\nuget-packages
It's tempting to just give up on building within docker and just pre-publish the compiled solution and build the image from that...
But another workaround, requiring a few more steps, is also possible:
run dotnet restore
before running the docker-compose
command, and use the --packages
option to save the restored packages to the solution folder
e.g.
dotnet-restore C:\slnfolder\myproj\myapp.csproj --packages C:\slnfolder\packages
(This could be wrapped in a single powershell script along with the docker-compose
command.)
Then in the Dockerfile (used by docker-compose
to build the image), assuming context is the solution folder and WORKDIR
is '/src'
COPY packages/. packages/.
and modify the Dockerfile restore line to
RUN dotnet restore "myproj/myapp.csproj" -s /src/packages -s https://api.nuget.org/v3/index.json