mediumFoundationSymptom-first#build#context#dockerignore#performance

Docker build sends massive context — slow builds and out of memory

Sending build context to Docker daemon X.XXMB/GB takes minutes

Likely source:performanceconfiguration

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 -1
  • Missing .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

Read the dockerignore file

Fix

echo -e 'node_modules\n.git\n*.log\ndist\nbuild' > .dockerignore
CHANGE

Create 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.

1
du -sh . --exclude=.git --exclude=node_modules 2>/dev/null || du -sh .
READ

Measure the build context

2
cat .dockerignore 2>/dev/null || echo 'No .dockerignore'
READ

Check for dockerignore

3
echo -e 'node_modules\n.git\n*.log\ndist\nbuild' > .dockerignore
CHANGE

Create a proper .dockerignore

4
docker build -f src/Dockerfile src/
CHANGE

Narrow the build context to the app directory

5
docker build --no-cache --progress=plain . 2>&1 | head -5
READ

Verify 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.

Commands marked READ CHANGE DESTRUCTIVE — never run DESTRUCTIVE commands against production data you cannot restore. Copy → verify → execute.