Goal: Understand what the heck is eating up your large Docker build context! Then, use that to update your .dockerignore and improve the performance of your builds.
You can add the DebugDockerContext.prompt.md file to .github/prompts folder and then run the prompt with GitHub Copilot. It will use this first method and analyze your context directory structure to summarize the results.
Based on this SO question, here's what worked for me with Docker Engine v27.4.0 on WSL2 and Windows 11.
In the directory with your .dockerignore:
printf 'FROM busybox\nCOPY . /tmp/build/' | docker build -t test -f- .
docker run --rm -it test du -xhd1 /tmp/buildWhat this does:
- Builds a
testDocker image (using latest Docker Desktop/Buildkit) - Copies the context to
/tmp/buildinside the image - Runs the
testimage (removing immediately) - Outputs the disk usage with folder sizes
It will output something like:
240.0K /tmp/build/docs
1.2M /tmp/build/Patched
1020.0K /tmp/build/TestResults
4.0K /tmp/build/web.blazor
12.0K /tmp/build/debug
40.0K /tmp/build/.github
20.0K /tmp/build/docker
28.3M /tmp/build/src
228.0K /tmp/build/Backup
34.8M /tmp/buildYou can then tweak it each time to go deeper if needed, e.g.
docker run --rm -it test du -xhd1 /tmp/build/src
This might be nice if you have a huge file structure, which was my case. You also don't need to have Docker Engine installed but you will need Golang and PowerShell.
Use along with docker-build-context-ls to pipe and get a tree of folders sorted by largest size first.
# Run this in your .dockerignore directory
docker-build-context-ls . | .\Summarize-DockerBuildContext.ps1You should see something like this output:
. [27.96 MB]
src [21.72 MB]
Web [15.61 MB]
Content [11.26 MB]
videos [8.44 MB]
images [1.76 MB]
articles [0.82 MB]
tiles [0.11 MB]
sprites [0.03 MB]
hopscotch [0.01 MB]
icons [0.01 MB]
ajax [0 MB]Mostly written by ChatGPT with my direction 🤖
I'm sure you can prompt your AI to convert this to Python, Bash, Node.js, whatever you want. Have fun!