Home » [FIXED] Error 'failed to compute cache key' in Docker Build
Systems & Servers

[FIXED] Error 'failed to compute cache key' in Docker Build

Quick Diagnostics

The error failed to compute cache key: failed to walk: lstat ...: no such file or directory during docker build occurs when a COPY or ADD instruction in your Dockerfile references a local file or directory that does not exist within the Docker build context.

Quick Solution (1 Minute):

  1. Verify the source file path exists inside the directory where you run docker build .
  2. Check if the file is ignored in .dockerignore.
  3. Specify the root context explicitly when building from subfolders: docker build -f docker/Dockerfile .

🚀 Step-by-Step Troubleshooting

Step 1: Understand the Docker Build Context

When you execute docker build -t my-image ., the trailing dot . defines the build context. Docker packages and sends files in that directory to the Docker daemon.

If your Dockerfile has:

DOCKERFILE
COPY package.json ./

Targeting a file outside the current working folder will cause failed to compute cache key.

Fix: Move to your project root or adjust your build command context:

BASH
docker build -t my-app:latest -f ./docker/Dockerfile .

Step 2: Check .dockerignore Exclusions

Open the .dockerignore file in your root folder. If an ignored rule matches a file targeted by COPY:

PLAINTEXT
src/config.json

Docker excludes it from the context, throwing the failed to compute cache key error.

Fix: Remove the rule or add an exception prefix !:

PLAINTEXT
!src/config.json

Step 3: Check File Case Sensitivity (Linux)

Linux filesystems are case-sensitive. If your file is named App.js but your Dockerfile has:

DOCKERFILE
COPY app.js ./

Docker fails to locate app.js. Update the filename to match exact capitalization.

Prevention Advice

Recommended security practices: