简体   繁体   English

无论如何使用 docker 作曲家图像和自定义 php?

[英]Is there anyway to use docker composer image with custom php?

I want to use docker composer image in multistage dockerfile for my laravel application in order to reduce its size.我想为我的 laravel 应用程序在多级 dockerfile 中使用 docker 作曲家图像以减小其大小。 The problem I need PHP 7.4 for my dependencies but the composer's PHP is 8.1.1.我的依赖项需要 PHP 7.4 的问题,但作曲家的 PHP 是 8.1.1。 Is there any way to customize this?有没有办法自定义这个? If there's no way to downgrade the PHP version of it, how can I reduce the size of my docker image and what should I use instead to install my project dependencies?如果没有办法降级它的 PHP 版本,我该如何减小我的 docker 映像的大小,我应该使用什么来安装我的项目依赖项?

I tried installing dependencies with --ignore-platform-reqs flag but the app doesn't work after the build.我尝试使用--ignore-platform-reqs标志安装依赖项,但应用程序在构建后无法运行。

FROM composer:latest

COPY database/ database/

COPY composer.json composer.json
COPY composer.lock composer.lock

RUN composer install

The Composer image is going to use whatever the latest version of PHP currently is, so composer:latest will never get you 7.4. Composer 映像将使用当前最新版本的 PHP,因此composer:latest永远不会为您提供 7.4。 You could scan backwards through the old Composer docker images until you find the most recent one that is based on 7.4, and then use that specific version of the image.您可以向后扫描旧的 Composer docker 映像,直到找到基于 7.4 的最新映像,然后使用该映像的特定版本。

FROM composer:1.10.19

Note however that you'll never get updated versions of composer this way.但是请注意,您永远不会以这种方式获得作曲家的更新版本。

Alternatively, if size is a concern then you could base your image on alpine:latest (which is tiny and currently uses 7.4 in its app repo) and then install PHP with apk add php-cli .或者,如果大小是一个问题,那么您可以将您的图像基于alpine:latest (它很小,目前在其应用程序仓库中使用 7.4),然后使用apk add php-cli安装 PHP。 This will give you an image of about 20mb, but sooner or later Alpine is going to update their base PHP to 8.0 and your image will break.这将为您提供大约 20mb 的图像,但 Alpine 迟早会将其基础 PHP 更新为 8.0,您的图像会损坏。

Probably your best option would be to base your image on one of the explicitly-versioned PHP containers like php:7.4-cli-alpine and then install the latest Composer into that image.可能您最好的选择是将您的映像基于显式版本化的 PHP 容器之一,例如php:7.4-cli-alpine ,然后将最新的 Composer 安装到该映像中。 This will give you much better control over what version of PHP runs in your image, and it'll be very easy to later copy this image to base it off 8.0 for upgrade testing.这将使您更好地控制在您的映像中运行的 PHP 版本,并且稍后复制此映像以使其基于 8.0 进行升级测试将非常容易。

I just ran into this recently and solved it with two steps.我最近遇到了这个问题,并通过两个步骤解决了它。

  1. Tell composer to use a specified version of PHP using the platform config.使用platform配置告诉作曲家使用 PHP 的指定版本。
  2. Tell composer to not generate the autoload, and do it manually in your final image or an image running the correct version of PHP.告诉作曲家不要生成自动加载,并在最终图像或运行正确版本的 PHP 的图像中手动执行。

This became a problem recently because, with PHP 8.1, the post-autoload-dump started throwing errors about Laravel's Collection class.这最近成为一个问题,因为在 PHP 8.1 中, post-autoload-dump开始抛出有关 Laravel 的Collection class 的错误。 The post autoload script is running Laravel code, meaning even though we just installed dependencies for v 7.X, we're executing the script with 8.1 still.后自动加载脚本正在运行 Laravel 代码,这意味着即使我们刚刚安装了 v 7.X 的依赖项,我们仍在使用 8.1 执行脚本。

There may be better ways to solve this, and if so please let me know,.可能有更好的方法来解决这个问题,如果有,请告诉我。 but here is what I did.但这就是我所做的。

Updated composer.json to specify the platform:更新了composer.json以指定平台:

"config": {
  "platform": {
    "php": "7.3.33" // Or your specific version
  }
},

Tell composer to skip autoload generation :告诉作曲家跳过自动加载生成

RUN composer install --no-autoloader

And last, in my final image:最后,在我的最终图像中:

RUN composer dump-autoload

The downside here is you must have composer installed in your final image, which in a way defeats the purpose of splitting the jobs during the build.这里的缺点是您必须在最终映像中安装 Composer,这在某种程度上违背了在构建期间拆分作业的目的。

You may choose instead to follow the recommendation from the Composer docker image readme and include it this way, and ditch the multi-stage build altogether.:您可以选择遵循Composer docker 映像自述文件中的建议并以这种方式包含它,并完全放弃多阶段构建:

COPY --from=composer /usr/bin/composer /usr/bin/composer

While I cannot confidently say there are not better options out there to address this problem, I hope this answer provides an explanation of why the problem exists, and points you in the right direction to solve it.虽然我不能自信地说没有更好的选择来解决这个问题,但我希望这个答案能够解释问题存在的原因,并为您指明解决问题的正确方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM