Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

docker build stops at RUN composer install with exit code 1

Errorfailed to solve: process "/bin/sh -c composer install" did not complete successfully: exit code: 1

Short answer

Exit code 1 only says the RUN step failed. The real reason is a few lines above it in the build log. In a PHP image it is usually one of four things: Composer ran out of memory, unzip is missing, a PHP extension the project requires is not installed, or a composer script failed.

  • Applies to Composer 2, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Rebuild with plain progress output so the full Composer message stays on screen.

    docker build --progress=plain --no-cache . 2>&1 | tail -n 60
  2. 2Match what you see, then jump to its fix

    1. 1Allowed memory size of XXXXXX bytes exhaustedComposer runs out of PHP memoryFix
    2. 2The log shows packages that fail to unpack, and unzip is not found in the imageThe image has no unzip or 7zFix
    3. 3The log names a missing ext-* requirement, or the platform check stops the appA required PHP extension is not installed in the imageFix
    4. 4The failing line starts with the name of a script, such as post-autoload-dump, not with a downloadA composer script fails during the buildFix

What does the error look like?

  • failed to solve: process "/bin/sh -c composer install" did not complete successfully: exit code: 1
  • Allowed memory size of ... bytes exhausted

Cause 1 of 4

Composer runs out of PHP memory

1Check

Read the memory limit PHP gives Composer inside the image.

docker run --rm your-image php -r 'echo ini_get("memory_limit"), PHP_EOL;'

2Fix

Use Composer 2, and raise the limit for the install step with the COMPOSER_MEMORY_LIMIT environment variable.

RUN COMPOSER_MEMORY_LIMIT=-1 composer install --no-interaction

3Verify

The install step completes.

docker build --progress=plain .

Confirmed by getcomposer.org

Did this fix it?

Cause 2 of 4

The image has no unzip or 7z

1Check

Composer unpacks archives with the system unzip or 7z, or PHP's ZipArchive. Slim images ship none of the tools, and Alpine-based images leave out others.

docker run --rm your-image sh -c 'which unzip 7z; php -m | grep -i zip'

2Fix

Install unzip with the image's package manager before running Composer.

RUN apt-get update && apt-get install -y --no-install-recommends unzip git \
    && rm -rf /var/lib/apt/lists/*

3Verify

Rebuild and watch the install extract packages normally.

docker build --progress=plain .

Confirmed by getcomposer.org

Did this fix it?

Cause 3 of 4

A required PHP extension is not installed in the image

1Check

Compare the extensions the project needs with what the image has. The official PHP image lists its built-ins with php -m, and extra ones are added with its own scripts.

docker run --rm your-image php -m
composer check-platform-reqs

2Fix

Install the extension in the Dockerfile before composer install. Do not hide the problem with --ignore-platform-req unless the extension is truly not needed at runtime.

RUN apt-get update && apt-get install -y libfreetype-dev libjpeg62-turbo-dev libpng-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd

3Verify

composer check-platform-reqs returns zero.

docker run --rm your-image composer check-platform-reqs

Confirmed by hub.docker.com

Did this fix it?

Cause 4 of 4

A composer script fails during the build

1Check

Scripts in composer.json run after install and may need app code, an .env file or services that are not in the image yet.

grep -n -A6 '"scripts"' composer.json

2Fix

Install without scripts first, copy the application, then run the scripts in a later step.

COPY composer.json composer.lock ./
RUN composer install --no-scripts --no-autoloader --no-interaction
COPY . .
RUN composer dump-autoload --optimize

3Verify

Both steps pass.

docker build --progress=plain .

Confirmed by getcomposer.org

Did this fix it?

How do I stop it coming back?

  • Commit composer.lock and install from it, so builds do not change between runs.
  • Run composer check-platform-reqs as a build step.
  • Keep composer install before the COPY of your source so Docker can cache it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

composer require laravel/horizon fails: ext-pcntl is missing

laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system

Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.