This error occurs when Docker Compose fails to build an image for one of your services. The root cause is typically a Dockerfile issue, missing build context files, incorrect paths in docker-compose.yml, or system resource constraints like insufficient memory or disk space.
When you execute `docker-compose build` or `docker-compose up --build`, Docker Compose reads your `docker-compose.yml` configuration and attempts to build Docker images for any services that specify a `build:` section. The "Service failed to build" error is a high-level message indicating that the underlying Docker build process encountered a failure. The specific error details appear in the output above this message. Docker Compose wraps the actual build error with this generic service failure notification. To diagnose the issue, you need to examine the complete build output, not just the final error line. Common underlying causes include: - **Dockerfile syntax errors**: Invalid instructions, typos, or malformed commands in your Dockerfile - **Missing files**: COPY or ADD instructions referencing files that don't exist in the build context - **Incorrect build context**: The `context:` path in docker-compose.yml points to the wrong directory - **Resource exhaustion**: Docker running out of memory or disk space during the build - **Network failures**: Package managers unable to download dependencies
The "Service failed to build" message is a wrapper - the actual error appears earlier in the output. Scroll up in your terminal to find the specific failure. Look for patterns like:
Step 4/12 : COPY requirements.txt .
COPY failed: file not found in build context or excluded by .dockerignoreor
E: Package 'some-package' has no installation candidate
The command '/bin/sh -c apt-get install -y some-package' returned a non-zero code: 100The step number and specific error message tell you exactly where and why the build failed.
Common Dockerfile syntax issues that cause build failures:
# WRONG - Missing newline between instructions
RUN apt-get update RUN apt-get install -y curl
# CORRECT - Each instruction on its own line
RUN apt-get update
RUN apt-get install -y curl
# WRONG - Backslash followed by content instead of newline
RUN apt-get install -y \ curl git vim
# CORRECT - Backslash at end of line for continuation
RUN apt-get install -y \
curl \
git \
vimUse a Dockerfile linter to catch issues:
# Install hadolint
docker run --rm -i hadolint/hadolint < DockerfileIn docker-compose.yml, the build context determines what files are available during the build:
services:
myservice:
build:
context: ./backend # This is the build context
dockerfile: DockerfileAll paths in COPY/ADD commands are relative to this context. If your Dockerfile has:
COPY package.json ./Then ./backend/package.json must exist. Verify the file is present:
# Check the build context directory
ls -la ./backend/
# Verify specific files exist
test -f ./backend/package.json && echo "Found" || echo "Missing"A .dockerignore file can exclude files your build needs. Check if one exists and what it excludes:
cat .dockerignoreProblematic patterns that might exclude needed files:
# Excludes all dotfiles including .env templates
.*
# Might exclude important config
*.json
*.yml
# Too broad - excludes everything except what's explicitly included
*
!src/Temporarily rename the file to test if it's causing the issue:
mv .dockerignore .dockerignore.bak
docker-compose build myserviceBuild failures with exit code 137 or OOM messages indicate memory issues.
Docker Desktop (Windows/macOS):
1. Open Docker Desktop
2. Click Settings (gear icon)
3. Go to Resources > Advanced
4. Set Memory to at least 4GB (8GB recommended for larger builds)
5. Click "Apply & Restart"
Linux with systemd:
# Check current memory limits
docker info | grep -i memory
# Monitor memory during build
docker stats --no-stream
# Check system memory
free -hIf system memory is low, close other applications before building.
Disk space issues can cause cryptic build failures. Clean up Docker resources:
# Show Docker disk usage
docker system df
# Remove all unused data (containers, networks, images, build cache)
docker system prune -a
# Remove only build cache
docker builder prune
# Check available disk space
df -hThen retry the build without using cached layers:
docker-compose build --no-cache myserviceBuilding directly with the docker build command often provides clearer error output:
# Navigate to the build context directory
cd ./backend
# Build with detailed output
docker build -t myservice-test . --progress=plain
# Even more verbose with build arguments
docker build -t myservice-test . --progress=plain --no-cache 2>&1 | tee build.logThis bypasses docker-compose and lets you see exactly where the build fails.
Ensure the base image in your FROM instruction is accessible:
# Try pulling the base image directly
docker pull node:18-alpine
# If using a private registry, ensure you're logged in
docker login your-registry.comIf the image doesn't exist, you'll see errors like:
manifest for node:18-alpine-nonexistent not foundUpdate your Dockerfile to use a valid image tag.
BuildKit and improved error messages: Enable BuildKit for better build performance and clearer error output:
# Enable BuildKit
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
# Or in docker-compose.yml (v3.8+)
# Add to the top level of the file:
# x-bake:
# _default:
# platforms: [linux/amd64]Multi-stage build debugging: If using multi-stage builds and the failure occurs in an intermediate stage, build up to that stage:
docker build --target builder -t debug-build .
docker run -it debug-build /bin/shBuild arguments and secrets: If your build requires authentication or secrets, use build args carefully:
services:
myservice:
build:
context: .
args:
- NPM_TOKEN=${NPM_TOKEN}For Docker 20.10+, use BuildKit secrets to avoid leaking credentials in image layers:
# syntax=docker/dockerfile:1
RUN --mount=type=secret,id=npm,target=/root/.npmrc npm ciCross-platform builds: When building on Apple Silicon (M1/M2) for x86-64 deployment:
services:
myservice:
build:
context: .
platforms:
- linux/amd64CI/CD considerations: In CI environments, ensure the Docker daemon has sufficient resources and that all required build contexts are checked out. For GitHub Actions:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build
run: docker-compose build --no-cacheimage 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