Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

docker build cannot find a local file or log in to a private NuGet feed

Error"/main.c": not found

Short 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.

  • Applies to .NET SDK 9, Docker BuildKit 27, NuGet 6
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 .
  2. 2Match what you see, then jump to its fix

    1. 1COPY fails with a not found error for a path that exists on your diskThe file is outside the build contextFix
    2. 2The file is inside the context but COPY still reports it missing.dockerignore excludes the fileFix
    3. 3dotnet restore fails for the private source while it works on your machineThe private feed needs credentials that the build does not haveFix
    4. 4The parameter is incorrectA plain-text password is stored in the Password fieldFix

What does the error look like?

  • "/main.c": not found
  • The 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/Api

2Fix

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

Did this fix it?

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 *.dockerignore

2Fix

Remove the pattern that hides the file, or re-include it with a ! line. The last matching line decides.

*.config
!nuget.config

3Verify

Rebuild and check the COPY step passes.

docker build --progress=plain .

Confirmed by docs.docker.com

Did this fix it?

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 source

2Fix

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 clean

Confirmed by docs.docker.com

Did this fix it?

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.config

2Fix

Use the ClearTextPassword key with an environment variable, as in the previous cause.

<add key="ClearTextPassword" value="%FEED_TOKEN%" />

3Verify

Run restore again.

dotnet restore

Confirmed by learn.microsoft.com

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.