简体   繁体   中英

PHP extension unavailable to composer container in docker-compose

I use docker-compose and have a number of containers in one project: Nginx, PHP, Composer and nginx. All works well except for one thing: composer does not work. I am trying to install a composer project that uses the GD extension, which is installed in PHP (confirmed using php -m inside the PHP container). However, the composer container does not "see" this extension and complaints it does not exist. How can I link those two?

docker-compose.yml:

version: '2'
services:
    web:
        image: nginx:1.15.1
        volumes:
            - ./.docker/conf/nginx/default.conf:/etc/nginx/conf.d/default.conf
            - ./html:/var/www/html
        ports:
            - 8888:80
        depends_on:
            - php
            - db
    php:
        build: .docker
        volumes:
            - ./.docker/conf/php/php.ini:/usr/local/etc/php/conf.d/php.ini
            - ./.docker/conf/php/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
            - ./html:/var/www/html
    composer:
        image: composer
        volumes:
            - ./html:/app
        command: install
        depends_on:
            - php
    db:
        image: postgres:10.4
        environment:
            - POSTGRES_DB=test
            - POSTGRES_USER=test
            - POSTGRES_PASSWORD=test
        ports:
            - 5432:5432
        volumes:
            - ./.docker/conf/postgres/:/docker-entrypoint-initdb.d/

    adminer:
        image: adminer
        ports:
        - 8080:8080

Dockerfile:

FROM php:7.2-fpm

RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
        libicu-dev \
        libpq-dev \
        libxpm-dev \
libvpx-dev \
    && pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install -j$(nproc) intl \
    && docker-php-ext-install -j$(nproc) zip \
    && docker-php-ext-install -j$(nproc) pgsql \
    && docker-php-ext-install -j$(nproc) pdo_pgsql \
    && docker-php-ext-install -j$(nproc) exif \
    && docker-php-ext-configure gd \
        --with-freetype-dir=/usr/include/ \
        --with-jpeg-dir=/usr/include/ \
        --with-xpm-dir=/usr/lib/x86_64-linux-gnu/ \
        --with-vpx-dir=/usr/lib/x86_64-linux-gnu/ \

Error:

Starting test_app_db_1      ... done
Starting test_app_php_1     ... done
Starting test_app_adminer_1 ... done
Recreating test_app_composer_1 ... done
Starting test_app_web_1        ... done
Attaching to test_app_adminer_1, test_app_php_1, test_app_db_1, test_app_web_1, test_app_composer_1
php_1       | [12-Oct-2018 21:26:47] NOTICE: fpm is running, pid 1
php_1       | [12-Oct-2018 21:26:47] NOTICE: ready to handle connections
adminer_1   | PHP 7.2.1 Development Server started at Fri Oct 12 21:26:47 2018
db_1        | 2018-10-12 21:26:47.716 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1        | 2018-10-12 21:26:47.716 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1        | 2018-10-12 21:26:47.736 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1        | 2018-10-12 21:26:47.827 UTC [21] LOG:  database system was shut down at 2018-10-12 21:11:42 UTC
db_1        | 2018-10-12 21:26:47.845 UTC [1] LOG:  database system is ready to accept connections
composer_1  | Loading composer repositories with package information
composer_1  | Updating dependencies (including require-dev)
composer_1  | Your requirements could not be resolved to an installable set of packages.
composer_1  | 
composer_1  |   Problem 1
composer_1  |     - gumlet/php-image-resize 1.9.1 requires ext-gd * -> the requested PHP extension gd is missing from your system.
composer_1  |     - gumlet/php-image-resize 1.9.0 requires ext-gd * -> the requested PHP extension gd is missing from your system.
composer_1  |     - Installation request for gumlet/php-image-resize 1.9.* -> satisfiable by gumlet/php-image-resize[1.9.0, 1.9.1].
composer_1  | 
composer_1  |   To enable extensions, verify that they are enabled in your .ini files:
composer_1  |     - 
composer_1  |     - /usr/local/etc/php/conf.d/date_timezone.ini
composer_1  |     - /usr/local/etc/php/conf.d/docker-php-ext-sodium.ini
composer_1  |     - /usr/local/etc/php/conf.d/docker-php-ext-zip.ini
composer_1  |     - /usr/local/etc/php/conf.d/memory-limit.ini
composer_1  |   You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
test_app_composer_1 exited with code 2

Alternatively, you may pass the --ignore-platform-reqs and --no-scripts flags to install or update (as mentioned here ).

docker run --rm --interactive --tty \
    --volume $PWD:/app \
    composer install --ignore-platform-reqs --no-scripts

But I always prefer installing composer in my php container.

You are installing the modules in your php service, but not in your composer service.

When composer runs, the php installation in its container doesn't have those modules installed.

You could install composer in your php container, and run it from there.

You could do it by simply adding:

# latest composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

In your php dockerfile, right after your RUN line.

That would require you removing your composer container completely, and run composer directly within the PHP container.

You can also simply run composer and tell it not to check for platform requirements:

docker run --rm --interactive --tty \
    --volume $PWD:/var/www/html \
    composer install --ignore-platform-reqs 

or if you have a separate composer container, for some reason:

docker-compose run composer install --ignore-platform-reqs

A better option could be to specify the env dependencies using platform setting. Found on: https://stackoverflow.com/a/55431247/2254615

"config": {
"platform": {
    "php": "7.2.14",
    "ext-fileinfo": "1.0.5",
    "ext-pdo": "7.2.14",
    "ext-session": "7.2.14",
    "ext-iconv": "7.2.14",
    "ext-zip": "1.15.4"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM