If you’re encountering the error:
failed to compute cache key: not found
while building a Docker image manually (e.g., using docker build
), but everything works fine in Visual Studio, you’re not alone. This error can be puzzling because it often appears out of nowhere—even when the Dockerfile and context appear to be correct.
In this blog post, we’ll explore why this error happens, why it doesn’t show up in Visual Studio, and how to fix it in your manual builds.
❓ What Does This Error Mean?
The full error usually looks something like this:
failed to compute cache key: not found
It indicates that Docker cannot find a file or directory specified in the COPY
or ADD
instructions in your Dockerfile
.
This happens before Docker even builds the image—during the caching and context preparation phase.
✅ Example That Triggers the Error
Let’s say your Dockerfile
includes:
COPY MyApp.csproj ./MyApp.csproj
But when you run:
docker build -t myapp .
You get:
failed to compute cache key: not found
That means Docker couldn’t find MyApp.csproj
relative to the build context (i.e., the .
directory you passed in).
🤔 Why It Works in Visual Studio
Visual Studio uses Docker Tools and MSBuild integration, which:
- Sets the correct build context automatically
- Performs file copies and preprocessing steps before building
- May generate
.dockerignore
and.docker/config.json
files that help resolve paths
This explains why the build works from inside Visual Studio but fails from the terminal.
🛠️ How to Fix It
Here are the most common fixes:
✅ 1. Double-check Paths in Your Dockerfile
Make sure all COPY
and ADD
commands reference files relative to the build context.
Bad:
COPY ./src/MyApp/MyApp.csproj ./MyApp.csproj
Good (if you run from the root project folder):
COPY MyApp.csproj ./MyApp.csproj
Or change your terminal build context to match:
cd src/MyApp
docker build -t myapp .
✅ 2. Set the Correct Build Context
The build context is defined by the last .
(or path) in the docker build
command:
docker build -t myapp .
If you’re in the wrong folder, Docker won’t find files referenced in COPY
.
✅ Make sure you are running the command from the folder that contains the files referenced in the Dockerfile.
✅ 3. Watch for .dockerignore
Exclusions
Check if your file is listed (or matched by a pattern) in .dockerignore
.
cat .dockerignore
If a file or folder is ignored here, it won’t be sent to Docker during build—causing a “not found” error.
✅ 4. Use Correct File Casing (especially on Windows)
Windows is case-insensitive; Linux and Docker often are not.
COPY myapp.csproj ./MyApp.csproj # May fail due to casing
Always match the file’s actual casing.
🧠 Summary
Problem Area | Solution |
---|---|
File not found | Check COPY paths relative to context |
Wrong folder | Run docker build from the correct directory |
File ignored | Check .dockerignore |
File name casing | Match file name exactly |
Works in VS only | Visual Studio sets its own context, handle it manually when using CLI |
✅ Final Thoughts
The error "failed to compute cache key: not found"
usually means there’s a problem with your file paths, not Docker itself. Visual Studio often “masks” these issues by automating path setup behind the scenes.
If you’re switching to manual builds in the terminal or CI/CD pipeline, make sure:
- You use the right build context
- All files exist where expected
.dockerignore
isn’t excluding necessary files
Following these best practices will help you avoid build-time headaches and align your local and automated Docker workflows more smoothly.