Solution:
Root Cause: Unignored Node.js Dependencies and Build Artifacts
When
docker build executes without an explicit
.dockerignore entry for local dependency trees, the client tarballs the entire workspace directory—including
node_modules, local
.next or
.dist cache folders, and active log files—before sending it to the Docker daemon. In Node.js projects, a single
node_modules directory can contain tens of thousands of small files, generating massive file IO overhead during archiving.
# Diagnostic Verification:
Execute the following command in your terminal to calculate the uncompressed size of your local dependencies:
bash
du -sh node_modules .next .git coverage 2>/dev/null
If these directories account for hundreds of megabytes or gigabytes, they are being packed into the build context payload.
# Step-by-Step Fix:
1.
Create or Update .dockerignore:
Ensure a file named .dockerignore resides in the exact root directory where your Dockerfile and build context are referenced.Add the following rules: gitignore
**/node_modules
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.next/
.nuxt/
dist/
coverage/
.git
.github
2.
Enable BuildKit Engine:
BuildKit evaluates source files on-demand instead of tarballing the full directory up front.Export the environment variable or set it permanently in your shell: bash
export DOCKER_BUILDKIT=1
docker build -t my-node-app:latest .
3.
Verify Context Size Reduction:
Run docker build . and verify the transfer phase completes in milliseconds rather than minutes.# Prevention & Long-Term Monitoring:
Enforce project-level .dockerignore templates via CI/CD linting tools.Always leverage multi-stage Dockerfiles so dependencies are resolved inside isolated build stages rather than copied from local host disk environments.