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

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

问题描述

我似乎无法在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 ex ec 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.

更新
尝试将composer命令与以下内容结合使用:

Update Tried combining the composer commands with:

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容器的构建并同时运行,并且某种程度上并不能真正向刚开始的人们轻松地展示其幕后工作

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 后面,有四个步骤:


  • docker-compose build (如果需要),并且如果尚无现有映像,则创建映像( s)

  • docker-compose create (如果需要),并且如果还没有容器,则创建容器)

  • docker-compose start 启动现有容器

  • docker-compose日志记录容器的stderr和stdout

  • 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

所以您必须发现在那里,f请执行 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.

因此,当您尝试对 RUN 命令时,这是映像创建步骤的一部分。在开始步骤中安装的> composer.lock 和 composer.json ,由于没有安装文件,最终没有任何东西可以通过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.

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

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