English 中文(简体)
在 Dockerfile 中运行 composer install
原标题:Running composer install within a 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

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

这个帖子似乎指向可能的问题,但并没有真正提供解决方案:运行Dockerfile时,Composer安装不安装软件包

最佳回答

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运行install时,它会在构建时正确地安装所有依赖项,然后,在运行docker-compose up时,我挂载主机目录到容器中并覆盖了所有更改。

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更改为:

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

这将运行Composer安装并作为docker-compose up的最后一部分启动Web服务器。

此解决方案的来源: Docker - 挂载卷后执行命令

问题回答

你也可以使用官方的DockerHub Composer镜像。

这是一个多阶段构建的示例,其中使用独立容器先运行Composer。生成的 /app/vendor 将复制到您最终镜像中想要的任何位置。

FROM composer as builder
WORKDIR /app/
COPY composer.* ./
RUN composer install
...
FROM php:7.1-fpm-alpine
...
COPY --from=builder /app/vendor /var/www/vendor

如果您不想在Dockerfile中使用该命令,我们发现最简单的方法是将其添加到我们的docker-compose文件中:

composer_installation:
  container_name: composer_installation
  image: composer
  volumes:
    - ./:/app
  command: composer install --ignore-platform-reqs

更新有点慢,可能是因为正在与PHP容器同步。

我使用这一指挥,并产生供应商:

docker run --rm -it --volume $(pwd):/app prooph/composer:7.2 install --ignore-platform-reqs




相关问题
Unable to connect to docker container inside windows server

As title. I am developing a system with many docker images with ASP.Net MVC core projects. I am publishing these docker images into a docker engine installed on Windows Server OS, and I found that I ...

Only can see postgreSQL as an admin

After installed Wsl and Docker, can t access PSQL and other things. I was studying Docker, and installed the latest version. So far so good, I received an error about the WSL version, saw some ...

make docker-compose block until healthy without `depends_on`

I am working with a team that uses docker-compose to start a set of helper services, but does not use docker when the code is being developed. Here, docker-compose is just a convenient way to run the ...

change grafana port using docker host network

I am trying to spin up a grafana container in docker, however my setup in docker does not allow ipv4 forwarding and thus I cannot use the default bridge network in docker. All I can use is the host ...

Pip instalation for Python 3.11 in docker

I have Dockerfile to build image for Django project that uses Python 3.11 and runs in poetry environment (see content below). Problem is when I try to donwload get-pip.py file and run it using Python, ...

在 Dockerfile 中运行 composer install

我正在尝试将我的Laravel应用程序进行Docker化。 该应用程序已经构建并在Git中,但我将vendor文件夹添加到了.gitignore中。 我添加了一个Dockerfile,看起来像这样: FROM php:7.1-fpm-alpine RUN apk update ...

热门标签