How to Free Disk Space Before a Docker Build in CI
GitHub-hosted runners ship many preinstalled SDKs; removing the ones you do not need frees space for a big image build.
Large image builds can exhaust the runner disk and fail with no space left on device. Removing unused toolchains (or pruning the Docker cache) before the build reclaims several gigabytes.
Steps
- Add a cleanup step before the build (a maintained action or manual
rm). - Remove large unused directories such as Android SDK, .NET, and GHC.
- Optionally run
docker system prune -afto clear stale build cache.
Workflow
.github/workflows/docker.yml
steps:
- name: Free disk space
run: |
sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc
sudo docker system prune -af
df -h /
- uses: docker/build-push-action@v6
with:
push: false
tags: app:ciGotchas
- Deleting a toolchain a later step needs will break that step; only remove what this job will not use.
- Print
df -h /after cleanup so logs show how much space the build actually had.
Related guides
How to Reduce Docker Image Size With a Multi-Stage Build in CICut Docker image size with a multi-stage build that compiles in a builder stage and copies only the runtime a…
How to Cache Docker Layers With a Registry Cache in CIPersist Docker build layers across CI runs with Buildx registry cache (type=registry), pushing and pulling ca…