Docker

How to Resolve "failed to solve: process '/bin/sh -c' returned a non-zero code: 1"

4 min read by DebuggedIt

Quick answer

The error "failed to solve: process '/bin/sh -c' returned a non-zero code: 1" is a generic failure message returned by Docker BuildKit when a command executed...

The error "failed to solve: process '/bin/sh -c' returned a non-zero code: 1" is a generic failure message returned by Docker BuildKit when a command executed inside a RUN directive fails. Because /bin/sh -c executes commands within the isolated build container environment, any non-zero exit status from shell commands (such as apt-get, npm install, or pip install) causes the entire Docker build step to fail. Fixing it requires locating the specific command failure within the build log and adjusting package versions, dependencies, or shell execution flags.

The Problem

During execution of docker build or docker buildx build, BuildKit processes your Dockerfile steps sequentially. When a command in a RUN instruction fails, output truncates with this terminal error:

------
 > [5/5] RUN npm install:
#0 1.842 npm ERR! code ERESOLVE
#0 1.845 npm ERR! ERESOLVE unable to resolve dependency tree
#0 2.102 npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2026-08-06T12_00_00_000Z-debug-0.log
------
Dockerfile:12
--------------------
  10 |     COPY package*.json ./
  11 | >>> RUN npm install
  12 |     
--------------------
error: failed to solve: process "/bin/sh -c npm install" returned a non-zero code: 1

In older Docker versions or legacy builder setups without BuildKit, the error message renders as:

The command '/bin/sh -c apt-get update && apt-get install -y python3' returned a non-zero code: 1

Why It Happens

The message process '/bin/sh -c' returned a non-zero code: 1 indicates that the subshell spawned by Docker received an error exit status (code 1) from the command executed inside the container context. The primary causes include:

  • Dependency Conflicts: Packages in npm install, pip install, or bundle install fail version resolution or missing OS-level headers.
  • Missing Package Lists: Running apt-get install without running apt-get update first in the same RUN layer.
  • File Path or Permission Issues: Build scripts missing executable permissions (chmod +x) or attempting to access files not copied into the build image context.
  • Architecture Mismatches: Compiling binary native extensions (like node-gyp or gfortran) without necessary build tools installed in slim base images (e.g., Alpine or Debian Slim).

The Fix

Follow these structured steps to isolate and eliminate the step failure.

Step 1: Disable BuildKit plain output truncation to see full logs

By default, Docker BuildKit truncates error output. Run the build command with --progress=plain to inspect every line of stderr:

DOCKER_BUILDKIT=1 docker build --progress=plain --no-cache -t my-app .

Step 2: Fix Common Package Manager Failures

If the error occurs during an apt-get step, ensure apt-get update and apt-get install are chained together in a single RUN block, using the -y flag to prevent interactive prompts:

# Incorrect: Separate RUN steps cause stale apt indexes
# RUN apt-get update
# RUN apt-get install -y curl

# Correct: Chained execution in a single layer
RUN apt-get update && apt-get install -y --no-install-recommends     curl     ca-certificates     && rm -rf /var/lib/apt/lists/*

If the error occurs during npm install, use npm ci for clean, repeatable builds based on package-lock.json, or pass legacy peer dependency flags if using newer Node versions:

RUN npm ci --legacy-peer-deps

Step 3: Test interactively in the container

If the root cause remains unclear, start an interactive container from the last successful layer to run the failing command manually:

docker run --rm -it <IMAGE_ID_OF_LAST_SUCCESSFUL_STEP> /bin/sh

Inside the interactive shell, run your build script line by line to replicate the exact failure and identify missing libraries or syntax errors.

Still Not Working?

If you are using Alpine Linux base images (e.g., node:alpine or python:alpine), many pre-compiled C/C++ libraries (musl libc vs glibc) fail to compile during RUN /bin/sh -c.

To fix Alpine binary compilation failures, install build-base and required system libraries before running your package manager:

RUN apk add --no-cache build-base python3 make g++

Alternatively, switch from an Alpine base image to a Debian-based slim image (e.g., node:20-slim or python:3.11-slim) to resolve binary compatibility issues.