141

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 github repository if you need to see the docker-compose.yml file or anything else.

6
  • 4
    Is there a need to reinvent the wheel? Why not use out-of-the-box solutions for docker, PHP and Laravel (like Laradock that covers pretty much everything you need and is easily configurable)? Commented Jul 20, 2018 at 13:30
  • Alternatively, there is always the possibility to use composer as .phar checked in as part of the repository. Commented Jul 20, 2018 at 13:31
  • 1
    Are you sure your container is successfully build? This script already makes composer executable. docker run -it --rm php:7.1.3-fpm bash curl -sS getcomposer.org/installer | php -- \ --install-dir=/usr/bin --filename=composer composer --version Composer version 1.6.5 2018-05-04 11:44:59 So problem is not in the docker or composer itself, probable wrong-config or some issue with docker-compose Commented Jul 20, 2018 at 13:57
  • 2
    @d3jn this is the first time I work with docker and I want to rebuild this laravel + docker walking skeleton to learn from it. Thanks anyway for pointing it Laradock! Commented Jul 21, 2018 at 8:31
  • 1
    @d3jn of course not, but this is only one possible scenario - try to think of all the people who will arrive at this post because they have a custom image build that also needs composer. Workarounds for specific use cases tend not to be very useful on SO. Commented Aug 5, 2021 at 0:51

10 Answers 10

374

You can perform an in-line Multi-stage build using composer image so you will basically just copy composer binary into the PHP docker image.

In Dockerfile :

COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
Sign up to request clarification or add additional context in comments.

8 Comments

Just be aware that --from is fairly new and won't be available in all environments.
Note if you have packages in your composer.json which can only be installed from source then you will also need to install git.
If you need a specific Version, this works fine, Tank you. I am using this COPY --from=composer:2.1.8 /usr/bin/composer /usr/local/bin/composer
My problem solved. I just stopped firewalld service on system
There's actually an improved version of this method to save some time and memory. It uses a very small Docker image that only contains the composer binary: COPY --from=composer/composer:latest-bin /composer /usr/local/bin/composer See the composer docs for more info: getcomposer.org/doc/00-intro.md#docker-image
|
179

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

4 Comments

Isnt composer --version just returning output from host machine? Or where do you call it?
you need to run the image generated with docker run -dit my-image command, after that you need to execute docker exec -it container-id sh, and with this command you can access to the container terminal, when you are on the container terminal you can execute the composer --version command to validate that this is installed on your container
Since Composer 2.0 is out and many projects still need the 1.x version, you can also pass --version to the installer for a specific Composer version as such: RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer --version=1.10.22
Better to get it like so: COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer, from this answer
8

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

2 Comments

be aware that it's not recommended using artisan serve for production.
I had to add --ignore-platform-reqs since the Composer image didn't have all the requirements like php version that are in my composer.json
6

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 

2 Comments

Unfortunately, still the same error code. Do you have an idea what could possibly be still missing?
Adding && chmod +x /usr/bin/composer fixed the composer: not found error for me
5

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

Comments

4

Use actual docs:

Dockerfile:

# Install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && php composer-setup.php \
    && php -r "unlink('composer-setup.php');" \
    && mv composer.phar /usr/local/bin/composer

Comments

1

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"

Comments

0

I'd just like to suggest another way.

Dockerfile:

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

Comments

0
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'

Comments

0

You can also use docker-php-extension-installer: Installing composer:

You can also install composer, and you also can specify a major version of it, or a full version.

Examples:

# Install the latest version install-php-extensions @composer
# Install the latest 1.x version install-php-extensions @composer-1
# Install a specific version install-php-extensions @composer-2.0.2

With php:8.1-fpm image:

FROM 8.1.4-fpm

RUN curl -sSLf \
    -o /usr/local/bin/install-php-extensions \
    https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions && \
    chmod +x /usr/local/bin/install-php-extensions
RUN install-php-extensions @composer

Originally, this is a recommended way to install PHP extensions from the official PHP images (How to use this image > How to install more PHP extensions > PHP Core Extensions):

If you are having difficulty figuring out which Debian or Alpine packages need to be installed before docker-php-ext-install, then have a look at the install-php-extensions project⁠.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.