This error occurs when Docker Compose cannot build one of your services. The root cause is usually a Dockerfile syntax error, missing files in the build context, incorrect paths, or insufficient system resources like memory or disk space.
When you run `docker-compose build` or `docker-compose up --build`, Docker Compose reads your `docker-compose.yml` file and attempts to build images for any services that have a `build:` configuration. The "Service failed to build" error is a wrapper message that indicates the underlying Docker build process failed. The actual root cause is hidden in the lines above this error message. Docker Compose surfaces this generic error after the specific build failure occurs. Common underlying issues include: - **Dockerfile errors**: Syntax mistakes, invalid commands, or typos in your Dockerfile - **Missing files**: Files referenced by COPY or ADD commands that don't exist in the build context - **Resource constraints**: Insufficient memory allocated to Docker or no disk space remaining - **Network issues**: Failed package downloads during the build process
The "Service failed to build" message appears at the end, but the actual error is in the preceding output. Scroll up in your terminal to find lines like:
Step 5/10 : COPY package.json .
COPY failed: file not found in build contextor
E: Unable to locate package some-package
The command '/bin/sh -c apt-get install some-package' returned a non-zero code: 100This specific error tells you exactly what went wrong.
Check your Dockerfile for common syntax issues:
# WRONG - RUN on same line as package name
RUN apt-get install -y python3 RUN pip install flask
# CORRECT - Each RUN is a separate command
RUN apt-get install -y python3
RUN pip install flaskAlso verify that:
- Each instruction (FROM, RUN, COPY, etc.) is on its own line
- Multi-line commands use \ for line continuation
- There are no invisible characters or encoding issues
Files in COPY and ADD commands must exist relative to the build context. In docker-compose.yml:
services:
app:
build:
context: ./my-app # Build context directory
dockerfile: Dockerfile # Relative to contextIf your Dockerfile contains COPY package.json ., then package.json must exist in ./my-app/.
Run this to verify:
ls -la ./my-app/package.jsonIf you have a .dockerignore file, it might be excluding files your build needs:
cat .dockerignoreCommon problematic patterns:
# This excludes ALL files starting with "."
.*
# This might exclude needed config files
*.jsonTemporarily rename or remove .dockerignore to test if it's the cause.
If the build fails with exit code 137 or mentions memory issues:
Docker Desktop (Windows/Mac):
1. Open Docker Desktop Settings
2. Go to Resources > Advanced
3. Increase Memory to at least 4GB
4. Click "Apply & Restart"
Linux:
Check available memory:
free -h
docker info | grep -i memoryIf low, close other applications or add swap space.
Stale caches or low disk space can cause build failures:
# Remove unused containers, networks, images
docker system prune -a
# Check disk space
docker system df
# Force rebuild without cache
docker-compose build --no-cacheIf disk space is critically low, consider removing old images:
docker image prune -aBuild the image directly with Docker to get more detailed output:
# Navigate to the build context directory
cd ./my-app
# Build with verbose output
docker build -t test-build . 2>&1 | tee build.logThis often provides clearer error messages than docker-compose.
Build context size: If your build seems to hang before starting, you may have a large build context. Docker sends the entire context directory to the daemon before building. Add a .dockerignore file to exclude node_modules, .git, and other large directories:
node_modules
.git
*.log
dist
buildMulti-stage builds: For complex applications, use multi-stage builds to reduce final image size and isolate build-time dependencies:
FROM node:18 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:18-slim
WORKDIR /app
COPY --from=builder /app/dist ./dist
CMD ["node", "dist/index.js"]Platform-specific issues: If building on Mac M1/M2 for x86 deployment, specify the platform:
services:
app:
build:
context: .
platform: linux/amd64Debugging failed layers: To inspect where the build failed, add temporary debug steps:
RUN ls -la /app # See what files exist at this point
RUN cat /app/package.json # Verify file contentsimage operating system "linux" cannot be used on this platform
How to fix 'image operating system linux cannot be used on this platform' in Docker
manifest unknown: manifest unknown
How to fix 'manifest unknown' in Docker
cannot open '/etc/passwd': Permission denied
How to fix 'cannot open: Permission denied' in Docker
Error response from daemon: failed to create the ipvlan port
How to fix 'failed to create the ipvlan port' in Docker
toomanyrequests: Rate exceeded for anonymous users
How to fix 'Rate exceeded for anonymous users' in Docker Hub