Skip to content

Docker "failed to compute cache key: not found" Error

Problem Overview

When building Docker images, developers often encounter the frustrating error "failed to compute cache key: not found" even when the Dockerfile and related files appear correct. This error typically occurs during the COPY instruction in Dockerfiles when Docker cannot locate the specified files or directories.

The most common scenario:

  • Docker builds successfully in Visual Studio
  • Command line builds fail with "failed to compute cache key" errors
  • Subsequent build errors like "Program does not contain a static 'Main' method" may appear

Root Causes and Solutions

1. Incorrect Build Context Path

The most common cause is running docker build from the wrong directory. Docker uses the build context (the path you specify at the end of the command) to resolve file paths in COPY instructions.

WARNING

Visual Studio builds Docker images differently than the command line

Solution: Build from the solution directory, not the project directory

bash
# Incorrect (from project folder)
cd project-folder
docker build .

# Correct (from solution folder)
cd solution-folder
docker build -f ProjectFolder/Dockerfile .

2. .dockerignore File Conflicts

The .dockerignore file may exclude files that your Dockerfile tries to copy, causing the "not found" error.

Solution: Check and modify your .dockerignore file:

dockerfile
# Example .dockerignore that might cause issues
*
!dist/
!nginx.conf

Ensure that files referenced in COPY commands are not excluded by your .dockerignore patterns.

3. File Path Case Sensitivity

Docker is case-sensitive, even on Windows systems when using Linux containers.

Solution: Verify exact filename matches:

dockerfile
# Ensure this matches your actual filename exactly
ENTRYPOINT ["dotnet", "MyApp.dll"]  # Not "myapp.dll" or "MYAPP.DLL"

4. Incorrect COPY Syntax

Using wrong path separators or relative paths can cause issues.

Solution: Use consistent path syntax:

dockerfile
# Use forward slashes consistently
COPY bin/Release/net5.0/publish .   # ✓ Correct
COPY bin\Release\net5.0\publish .   # ✗ Incorrect (on Linux containers)

File permissions or symbolic links might prevent Docker from accessing files.

Solution: Check for symlinks in your project and ensure Docker has read access to all necessary files.

Complete Working Example

Here's a properly structured Dockerfile and build command:

dockerfile
FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

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

FROM build AS publish
RUN dotnet publish "ProjectName.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ProjectName.dll"]
bash
# From solution directory (contains .sln file)
docker build -f ProjectName/Dockerfile -t your-image-name .

Advanced Scenarios

Azure DevOps Pipelines

When using Azure DevOps, explicitly set the build context:

yaml
- task: Docker@2
  displayName: Build an image
  inputs:
    command: build
    buildContext: $(Build.SourcesDirectory)
    Dockerfile: '**/Dockerfile'

Directory Structure Best Practices

Visual Studio generates Dockerfiles that expect this structure:

SolutionFolder/
├── Solution.sln
├── ProjectFolder/
│   ├── Dockerfile
│   ├── Project.csproj
│   └── Program.cs
└── OtherProjects/

Troubleshooting Steps

  1. Check build context: Run docker build from the correct directory
  2. Verify .dockerignore: Ensure needed files aren't excluded
  3. Restart Docker: Sometimes a simple restart resolves caching issues
    bash
    docker system prune  # Warning: removes all unused data
  4. Check file names: Verify exact case matching
  5. Inspect build output: Use detailed logging to identify the exact failure point

WARNING

docker system prune removes all unused containers, networks, images, and volumes. Use with caution.

Common Mistakes to Avoid

  • Running docker build from the project directory instead of solution directory
  • Case mismatches in filenames between Dockerfile and actual files
  • Forgetting the trailing . in docker build commands
  • Having conflicting patterns in .dockerignore files
  • Using backslashes in paths for Linux containers

Conclusion

The "failed to compute cache key: not found" error typically stems from mismatches between Docker's build context and the paths specified in your Dockerfile. By understanding how Docker resolves file paths and following the pattern Visual Studio uses (building from the solution directory with the -f flag), you can resolve this issue and successfully build your Docker images from the command line.

Remember to always check your .dockerignore file, verify filename cases, and ensure you're running commands from the correct directory to avoid this common Docker pitfall.