How to install PHP composer inside a docker container

PhpLaravelDockerComposer PhpDocker Compose

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

In Dockerfile :

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

Solution 2 - Php

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 3 - Php

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 - Php

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 - Php

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 - Php

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 - Php

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 - Php

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 - Php

My problem solved. I just stopped firewalld service on system

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreView Question on Stackoverflow
Solution 1 - PhpybenhssaienView Answer on Stackoverflow
Solution 2 - PhpBrayan CalderaView Answer on Stackoverflow
Solution 3 - Phphannes_rdView Answer on Stackoverflow
Solution 4 - PhpPULL STACK DEVView Answer on Stackoverflow
Solution 5 - PhpmutasView Answer on Stackoverflow
Solution 6 - PhpPankaj SalunkheView Answer on Stackoverflow
Solution 7 - PhpLeonel KahameniView Answer on Stackoverflow
Solution 8 - PhpMHAMMED TALHAOUYView Answer on Stackoverflow
Solution 9 - PhpsoroushView Answer on Stackoverflow