Fixing Docker Error: “failed to compute cache key: not found” (But It Runs Fine in Visual Studio)

When building Docker images outside of Visual Studio, you may encounter this error:

failed to solve with frontend dockerfile.v0: failed to create LLB definition: failed to compute cache key: not found

Strangely, the same project might build perfectly inside Visual Studio. Why does this happen? Let’s break it down.


1️⃣ Why It Works in Visual Studio but Fails in CLI

Visual Studio integrates with Docker and manages the build context for you. That means it includes the correct files and paths when running docker build.

When you run docker build manually from the command line, you may be missing files or using the wrong build context. Docker can’t find those files, so it throws the “not found” cache key error.


2️⃣ Common Causes of the Error

  1. Wrong build context
    • Example: You run the command from the wrong folder.
    • Dockerfile might expect COPY ./src /app but your current directory doesn’t contain src.
    ✅ Fix: Always run from the project root or specify context: docker build -t myapp -f Dockerfile .
  2. Files outside of Docker’s build context
    • Docker can only access files inside the build context (the directory you specify in the docker build command).
    • If your Dockerfile tries to copy something outside this context, it will fail.
    ✅ Fix: Reorganize your project so needed files are inside the context, or adjust your Dockerfile paths.
  3. .dockerignore excluding files
    • If .dockerignore excludes files that are required in the build, Docker won’t find them.
    ✅ Fix: Check .dockerignore and ensure essential files aren’t being ignored.
  4. Multi-stage builds referencing missing files
    • If you use multi-stage builds, a COPY --from=builder step may fail if the referenced file wasn’t generated in the earlier stage.
    ✅ Fix: Double-check stage names and file paths.

3️⃣ Example: Visual Studio Works, CLI Fails

Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build

If you run:

docker build -t myapp .

from the wrong directory, Docker won’t find MyApp/MyApp.csproj.

✅ Fix: Run it from the solution root:

docker build -t myapp -f MyApp/Dockerfile .

4️⃣ How to Debug

  • Run with verbose output: DOCKER_BUILDKIT=0 docker build . This gives more detailed error messages.
  • Double-check your build context: docker build -t myapp -f Dockerfile <context_directory>
  • Verify file paths inside the container’s build environment.

📌 Summary

The error

“failed to compute cache key: not found”

usually means Docker can’t find a file that the Dockerfile references.

  • Visual Studio fixes this automatically by setting the correct build context.
  • On the CLI, you need to carefully set your build context, check .dockerignore, and validate paths.

👉 Next time you see this, double-check where you’re running docker build from—that’s usually the culprit.

Sharing Is Caring:

Leave a Comment