Search code examples
phplaraveldockercomposer-phpdocker-compose

How to install PHP composer inside a docker container


I try to work out a way to create a dev environment using docker and laravel.

I have the following dockerfile:

FROM php:7.1.3-fpm

RUN apt-get update && apt-get install -y libmcrypt-dev \
    mysql-client libmagickwand-dev --no-install-recommends \
    && pecl install imagick \
    && docker-php-ext-enable imagick \
&& docker-php-ext-install mcrypt pdo_mysql
&& chmod -R o+rw laravel-master/bootstrap laravel-master/storage

Laravel requires composer to call composer dump-autoload when working with database migration. Therefore, I need composer inside the docker container.

I tried:

RUN curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/bin --filename=composer

But when I call

docker-compose up
docker-compose exec app composer dump-autoload

It throws the following error:

rpc error: code = 13 desc = invalid header field value "oci runtime error: exec failed: container_linux.go:247: starting container process caused \"exec: \\\"composer\\\": executable file not found in $PATH\"\n"

I would be more than happy for advice how I can add composer to the PATH within my dockerfile or what else I can do to surpass this error.

Thanks for your support. Also: this is the gitub repository if you need to see the docker-compose.yml file or anything else.


Solution

  • I can install composer adding this line on my test dockerfile:

    # Install Composer
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
    

    Here is the dockerfile:

    FROM php:7.1.3-fpm
    
    RUN apt-get update && apt-get install -y libmcrypt-dev \
        mysql-client libmagickwand-dev --no-install-recommends \
        && pecl install imagick \
        && docker-php-ext-enable imagick \
    && docker-php-ext-install mcrypt pdo_mysql
    
    # Install Composer
    RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
    

    It works for me, to test if the composer are installed i access to my container bash and execute:

    composer --version
    Composer version 1.6.5 2018-05-04 11:44:59