[英]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.我尝试找出一种使用 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. Laravel 要求 composer 在处理数据库迁移时调用 composer dump-autoload。 Therefore, I need composer inside the docker container.因此,我需要 docker 容器内的作曲家。
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.我会非常乐意建议我如何在 dockerfile 中将作曲家添加到 PATH 中,或者我还能做些什么来克服这个错误。
Thanks for your support.谢谢你的支持。 Also: this is the gitub repository if you need to see the docker-compose.yml file or anything else.另外:如果您需要查看 docker-compose.yml 文件或其他任何内容,这是 gitub 存储库。
在 Dockerfile 中:
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
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
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
This is how i do it with Laravel 8.4 in 2021 to deploy it to CloudRun in Google Cloud:这是我在 2021 年使用 Laravel 8.4 将其部署到 Google Cloud 中的 CloudRun 的方法:
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 // 复制 Composer 依赖项
# 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结束我的 Dockerfile 以使用清除的优化缓存启动应用程序
# Run Laravel commands
RUN php artisan optimize:clear
CMD php artisan serve --host=0.0.0.0 --port=8080
EXPOSE 8080
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"
I'd just like to suggest another way.我只想建议另一种方式。
Dockerfile: Dockerfile:
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
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'
My problem solved.我的问题解决了。 I just stopped firewalld
service on system我刚刚停止了系统上的firewalld
服务
use composer
in dockerfile using curl
在 dockerfile 中使用composer
curl
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
Dockerfile 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.