How to fix ERROR: Service 'app' failed to build in Docker

DockerBEGINNERMEDIUMUpdated Dec 1, 2025ยท 164 views

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.

Quick fix

Fixes ERROR: Service 'app' failed to build

Step 5/10 : COPY package.json .
COPY failed: file not found in build context
  1. Read the full error output above the failure message
  2. Verify your Dockerfile syntax
  3. Check file paths and build context
  4. Check .dockerignore for excluded files
docker โ€” error
Step 5/10 : COPY package.json .
COPY failed: file not found in build context
ERROR: Service 'app' failed to build

What this error means

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

Typical symptoms

  • Running 'docker-compose build' or 'docker-compose up --build' fails
  • Error message shows 'ERROR: Service '<name>' failed to build'
  • Build process starts but terminates before completion
  • Previous builds worked but now fail after changes
  • Build hangs indefinitely before eventually failing

Common causes

  1. Dockerfile has syntax errors or invalid commands
  2. Files referenced in COPY/ADD don't exist in the build context
  3. Build context path in docker-compose.yml is incorrect
  4. Insufficient memory allocated to Docker
  5. No disk space left on device
  6. Package installation fails due to network issues or missing dependencies
  7. Base image specified in FROM doesn't exist or can't be pulled
  8. .dockerignore excludes files needed by the build
  9. Cross-platform compatibility issues (e.g., Windows line endings)

How to fix "ERROR: Service 'app' failed to build"

1Read the full error output above the failure message

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:

bash
Step 5/10 : COPY package.json .
COPY failed: file not found in build context

or

bash
E: Unable to locate package some-package
The command '/bin/sh -c apt-get install some-package' returned a non-zero code: 100

This specific error tells you exactly what went wrong.

2Verify your Dockerfile syntax

Check your Dockerfile for common syntax issues:

dockerfile
# 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 flask

Also 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

3Check file paths and build context

Files in COPY and ADD commands must exist relative to the build context. In docker-compose.yml:

yaml
services:
  app:
    build:
      context: ./my-app      # Build context directory
      dockerfile: Dockerfile  # Relative to context

If your Dockerfile contains COPY package.json ., then package.json must exist in ./my-app/.

Run this to verify:

bash
ls -la ./my-app/package.json
4Check .dockerignore for excluded files

If you have a .dockerignore file, it might be excluding files your build needs:

bash
cat .dockerignore

Common problematic patterns:

bash
# This excludes ALL files starting with "."
.*

# This might exclude needed config files
*.json

Temporarily rename or remove .dockerignore to test if it's the cause.

5Increase Docker memory allocation

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:

bash
free -h
docker info | grep -i memory

If low, close other applications or add swap space.

6Clean up Docker resources and retry

Stale caches or low disk space can cause build failures:

bash
# Remove unused containers, networks, images
docker system prune -a

# Check disk space
docker system df

# Force rebuild without cache
docker-compose build --no-cache

If disk space is critically low, consider removing old images:

bash
docker image prune -a
7Test the Dockerfile directly

Build the image directly with Docker to get more detailed output:

bash
# Navigate to the build context directory
cd ./my-app

# Build with verbose output
docker build -t test-build . 2>&1 | tee build.log

This often provides clearer error messages than docker-compose.

Advanced notes

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:

bash
node_modules
.git
*.log
dist
build

Multi-stage builds: For complex applications, use multi-stage builds to reduce final image size and isolate build-time dependencies:

dockerfile
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:

yaml
services:
  app:
    build:
      context: .
      platform: linux/amd64

Debugging failed layers: To inspect where the build failed, add temporary debug steps:

dockerfile
RUN ls -la /app  # See what files exist at this point
RUN cat /app/package.json  # Verify file contents

Frequently asked questions

What does "ERROR: Service 'app' failed to build" mean?
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.
How do I fix "ERROR: Service 'app' failed to build"?
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.
What causes "ERROR: Service 'app' failed to build"?
Dockerfile has syntax errors or invalid commands Files referenced in COPY/ADD don't exist in the build context Build context path in docker-compose.yml is incorrect
Is the "ERROR: Service 'app' failed to build" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading