如何在 docker 容器中安装 PHP composer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51443557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:52:24  来源:igfitidea点击:

How to install PHP composer inside a docker container

phplaraveldockercomposer-phpdocker-compose

提问by Andre

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

我尝试找到一种使用 docker 和 laravel 创建开发环境的方法。

I have the following dockerfile:

我有以下 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.

Laravel 需要 Composer 在处理数据库迁移时调用 composer dump-autoload。因此,我需要在 docker 容器中使用 Composer。

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.

我很乐意为我提供建议,如何将 Composer 添加到我的 dockerfile 中的 PATH 或者我可以做些什么来克服这个错误。

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

谢谢您的支持。另外:如果您需要查看 docker-compose.yml 文件或其他任何内容,是 gitub 存储库。

回答by yoben

In Dockerfile :

在 Dockerfile 中:

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

回答by Brayan Caldera

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

我可以安装 composer 在我的测试 dockerfile 上添加这一行:

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

Here is the dockerfile:

这是 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 我访问我的容器 bash 并执行:

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

回答by mutas

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.

或者,您应该将 Composer bin 文件路径添加到 $PATH 变量。

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

回答by Adnan Mumtaz

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