How to install PHP composer inside a docker container

In Dockerfile :

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

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


#Get Composer
FROM composer:2.0 as vendor


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

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

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= --port=8080


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

# Install Composer
RUN curl -sS | 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 | 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

Create an executable of your composer file using

RUN curl -sS | php -- \
--install-dir=/usr/bin --filename=composer && chmod +x /usr/bin/composer