ENOMEM: not enough memory
ERROR: failed to solve: process "/bin/sh -c npm run build" did not complete successfully
Killed
Your Docker build or container ran out of memory. The OOM (Out of Memory) killer terminated the process.
Fix 1: Increase Docker Memory Limit
Docker Desktop has a default memory limit (usually 2GB).
Docker Desktop (macOS/Windows): Settings β Resources β Memory β increase to 4GB or more β Apply & Restart
Docker Engine (Linux):
# Check current memory
docker info | grep "Total Memory"
# Run container with more memory
docker run --memory=4g my-image
Fix 2: Node.js Build Running Out of Memory
npm run build (especially Next.js, Webpack) is memory-hungry.
# Increase Node.js heap size in Dockerfile
ENV NODE_OPTIONS="--max-old-space-size=4096"
RUN npm run build
Or in docker-compose:
services:
app:
build: .
environment:
- NODE_OPTIONS=--max-old-space-size=4096
Fix 3: Too Many Parallel Processes
# β npm install runs many processes in parallel
RUN npm install
# β
Limit concurrency
RUN npm install --maxsockets=2
Fix 4: Multi-Stage Build (Reduce Final Image Size)
Build in one stage, copy only the output to a slim final image:
# Build stage
FROM node:20 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage (much smaller)
FROM node:20-slim
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]
Fix 5: Reduce Build Context
A huge build context eats memory. Add a .dockerignore:
node_modules
.git
dist
.next
coverage
*.log
Fix 6: Check Whatβs Using Memory
# Memory usage of running containers
docker stats
# Check if OOM killed a container
docker inspect <container> | grep -i oom
# "OOMKilled": true
# System-wide
docker system df
See also: Node heap out of memory fix and Docker no space left fix