Composer install 在 Dockerfile 中运行时不安装包 [英] Composer install doesn't install packages when running in Dockerfile

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

问题描述

我似乎无法在 Dockerfile 中运行 composer install,但我可以在构建映像并运行容器后在容器中运行.

I cannot seem to run composer install in Dockerfile but I can in the container after building an image and running the container.

这是来自 Dockerfile 的命令:

Here's the command from Dockerfile:

RUN composer require drupal/video_embed_field:1.5
RUN composer install --no-autoloader --no-scripts --no-progress

输出是:

Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update

但是在使用 docker-compose 运行容器之后:

But after running the container with docker-compose:

...
drupal:
    image: docker_image
    container_name: container
    ports:
      - 8081:80
    volumes:
      - ./container/modules:/var/www/html/web/modules

    links:
      # Link the DB container:
      - db

运行 docker exec composer install 将正确安装软件包:

running docker exec composer install will install the packages correctly:

Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 1 installs, 0 updates, 0 removals
...
Generating autoload files

我假设 composer.json 和 composer.lock 文件是正确的,因为我可以在容器中运行 composer install 命令而无需任何进一步的努力,但只能在容器运行之后.

I am assuming that the composer.json and composer.lock files are correct because I can run the composer install command in the container without any further effort, but only after the container is running.

更新尝试将作曲家命令与:

RUN composer require drupal/video_embed_field:1.5 && composer install

同样的问题,无需安装或更新".最终我想继续在 Dockerfile 中使用单独的 RUN 语句来利用 docker 缓存.

Same issue, "Nothing to install or update". Ultimately I would like to continue using seperate RUN statements in Dockerfile to take advantage of docker caching.

推荐答案

您的问题是因为 docker-compose 是为了编排多个 docker 容器构建并同时运行并且不知何故,它并没有真正轻松地向从 docker 开始的人们展示它在幕后所做的事情.

Your issue is coming from the fact that, docker-compose is meant to orchestrate multiple docker container build and run at the same time and it somehow is not really showing easily what it does behind the scene to people starting with docker.

docker-compose up 后面有四个步骤:

Behind a docker-compose up there are four steps:

  • docker-compose build 如果需要,如果还没有现有图像,则创建图像
  • docker-compose create 如果需要,如果还没有容器,则创建容器
  • docker-compose start 启动现有容器
  • docker-compose logs 记录容器的标准错误和标准输出
  • docker-compose build if needed, and if there is no existing image(s) yet, create the image(s)
  • docker-compose create if needed, and if there is no container(s) existing yet, create the container(s)
  • docker-compose start start existing container(s)
  • docker-compose logs logs stderr and stdout of the containers

所以你必须在那里发现,包含在你 Dockerfile 中的操作是在映像创建步骤中执行的.
在容器步骤开始时执行挂载文件夹时.

So what you have to spot on there, is the fact that action contained into you Dockerfile are executed at the image creation step.
When mounting folders is executed at start of containers step.

因此,当您尝试在 composer.lockcomposer.jsonRUN 命令时,这是图像创建步骤的一部分> 在开始步骤安装的,你最终没有任何东西可以通过 composer 安装,因为你的文件还没有安装在任何地方.

So when you try to use a RUN command, part of the image creation step, on files like composer.lock and composer.json that are mounted on starting step, you end up having nothing to install via composer because your files are not mounted anywhere yet.

如果你对那些文件进行 COPY 可能会真正让你到达某个地方,因为你将把作曲家文件作为图像的一部分.

If you do a COPY of those files that may actual get you somewhere, because you will then have the composer files as part of your image.

这就是说,请注意已安装的源文件夹将完全覆盖安装点,因此您最终可能会期待供应商文件夹而没有它.
理想情况下,您应该将其作为 ENTRYPOINT,这发生在容器启动的最后一步.

This said, be careful that the mounted source folder will totally override the mounting point so you could end up expecting a vendor folder and not have it.
What you should ideally do is to have it as the ENTRYPOINT, this one happens at the last step of the container booting.

这里是一个小的开发比较:一个 docker 镜像对于一个 docker 容器就像一个类对于一个类的实例一样—一个对象.
您的容器都是从可能很久以前构建的图像创建的.
Dockerfile 中的大部分步骤发生在映像创建时,而不是在容器启动时.
而 docker-compose 的大部分指令都是针对容器构建的自动化,包括文件夹的挂载.

Here is for a little developing comparison: a docker image is to a docker container what a class is to an instance of an class — an object.
Your container are all created from images built possibly long time before.
Most of the steps in your Dockerfile happens at image creation and not at container boot time.
While most of the instruction of docker-compose are aimed at the automatisation of the container build, which include the mounting of folders.

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

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