Docker build sends massive context — slow builds and out of memory
Sending build context to Docker daemon X.XXMB/GB takes minutes
Focused incident mode — strips away everything but this runbook.
Symptoms
- Sending build context to Docker daemon X.XXMB/GB takes minutes
- docker build hangs at 'Sending build context to Docker daemon'
- Build fails with out of memory or context deadline exceeded
Docker sends the ENTIRE build context to the daemon before executing any Dockerfile instruction. Even a single large file in the root directory bloats every build.
Evidence to grab
Collect these artifacts before you change anything — they prove (or rule out) each cause.
Build context size
du -sh . | head -1Missing .dockerignore
ls -la .dockerignore 2>/dev/null || echo 'No .dockerignore file'
Diagnostic flow
Run the steps in order, marking each claim confirmed or refuted. The evidence you gather feeds the ranked causes below.
Guided decision tree is ready.
Steps with commands, expected output, and pass/fail branches.
Ranked root causes
Evidence checklist
- Context size is > 500MB with node_modules or .git present
- .dockerignore does not exist or is empty
- Sending build context takes > 30 seconds
Confirm before fixing
cat .dockerignore 2>/dev/null || echo 'No .dockerignore'Read the dockerignore file
Fix
echo -e 'node_modules\n.git\n*.log\ndist\nbuild' > .dockerignoreCreate a .dockerignore excluding large and irrelevant directories
Resolution runbook
Apply from top to bottom after the cause is confirmed. Review every command before running it — risk tags are shown on each.
du -sh . --exclude=.git --exclude=node_modules 2>/dev/null || du -sh .Measure the build context
cat .dockerignore 2>/dev/null || echo 'No .dockerignore'Check for dockerignore
echo -e 'node_modules\n.git\n*.log\ndist\nbuild' > .dockerignoreCreate a proper .dockerignore
docker build -f src/Dockerfile src/Narrow the build context to the app directory
docker build --no-cache --progress=plain . 2>&1 | head -5Verify the new context size
Prevention
- Always include a comprehensive .dockerignore in every project.
- Build from the narrowest possible directory (docker build -f path/Dockerfile path/).
- Use multi-stage builds to keep the final image small even if the context is large.
- Automate .dockerignore checks in CI: warn if context > 100MB.
Related runbooks
Same stack, same failure family — worth a skim.
docker build fails at RUN apt-get install with 'Could not resolve archive.ubuntu.com'
docker history shows a COPY or ARG containing a secret in an early layer
Commands marked READ CHANGE DESTRUCTIVE — never run DESTRUCTIVE commands against production data you cannot restore. Copy → verify → execute.