How to fix Service 'myservice' failed to build in Docker

DockerINTERMEDIATEMEDIUMUpdated Dec 1, 2025ยท 198 views

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.

Quick fix

Fixes Service 'myservice' failed to build

Step 4/12 : COPY requirements.txt .
COPY failed: file not found in build context or excluded by .dockerignore
  1. Examine the complete error output above the failure message
  2. Validate your Dockerfile syntax
  3. Verify the build context and file paths
  4. Review .dockerignore for accidentally excluded files
docker โ€” error
Step 4/12 : COPY requirements.txt .
COPY failed: file not found in build context or excluded by .dockerignore
Service 'myservice' failed to build

What this error means

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

Typical symptoms

  • Running 'docker-compose build' terminates with 'Service failed to build'
  • 'docker-compose up --build' fails during the image building phase
  • Build starts successfully but fails partway through with a non-zero exit code
  • Previously working builds now fail after making changes to Dockerfile or source files
  • Build appears to hang before ultimately failing with this error
  • Error message specifies which service failed: "Service 'myservice' failed to build"

Common causes

  1. Dockerfile contains syntax errors, invalid instructions, or typos
  2. COPY or ADD commands reference files that don't exist in the build context directory
  3. The build context path in docker-compose.yml is incorrect or misspelled
  4. Base image in FROM instruction is unavailable or the tag doesn't exist
  5. Insufficient memory allocated to Docker (especially common on Docker Desktop)
  6. Disk space is full, preventing image layers from being written
  7. Network issues prevent downloading packages during RUN commands
  8. .dockerignore file excludes files that the Dockerfile needs during build
  9. Line ending issues (CRLF vs LF) in scripts copied into the container

How to fix "Service 'myservice' failed to build"

1Examine the complete error output above the failure message

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:

bash
Step 4/12 : COPY requirements.txt .
COPY failed: file not found in build context or excluded by .dockerignore

or

bash
E: Package 'some-package' has no installation candidate
The command '/bin/sh -c apt-get install -y some-package' returned a non-zero code: 100

The step number and specific error message tell you exactly where and why the build failed.

2Validate your Dockerfile syntax

Common Dockerfile syntax issues that cause build failures:

dockerfile
# 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 \
    vim

Use a Dockerfile linter to catch issues:

bash
# Install hadolint
docker run --rm -i hadolint/hadolint < Dockerfile
3Verify the build context and file paths

In docker-compose.yml, the build context determines what files are available during the build:

yaml
services:
  myservice:
    build:
      context: ./backend    # This is the build context
      dockerfile: Dockerfile

All paths in COPY/ADD commands are relative to this context. If your Dockerfile has:

dockerfile
COPY package.json ./

Then ./backend/package.json must exist. Verify the file is present:

bash
# Check the build context directory
ls -la ./backend/

# Verify specific files exist
test -f ./backend/package.json && echo "Found" || echo "Missing"
4Review .dockerignore for accidentally excluded files

A .dockerignore file can exclude files your build needs. Check if one exists and what it excludes:

bash
cat .dockerignore

Problematic patterns that might exclude needed files:

bash
# 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:

bash
mv .dockerignore .dockerignore.bak
docker-compose build myservice
5Check Docker resource allocation

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

bash
# Check current memory limits
docker info | grep -i memory

# Monitor memory during build
docker stats --no-stream

# Check system memory
free -h

If system memory is low, close other applications before building.

6Clear Docker cache and free disk space

Disk space issues can cause cryptic build failures. Clean up Docker resources:

bash
# 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 -h

Then retry the build without using cached layers:

bash
docker-compose build --no-cache myservice
7Build the image directly with Docker for better error messages

Building directly with the docker build command often provides clearer error output:

bash
# 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.log

This bypasses docker-compose and lets you see exactly where the build fails.

8Verify base image availability

Ensure the base image in your FROM instruction is accessible:

bash
# 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.com

If the image doesn't exist, you'll see errors like:

bash
manifest for node:18-alpine-nonexistent not found

Update your Dockerfile to use a valid image tag.

Advanced notes

BuildKit and improved error messages: Enable BuildKit for better build performance and clearer error output:

bash
# 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:

bash
docker build --target builder -t debug-build .
docker run -it debug-build /bin/sh

Build arguments and secrets: If your build requires authentication or secrets, use build args carefully:

yaml
services:
  myservice:
    build:
      context: .
      args:
        - NPM_TOKEN=${NPM_TOKEN}

For Docker 20.10+, use BuildKit secrets to avoid leaking credentials in image layers:

dockerfile
# syntax=docker/dockerfile:1
RUN --mount=type=secret,id=npm,target=/root/.npmrc npm ci

Cross-platform builds: When building on Apple Silicon (M1/M2) for x86-64 deployment:

yaml
services:
  myservice:
    build:
      context: .
      platforms:
        - linux/amd64

CI/CD considerations: In CI environments, ensure the Docker daemon has sufficient resources and that all required build contexts are checked out. For GitHub Actions:

yaml
- name: Set up Docker Buildx
  uses: docker/setup-buildx-action@v3
- name: Build
  run: docker-compose build --no-cache

Frequently asked questions

What does "Service 'myservice' failed to build" mean?
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.
How do I fix "Service 'myservice' failed to build"?
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.
What causes "Service 'myservice' failed to build"?
Dockerfile contains syntax errors, invalid instructions, or typos COPY or ADD commands reference files that don't exist in the build context directory The build context path in docker-compose.yml is incorrect or misspelled
Is the "Service 'myservice' 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