简体   繁体   English

应从哪个 Dockerfile 调用 composer install

[英]From which Dockerfile should composer install be called from

I need to call composer install and unsure from which Dockerfile to call it from - Dockerfile-apache or Dockerfile-php-fpm ?我需要调用composer install并不确定从哪个 Dockerfile 调用它 - Dockerfile-apacheDockerfile-php-fpm

Should I install composer in Dockerfile-apache (and PHP CLI?) and run it from there?我应该在Dockerfile-apache (和 PHP CLI 吗?)中安装 composer 并从那里运行它?

Running composer install from Dockerfile-php-fpm gives me this: Composer could not find a composer.json file in /var/www/html从 Dockerfile-php-fpm 运行 composer install 给我这个: Composer could not find a composer.json file in /var/www/html

Docker-php-fpm

# Do we target a specific version? ie. 7.4.25?
FROM php:7.4-fpm

# Need to add zip and other extensions
RUN buildDeps=" \
        libonig-dev \
        libzip-dev \
        libxml2-dev \ 
    " \ 
&& apt-get -y update \
&& apt-get install -y $buildDeps zip libicu-dev \ 
&& docker-php-ext-configure intl \
&& docker-php-ext-install intl mbstring json mysqli opcache pdo pdo_mysql xml \ 
&& apt-get purge -y --auto-remove $buildDeps \
&& rm -r /var/lib/apt/lists/*

# Copying of base conf files
COPY docker/php-fpm/conf/php-fpm.conf /usr/local/etc
COPY docker/php-fpm/conf/php.ini-development /usr/local/etc/php/php.ini
COPY docker/php-fpm/conf/www.conf /usr/local/etc/php-fpm.d

# Install Composer.
RUN curl -sS https://getcomposer.org/installer | php \
        && mv composer.phar /usr/local/bin/ \
        && ln -s /usr/local/bin/composer.phar /usr/local/bin/composer

EXPOSE 9000

Docker-apache : Docker-apache

FROM httpd:2.4

# Copy config files
COPY docker/apache/conf/httpd.conf /usr/local/apache2/conf
COPY docker/apache/conf/httpd-vhosts.conf /usr/local/apache2/conf/extra

# Do something about log rotate?

# Create vhost directory
WORKDIR /var/www/html

#Set our application folder as an environment variable
ENV APP_HOME /var/www/html

#copy files
COPY bin ${APP_HOME}/bin
COPY config ${APP_HOME}/config
COPY plugins ${APP_HOME}/plugins
COPY src ${APP_HOME}/src
COPY webroot ${APP_HOME}/webroot
COPY .htaccess ${APP_HOME}
COPY index.php ${APP_HOME}
COPY composer.json ${APP_HOME}
COPY composer.lock ${APP_HOME}

Edit #1编辑#1

docker-compose.yml

version: "3.2"
services:
  php-fpm:
    container_name: php-fpm
    build:
      context: .
      dockerfile: Dockerfile-php-fpm
    networks:
      - backend
    ports:
      - "9000:9000"
  apache:
    container_name: httpd
    build:
      context: .
      dockerfile: Dockerfile-apache
    depends_on:
      - php
    networks:
      - frontend
      - backend
    ports:
      - "80:80"
      - "443:443"
networks:
  frontend:
  backend:

Edit #2编辑#2

This docker-compose file enables the communication via a common volume.这个 docker-compose 文件允许通过公共卷进行通信。 I just need to compile, build and copy the files in Apache at this point.我此时只需要编译构建并复制Apache中的文件即可。

version: "3.9"
services:
  php-fpm:
    container_name: php-fpm
    build:
      context: .
      dockerfile: Dockerfile-php-fpm
    volumes:
      - mydata:/var/www/html:rw
    networks:
      - backend
    ports:
      - "9000:9000"
  apache:
    container_name: httpd
    build:
      context: .
      dockerfile: Dockerfile-apache
    depends_on:
      - php
    volumes:
      - mydata:/var/www/html:rw
    networks:
      - frontend
      - backend
    ports:
      - "80:80"
      - "443:443"
networks:
  frontend:
  backend:
volumes:
  mydata:

I would go with neither of the above;我会 go 以上都没有; instead, run composer install locally and copy the resulting vendor directory as part of your application .相反,在本地运行composer install并将生成的vendor目录复制为您的应用程序的一部分

Fetching dependencies is fundamentally part of building an application, not part of running it, so Composer shouldn't even be installed on a production host or container.获取依赖项基本上是构建应用程序的一部分,而不是运行应用程序的一部分,因此 Composer 甚至不应该安装在生产主机或容器上。 If you were writing a C application which needed to be compiled with gcc , you would run that as an earlier step, and then copy the binary into the container;如果您正在编写一个需要使用 gcc 编译的gcc应用程序,您可以将其作为较早的步骤运行,然后将二进制文件复制到容器中; composer install can be treated the same way. composer install可以用同样的方式处理。

So for instance, you might have a build script (to run manually, or in a CI server like Jenkins, Github Actions, Azure DevOps, etc) that went through the following steps:因此,例如,您可能有一个构建脚本(手动运行,或在 Jenkins、Github Actions、Azure DevOps 等 CI 服务器中运行),它经历了以下步骤:

  1. Clone the repo from a git repository从 git 存储库克隆 repo
  2. Check out the latest tag查看最新标签
  3. Run composer install运行composer install
  4. Run a script to minify the client-side JS运行脚本来缩小客户端 JS
  5. Run docker-composer , copying the source code, minified JS, and vendor directory运行docker-composer ,复制源代码、缩小的 JS 和vendor目录

The software inside the Docker container therefore only needs to be able to run the application, not build it.因此,Docker容器中的软件只需要能够运行该应用程序,而不需要构建它。

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

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