在 Dockerfile 中运行 composer install [英] Running composer install within a Dockerfile

查看:59
本文介绍了在 Dockerfile 中运行 composer install的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Dockerize 我的 Laravel 应用程序.该应用程序已经在 git 中构建,但我 .gitignore 我的供应商文件夹.我添加了一个 Dockerfile,如下所示:

I'm trying to Dockerize my laravel app. The app is already built and in git, but I .gitignore my vendor folder. I've added a Dockerfile, which looks like this:

FROM php:7.1-fpm-alpine

RUN apk update && apk add curl && 
  curl -sS https://getcomposer.org/installer | php 
  && chmod +x composer.phar && mv composer.phar /usr/local/bin/composer

RUN apk --no-cache add --virtual .build-deps $PHPIZE_DEPS 
  && apk --no-cache add --virtual .ext-deps libmcrypt-dev freetype-dev 
  libjpeg-turbo-dev libpng-dev libxml2-dev msmtp bash openssl-dev pkgconfig 
  && docker-php-source extract 
  && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ 
                                   --with-png-dir=/usr/include/ 
                                   --with-jpeg-dir=/usr/include/ 
  && docker-php-ext-install gd mcrypt mysqli pdo pdo_mysql zip opcache 
  && pecl install mongodb redis xdebug 
  && docker-php-ext-enable mongodb 
  && docker-php-ext-enable redis 
  && docker-php-ext-enable xdebug 
  && docker-php-source delete 
  && apk del .build-deps

WORKDIR /var/www/html

COPY composer.json composer.lock ./
RUN composer install --no-scripts --no-autoloader

COPY . .
RUN chmod +x artisan

RUN composer dump-autoload --optimize && composer run-script post-install-cmd

CMD php artisan serve --host 0.0.0.0 --port 5001

当我构建时,这似乎很好用.我看到依赖项被下载,我看到输出中生成了自动加载文件.但是,一旦构建完成,供应商文件夹实际上并不存在.我猜这一切都是在一个中间容器中完成的,然后被移除了?所以当我运行 docker-compose up 时,我得到:致命错误:require():无法打开所需的'/var/www/html/bootstrap/../vendor/autoload.php'

When I build, this seems to work great. I see the dependencies getting downloaded, I see the autoload file being generated in the output. However, once the build is complete, the vendor folder is not actually there. I'm guessing it was all done in an intermediate container which was then removed? So when I run docker-compose up, I get: Fatal error: require(): Failed opening required '/var/www/html/bootstrap/../vendor/autoload.php'

这个线程似乎指向了这个问题——可能——但并没有真正提供解决方案:Composer install 在 Dockerfile 中运行时不安装包

This thread seems to point to the issue - possibly - but doesn't really provide a solution: Composer install doesn't install packages when running in Dockerfile

推荐答案

对于 Docker 的新手来说,这需要大量的挖掘 :)感谢@iurii-drozdov 为我指出了正确的方向,并给出了关于 docker-compose.yml 的评论.

This took a lot of digging for someone new to Docker :) Thanks to @iurii-drozdov for pointing me in the right direction with the comment about the docker-compose.yml.

在我的 docker-compose.yml 中,我将我的主机工作目录安装到/var/www/html.这发生在构建之后.因此,composer 运行安装程序,在构建时正确安装所有依赖项,然后,在运行 docker-compose up 时,我将主机目录安装到容器中并清除所有这些更改.

In my docker-compose.yml, I was mounting my host working dir into /var/www/html. This happened after the build. So composer ran the install, installed all the dependencies correctly on build, and then, when running docker-compose up, I was mounting my host dir into the container and wiping all those changes out.

解决方案是在安装卷之后运行 composer install .只需在启动容器后简单地执行到容器中即可完成此操作 - 运行 Composer 和任何其他包管理器 - 然后最终运行 Web 服务器.

The solution was to run composer install after mounting the volume. It's straight forward enough to do this by simply exec'ing into the container after bringing it up - running composer and any other package managers - then finally running the web server.

但是,我找到了一个更简洁的解决方案.我将 Dockerfile 中的最终 CMD 更改为:

However, I found a neater solution. I changed my final CMD in the Dockerfile to:

CMD bash -c "composer install && php artisan serve --host 0.0.0.0 --port 5001"

这将运行 composer install 并启动 web 服务器作为 docker-compose up 的最后一部分.

This will run composer install and bring up the web server as a final part of the docker-compose up.

此处提供解决方案的信用:Docker - 安装卷后执行命令

Credit for the solution here: Docker - Execute command after mounting a volume

这篇关于在 Dockerfile 中运行 composer install的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆