The "build cancelled" or "context canceled" error occurs when Docker terminates a build process prematurely. This typically happens due to large build contexts, file locking issues, or network interruptions.
The "build cancelled" error in Docker indicates that the build process was interrupted before completion. This can happen for several reasons: Docker BuildKit may cancel parallel build steps that become unnecessary due to cache hits, the build context may be too large causing Docker to timeout, files in the build context may be locked by other applications, or network issues may interrupt downloads during the build. When you see this error, Docker has terminated the build and no image will be created. The error message variants include "context canceled", "Failed to solve: Canceled: context canceled", and "CANCELED [frontend internal] load build context". Understanding the specific cause requires examining what was happening when the cancellation occurred.
Add a .dockerignore file in the same directory as your Dockerfile to exclude large directories:
node_modules/
.git/
.venv/
.idea/
*.log
dist/
build/
.npm/
__pycache__/This prevents Docker from copying unnecessary files into the build context, significantly reducing transfer time and memory usage.
IDEs like VS Code, IntelliJ, or RStudio may lock files during indexing or compilation. Close these applications before running the build:
# After closing your IDE, run the build from a separate terminal
docker build -t myimage .If you have a development server running, stop it before building.
Corrupted or stale cache can cause build issues. Clear the builder cache:
docker builder prune --allThis removes all build cache. Add the -f flag to skip the confirmation prompt.
Test if you can pull the base image directly:
docker pull <your-base-image>If this fails, you may have network issues. Try:
- Disconnecting from VPN
- Checking proxy settings
- Flushing DNS cache: ipconfig /flushdns (Windows) or sudo systemd-resolve --flush-caches (Linux)
On Windows, running Docker build from a different drive than where your project is located can cause issues:
# Navigate to your project directory first
cd D:\Projects\myapp
# Then run the build
docker build -t myimage .Always run the build command from within the project directory.
If builds fail during memory-intensive operations (like compiling), increase Docker's memory limit:
Docker Desktop:
1. Open Docker Desktop Settings
2. Go to Resources > Advanced
3. Increase Memory allocation
4. Apply & Restart
Linux with systemd:
sudo systemctl edit docker.serviceAdd memory limits as needed for your system.
BuildKit may show "CANCELED" for parallel steps that become unnecessary due to cache hits. This is normal behavior:
#8 CANCELED [stage-1 2/3] RUN npm install
#9 DONE [stage-1 2/3] COPY --from=cache /app/node_modules ./node_modulesIf the build completes successfully and produces an image, you can safely ignore these CANCELED messages.
When using BuildKit (default in Docker 23.0+), parallel step execution means you may see "CANCELED" status for steps that were superseded by cache hits. This is an optimization, not an error. However, if the entire build fails with "context canceled", check your build context size with before building. For CI/CD pipelines, ensure your runner has adequate memory and network connectivity. In Kubernetes environments, resource limits on the builder pod may need adjustment. For complex multi-stage builds, consider using to build specific stages and identify which stage causes the cancellation.
dockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec