简体   繁体   中英

Docker - Using Composer inside PHP container

I want to be able to use composer install inside my php-fpm container. My current setup:

docker-compose.yml

version: '2'

services:
    web:
        image: nginx
        ports:
          - "80:80"
        volumes:
          - ./public:/var/www/html
          - ./vhost.conf:/etc/nginx/conf.d/vhost.conf

    fpm:
        image: php:fpm
        volumes:
          - ./public:/var/www/html
        expose:
          - 9000

    composer:
        restart: 'no'
        image: composer/composer
        command: install --working-dir=/var/www/html
        volumes_from:
          - fpm

But obviously my command: install is happening in the composer container and doesnt have the required php extensions to complete the install.

And composer install inside php-fpm container says composer is not installed

Somehow google doesn't have an answer for this from what I have seen.

You just need to install composer inside your fpm container.

Something like

FROM php:5.6-fpm 

...
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && php composer-setup.php --install-dir=/usr/local/bin --filename=composer \
    && php -r "unlink('composer-setup.php');"

A more sexy way yo add composer into your image :

# Install Composer
ADD https://getcomposer.org/installer /tmp/composer-setup.php
RUN php /tmp/composer-setup.php --install-dir /usr/local/bin/ --filename composer \
 && rm /tmp/composer-setup.php

Note that composer can be useful for development images (eg: For Continuous Integration or tests). A production image should not have composer inside... Only the generated vendors :)

Actually what I would suggest would be a separate lightweight php-cli container just to run composer (and other commands). It doesn't have to run a "persistent" way. If interested have a look how I did my image for running php tasks like composer or phpunit.

docker run -ti --rm -v /path/to/your/project:/app kmwrona/ci-stack /usr/bin/composer install --quiet
docker run -ti --rm -v /path/to/your/project:/app kmwrona/ci-stack /usr/bin/composer dump-autoload --optimize
docker run -ti -a STDOUT -v /path/to/your/project:/app kmwrona/ci-stack /usr/bin/phpunit --log-junit /app/junit.xml --testdox-html /app/unit-tests-html-report.html

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