'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 1:[1]

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

Solution 2:[2]

In Dockerfile :

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

Solution 3:[3]

This is how i do it with Laravel 8.4 in 2021 to deploy it to CloudRun in Google Cloud:

Dockerfile

#Get Composer
FROM composer:2.0 as vendor

WORKDIR /app

COPY database/ database/
COPY composer.json composer.json
COPY composer.lock composer.lock

RUN composer install \
    --no-interaction \
    --no-plugins \
    --no-scripts \
    --no-dev \
    --prefer-dist

COPY . .
RUN composer dump-autoload

// some more custom steps like

FROM node:14.9 as frontend
...
FROM php:7.4-fpm
...

// Copy Composer dependencies

# Copy Composer dependencies
COPY --from=vendor app/vendor/ ./vendor/
COPY . .

// Some more custom steps

...

End of my Dockerfile to launch app with cleared optimized cache

# Run Laravel commands
RUN php artisan optimize:clear

CMD php artisan serve --host=0.0.0.0 --port=8080

EXPOSE 8080

Solution 4:[4]

Create an executable of your composer file using

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

Solution 5:[5]

We have basicly the same command running with the difference,

--install-dir=/usr/local/bin

Alternatively, you should add the composer bin files path to the $PATH variable.

export PATH=$PATH":/usr/bin"

Solution 6:[6]

I'd just like to suggest another way.

Dockerfile:

RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer

Solution 7:[7]

FROM php:7.3-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
RUN apk update
RUN apk upgrade
RUN apk add bash
RUN alias composer='php /usr/bin/composer'

Solution 8:[8]

use composer in dockerfile using curl

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

Dockerfile

FROM 8.1.4-fpm

RUN apt-get update && apt-get install -y \
    git \
    curl \
    zip \
    unzip

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

WORKDIR /var/www

Solution 9:[9]

My problem solved. I just stopped firewalld service on system