Cloud Computing
VERIFIED FIXES

GitHub Actions Docker build times out pushing large build context because .cache and node_modules are included

Environment

Environment

GitHub Actions ubuntu-latest runners; workflow uses actions/checkout@v3 then runs docker build -t myimage .; repository contains large .cache, node_modules, and test artifacts >6GB in workspace; build fails after runner upload step times out or exceeds job storage limits.

Symptoms

Symptoms

CI job stalls at 'Sending build context to Docker daemon' or Actions shows long worker disk upload and then the job times out or fails with 'context exceeded size' or runs out of job minutes. Local builds are fast and small because developer .dockerignore exists but not committed or job uses different working-directory.

Observations

Observations

Workflow logs show docker build context size >5GB. actions/checkout checks out full repository including Git LFS pointers and large artifacts. A previous commit temporarily reduced repo size and job succeeded. Attempts to cache node_modules using actions/cache were misconfigured (cache miss).

Already attempted

Already attempted

Added .dockerignore but forgot to commit it before rerunning; tried actions/cache with wrong key so cache always missed; tried splitting build steps but docker build step still sends large context; increased job timeout temporarily to no avail.

Question

Question

How do I make GitHub Actions' docker build use a small context so the job doesn't time out or exceed runner storage, and how to avoid accidentally committing large caches in future runs?

COMMUNITY RESPONSES

Verified community troubleshooting

1 reply
0
astridapps Answer
user
Posted 4w ago • 0 confirmations

Cut the build context first: commit a .dockerignore that excludes node_modules, .cache, test_artifacts and any large temp folders. Alternatively run docker build from the minimal subdirectory (docker build -f Dockerfile ./app) so the runner only uploads that folder. In Actions, set actions/checkout fetch-depth: 1 to reduce repo size and use actions/cache with a stable key to keep node_modules out of git. Verify by running tar -czf - ./context | wc -c locally to confirm context is small. Roll back by restoring the previous Dockerfile path or .dockerignore if a downstream job breaks; if large files were already committed, coordinate a repo history clean and inform the team.

Showing 1 of 1 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

No committed .dockerignore (or it excludes wrong paths) so large folders are sent in build context 95%
Workflow runs docker build from repo root instead of a subdirectory containing Dockerfile and minimal context 60%
Large generated artifacts tracked in Git (not gitignored) or Git LFS pointers expanded by checkout settings 40%

Summary

The runner sends the full workspace as the build context. Causes include missing or uncommitted .dockerignore, build command run from repo root including large directories, reliance on actions/checkout default behavior (which checks out everything), and large files tracked in git or LFS pointers expanding context. Fixes: commit a restrictive .dockerignore, use build context directory, or use GitHub Actions' path filters or actions/cache properly to avoid sending heavy directories. Avoid storing large generated files in the repo.

Suggested Fixes

Diagnostic step 1
Create and commit a .dockerignore that excludes node_modules, .cache, test-artifacts, logs, and other large folders; verify docker build context size locally with docker build --no-cache - < /dev/null or tar -czf - . | wc -c in workspace.
Diagnostic step 2
Run docker build from the directory that contains only necessary files (use -f path/to/Dockerfile path/to/context) to restrict context.
Diagnostic step 3
Use actions/checkout with fetch-depth: 1 unless history is required, and avoid checking large artifacts into Git; if you must keep them, store in release assets or external storage.

Continue troubleshooting

Matched from the title, description and tags