使用dockerfile安装Composer [英] Composer install with dockerfile

查看:132
本文介绍了使用dockerfile安装Composer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对docker很陌生,我尝试在Dockerfile中自动执行composer安装,但似乎在安装时无法cd进入我的应用程序,这是怎么回事?也许还有另一种更好的方法可以做到这一点?

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? Or maybe there is another better way to do that?

我的docker-compose.yml

my 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

我的Dockerfile

my 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

运行此命令后的输出:

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

如果我尝试从Dockerfile中删除最后一行,请在运行以下命令后将其启动并运行:

If I try to remove the last line from my Dockerfile, once it's up and running if I run this command:

docker-compose exec --user www-data php-fpm-app bash -c'cd/application/app&&作曲家安装"

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

它起作用了,我不明白为什么我的Dockerfile无法做到这一点.

It works, I don't understand why I can't do this with my 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

推荐答案

这是因为直到构建完成才挂载卷.只需使用COPY将 only composer.json复制到该文件夹​​中,然后照常运行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.

这篇关于使用dockerfile安装Composer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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