mlocati / docker-php-extension-installer

Easily install PHP extensions in Docker containers
MIT License
4.33k stars 388 forks source link

Docker build does not abort on error #934

Closed dieisraels closed 4 months ago

dieisraels commented 4 months ago

Version of install-php-extensions

2.2.17

Error description

It can happen that the installation is not successful if, for example, the sources are not available. Here is an example of the output in our Bitbucket pipeline:

#9 [linux/amd64 2/5] RUN curl -sSL https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - | sh -s     @composer apcu bcmath exif gd gmagick iconv intl redis mysqli pcntl pdo_mysql soap xml xsl zip
#9 40.18 curl: (6) Could not resolve host: [objects.githubusercontent.com](http://objects.githubusercontent.com/)
#9 DONE 40.6s

In this case, the Docker build does not abort, but simply continues to run. As we run this process automatically, we usually do not see the error. The build process should abort in the step if the installation was not successful.

Or do we need to implement this differently in the Dockerfile?

Docker image

php:8.3-fpm-bookworm

Minimal Dockerfile

RUN curl -sSL https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - | sh -s \
    @composer apcu bcmath exif gd gmagick iconv intl redis mysqli pcntl pdo_mysql soap xml xsl zip
mlocati commented 4 months ago

This problem is not related at all with the install-php-extensions script.

What's failing is the curl part: it can't download the install-php-extensions script (so, install-php-extensions isn't executed at all).

In order to have a (correct) failure build, you should:

  1. tell curl to exit with a failure in case of errors: that's done by using the -f option (so, use curl -sSLf instead of curl -sSL)
  2. enable "exit with a failure if the left part of a pipe fails": that's done by calling set -o pilefail
  3. since on Debian sh doesn't understand set -o pilefail, you have to instruct docker to use bash instead of sh for the RUN scripts: that's done by adding a SHELL ["/bin/bash", "-c"] line to the dockerfile

To sum it all up, the Dockerfile should be something like this:

FROM php:8.3-fpm-bookworm

SHELL ["/bin/bash", "-c"]

RUN set -o pipefail && \
    curl -sSLf https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - | sh -s \
    @composer apcu bcmath exif gd gmagick iconv intl redis mysqli pcntl pdo_mysql soap xml xsl zip

Another solution would be to use any other method to fetch and run install-php-extensions - see the README (and that's what I prefer)

mlocati commented 4 months ago

Another (simpler) solution is the one I suggested here: https://github.com/mlocati/docker-php-extension-installer/commit/45759d1b6c9b8cb83dabaf960a598b98e5ee333b