GitHub Actions Docker build times out pushing large build context because .cache and node_modules are included
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
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
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
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
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?
Verified community troubleshooting
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.
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Software35% match
Self-hosted GitHub Actions runners produce incomplete multi-stage Docker images after kernel update (overlay2: missing build artifacts)
Open0 answers - Programming27% match
CI Gradle build fails with 'duplicate classes' intermittently in Docker-in-Docker runners due to shared Gradle cache corruption
Solved1 answer - Programming24% match
Git LFS pointers committed to main branch after GitHub Actions checkout lacked LFS pull: large files missing in CI and deployed artifact
Open0 answers - Cloud Computing24% match
Self-hosted GitHub Actions runner cannot access /var/run/docker.sock when launched as systemd service
Solved1 answer