简体   繁体   English

使用 dockerfile 安装 Composer

[英]Composer install with dockerfile

I'm pretty new with docker, I try to automatically execute composer install within my Dockerfile but it seems that I can't cd into my application while installing, what's wrong?我对 docker 很陌生,我尝试在我的 Dockerfile 中自动执行 composer install 但似乎我在安装时无法 cd 进入我的应用程序,怎么了? Or maybe there is another better way to do that?或者也许有另一种更好的方法来做到这一点?

my docker-compose.yml我的 docker-compose.yml

version: "3.1"
services:

    app:
      image: nginx:alpine
      container_name: app
      working_dir: /application
      volumes:
          - ./Projects/app:/application/app
          - ./Docker/nginx/app.conf:/etc/nginx/conf.d/app.conf
      ports:
          - "8080:8080"

    php-fpm-app:
      build: Docker/php-fpm-app
      container_name: php-fpm-app
      working_dir: /application
      volumes:
          - ./Projects:/application
          - ./Docker/php-fpm-app/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini

my Dockerfile我的 Dockerfile

FROM phpdockerio/php72-fpm:latest
WORKDIR "/application"

# Fix debconf warnings upon build
ARG DEBIAN_FRONTEND=noninteractive

RUN mkdir -p /var/www/.composer \
    && chown -R www-data:www-data /var/www/.composer

USER www-data

RUN cd /application/app; composer install

The output after I run this command:运行此命令后的输出:

docker-compose up -d docker-compose up -d

Step 6/6 : RUN cd /application/app; composer install
 ---> Running in ac53e653af46
/bin/sh: 1: cd: can't cd to /application/app
Composer could not find a composer.json file in /application
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section
ERROR: Service 'php-fpm-app' failed to build: The command '/bin/sh -c cd /application/app; composer install' returned a non-zero code: 1

If I try to remove the last line from my Dockerfile, once it's up and running if I run this command:如果我尝试从我的 Dockerfile 中删除最后一行,一旦它启动并运行,如果我运行以下命令:

docker-compose exec --user www-data php-fpm-app bash -c 'cd /application/app && composer install' docker-compose exec --user www-data php-fpm-app bash -c 'cd /application/app && composer install'

It works, I don't understand why I can't do this with my Dockerfile.它有效,我不明白为什么我不能用我的 Dockerfile 做到这一点。

=========================== Finally I find a way to execute a script but I don't see the output, so if the script last for many secondes/minutes I won't know when it's done. ==========================最后我找到了一种执行脚本的方法,但我没有看到输出,所以如果脚本最后很多秒/分钟我都不知道什么时候完成。

ADD ./setup.sh /setup.sh
RUN chmod +x /setup.sh
CMD ["sh", "/setup.sh"]

I decided to execute this script manually once all it's up and running我决定在它启动并运行后手动执行这个脚本

sh ./setup.sh

You should not run composer install in Dockerfile.你不应该在 Dockerfile 中运行 composer install。 It will fail because you have not created/synced volume from the local to container yet.它会失败,因为您尚未从本地创建/同步卷到容器。 Thus all files including composer.json is not placed under /var/www/html.因此,包括 composer.json 在内的所有文件都不会放在 /var/www/html 下。

What you can do is to add 1 line (command) into your docker-compose.yaml file inside the service eg.您可以做的是将 1 行(命令)添加到服务内的 docker-compose.yaml 文件中,例如。

services:
  api-php-fpm:
    build: ./docker/php-fpm
    command: sh -c "composer install"

This is because the volumes aren't mounted until the build is complete.这是因为在构建完成之前不会安装卷。 Just use COPY to copy only the composer.json into the folder, and run the composer install as normal.只需使用 COPY将 composer.json 复制到文件夹中,然后正常运行 composer install。

Referencing the Documentation for Dockerfiles you need to edit your Dockerfile.引用为Dockerfiles文档,你需要编辑Dockerfile。

it should be:它应该是:

WORKDIR /application/app
RUN composer install

Does this fix your problem?这能解决您的问题吗?

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

相关问题 在 Dockerfile 中运行 composer install - running composer install in Dockerfile 从 dockerfile 安装 PHP composer - Install PHP composer from a dockerfile 使用dockerfile安装COMPOSER.json - COMPOSER.json install with dockerfile 应从哪个 Dockerfile 调用 composer install - From which Dockerfile should composer install be called from 如何通过Dockerfile中的Composer for Symfony项目安装依赖项? - How to install dependencies via Composer for Symfony project in Dockerfile? 如何在 php 容器中从 dockerfile 运行 composer install - How run composer install from dockerfile in php container 为什么在 dockerfile 中运行的 composer install 命令中看不到卷目录中的 composer.json 文件? - Why composer.json file from volume directory is not visible in composer install command run in dockerfile? => 错误 [8/9] 在 dockerfile symfony php 项目中运行 cd /var/www && composer install && npm install && npm run build - => ERROR [8/9] RUN cd /var/www && composer install && npm install && npm run build in a dockerfile symfony php project dockerfile:`composer install --no-dev 安装开发依赖项,然后立即删除它们 - dockerfile: `composer install --no-dev installs dev-dependencies, then deletes them straight away after Dockerfile 安装 amqp 失败 - Dockerfile install amqp failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM