docker build cannot find a local file or log in to a private NuGet feed
"/main.c": not foundShort answer
A Docker build sees only the build context, minus anything in .dockerignore, so files outside it are not found. Private feeds need credentials inside the build: put the nuget.config in the context, read the password from an environment variable, and pass it as a build secret, not as ARG or ENV.
Fix it fast
1Find out which cause you have
Print the context you send and what .dockerignore hides. A file that is not in the context is invisible to COPY, whatever your disk has.
cat .dockerignore docker build --progress=plain -f Dockerfile .2Match what you see, then jump to its fix
- 1
COPY fails with a not found error for a path that exists on your diskThe file is outside the build contextFix - 2
The file is inside the context but COPY still reports it missing.dockerignore excludes the fileFix - 3
dotnet restore fails for the private source while it works on your machineThe private feed needs credentials that the build does not haveFix - 4
The parameter is incorrectA plain-text password is stored in the Password fieldFix
- 1
What does the error look like?
"/main.c": not foundThe parameter is incorrect
Cause 1 of 4
The file is outside the build context
1Check
COPY and ADD resolve paths against the context root, the directory you pass at the end of docker build. A file in a parent or sibling folder cannot be reached.
docker build -f src/Api/Dockerfile src/Api2Fix
Pass a parent directory as the context and keep -f pointing at the Dockerfile. Write COPY paths relative to that parent.
# run from the solution root
docker build -f src/Api/Dockerfile .
# Dockerfile
COPY nuget.config ./
COPY src/Api/Api.csproj src/Api/3Verify
The build moves past the COPY step.
docker build --progress=plain -f src/Api/Dockerfile .Confirmed by docs.docker.com
Cause 2 of 4
.dockerignore excludes the file
1Check
Excluded files cannot be copied with ADD, COPY or bind mounts. A Dockerfile-specific ignore file named like Dockerfile.dockerignore next to the Dockerfile overrides the root .dockerignore.
cat .dockerignore
ls *.dockerignore2Fix
Remove the pattern that hides the file, or re-include it with a ! line. The last matching line decides.
*.config
!nuget.config3Verify
Rebuild and check the COPY step passes.
docker build --progress=plain .Confirmed by docs.docker.com
Cause 3 of 4
The private feed needs credentials that the build does not have
1Check
Credentials in your user-level NuGet.Config live on your machine, not in the image. NuGet reads solution-level nuget.config files from the project folder up to the drive root, and values may use %VAR% environment variables.
dotnet nuget list source2Fix
Commit a nuget.config with the source and a %VAR% password, copy it into the build, and feed the value in as a BuildKit secret mounted as an environment variable for that one RUN. Do not use ARG or ENV, because they persist in the image.
<!-- nuget.config -->
<configuration>
<packageSources>
<add key="Contoso" value="https://nuget.contoso.com/v3/index.json" />
</packageSources>
<packageSourceCredentials>
<Contoso>
<add key="Username" value="build-user" />
<add key="ClearTextPassword" value="%FEED_TOKEN%" />
</Contoso>
</packageSourceCredentials>
</configuration>
# Dockerfile
COPY nuget.config ./
RUN --mount=type=secret,id=feed_token,env=FEED_TOKEN dotnet restore
# build command
docker build --secret id=feed_token,env=FEED_TOKEN .3Verify
Restore finishes and the secret is not in the image history.
docker history --no-trunc myapp | grep -i feed_token || echo cleanConfirmed by docs.docker.com
Cause 4 of 4
A plain-text password is stored in the Password field
1Check
In nuget.config the Password key holds an encrypted value, and encryption works only on Windows for the same user and machine. A readable password in that key is rejected.
grep -n "Password" nuget.config2Fix
Use the ClearTextPassword key with an environment variable, as in the previous cause.
<add key="ClearTextPassword" value="%FEED_TOKEN%" />3Verify
Run restore again.
dotnet restoreConfirmed by learn.microsoft.com
How do I stop it coming back?
- Keep one nuget.config at the repository root so local and Docker builds read the same sources.
- Never put tokens in ARG, ENV or a committed file; pass them as build secrets.
- Run docker build from the solution root and keep COPY paths relative to it.
- Keep .dockerignore to build output and secrets only.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- RaheebA consumer app shipped to both stores from one Flutter codebase on a .NET and Azure back end.
Related fixes
Servers
dotnet restore fails with NU1301 inside docker build
NU1301: The local source 'C:\Code\Contoso\contosoLocalSource' doesn't exist.
NU1301 means restore could not use one of the configured package sources.
3 causes, each with a fixServers
A containerized ASP.NET Core app says the framework Microsoft.AspNetCore.App was not found
Framework: 'Microsoft.AspNetCore.App', version '6.0.0' (x64)
The app asks for a shared framework the final image does not have.
3 causes, each with a fixServers
ASP.NET Core container does not answer on the published port
The container is running and logs no error, but the mapped host port never answers
Since .NET 8 the ASP.NET Core container images listen on port 8080, not 80.
3 causes, each with a fixServers
docker build fails with CS5001, no entry point found
error CS0017: Program has more than one entry point defined.
CS5001 means the compiler built an executable project and found no entry point.
4 causes, each with a fixServers
MSB1011 when dotnet build or publish runs inside a Docker build
dotnet build or dotnet publish fails in a RUN step of the Dockerfile
dotnet build and dotnet publish pick a project by searching the current folder.
3 causes, each with a fixServers
dotnet says it cannot find any installed .NET SDKs (in Docker or on a server)
A compatible .NET SDK was not found
The dotnet command found a runtime but no SDK that fits.
3 causes, each with a fix