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

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

✨ Quick Answer

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 Diagnostics

Cause
Source path in Dockerfile COPY/ADD instruction does not exist
Solution
Verify exact local file path and check .dockerignore file
Cause
Docker build context pointing to incorrect working directory
Solution
Run docker build ensuring proper trailing . context syntax

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:

Author • Web Designer & App Creator

Rodolfo Castro

Web designer, app developer, and founder of SoporteCero. Specializing in UI/UX architecture, digital products, and modern web environments. Every tutorial and guide on SoporteCero is thoroughly tested and verified in our technical lab to ensure reliable, up-to-date solutions.