🔧 Error Fixes
· 2 min read

Docker Build Failed — How to Fix Common Docker Build Errors


ERROR: failed to solve: failed to compute cache key: failed to calculate checksum
COPY failed: file not found in build context
RUN apt-get install: E: Unable to locate package

Docker builds fail for many reasons. Here are the most common ones.

Fix 1: COPY — File Not Found

# ❌ File isn't in the build context
COPY config.json /app/  # 💥 File doesn't exist or is in .dockerignore

# ✅ Check the file exists relative to the Dockerfile
ls config.json

# ✅ Check .dockerignore isn't excluding it
cat .dockerignore

Fix 2: apt-get — Package Not Found

# ❌ Package index is outdated
RUN apt-get install -y curl  # 💥 Unable to locate package

# ✅ Always update first
RUN apt-get update && apt-get install -y curl

Fix 3: npm install Fails

# ❌ Missing package.json in the container
COPY . .
RUN npm install

# ✅ Copy package files first (better caching too)
COPY package*.json ./
RUN npm install
COPY . .

Fix 4: Permission Denied

# ❌ Running as root but files need different permissions
RUN chmod +x /app/start.sh

# ✅ Or switch to a non-root user
RUN adduser --disabled-password appuser
USER appuser

Fix 5: Build Context Too Large

# ❌ Sending entire directory including node_modules
Sending build context to Docker daemon  2.5GB  # Way too big

# ✅ Add a .dockerignore
echo "node_modules
.git
dist
*.log" > .dockerignore

Fix 6: Multi-Stage Build — File Missing

# ❌ File from build stage not copied to final stage
FROM node:20 AS builder
RUN npm run build

FROM node:20-slim
# 💥 Forgot to copy the build output
CMD ["node", "dist/index.js"]

# ✅ Copy from the builder stage
FROM node:20-slim
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]

Debugging

# Build with verbose output
docker build --progress=plain .

# Build without cache (start fresh)
docker build --no-cache .

# Check what's in the build context
docker build . 2>&1 | head -5
📘