2

我正在尝试创建一个多阶段构建,其中第一阶段为主题安装纱线,第二阶段为 Drupal 设置 PHP 环境。

当我查看输出时,看起来 yarn install 正在运行,但COPY底部附近的命令并未将其复制到 PHP 映像中。如果我是对的,那么应该在我的本地机器上创建 node_modules 目录吗?

码头工人-compose.yml:

version: '3.7'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./:/var/www/html:cached
    env_file:
      - ./local-development.env
    ports:
      - "8888:80"
  db:
    image: mysql:5.7
    env_file:
      - ./local-development.env
    ports:
      - "3306:3306"

Dockerfile:

FROM node:latest as yarn-install
WORKDIR /app
COPY ./web/themes/material_admin_mine ./
RUN yarn install --verbose --force;

# from https://www.drupal.org/docs/8/system-requirements/drupal-8-php-requirements
FROM php:7.2-apache

# Install & setup Xdebug
RUN yes | pecl install xdebug \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_connect_back=0' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_host=docker.for.mac.localhost' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_port=9000' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_handler=dbgp' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_mode=req' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.remote_autostart=1' >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo 'xdebug.idekey=PHPSTORM' >> /usr/local/etc/php/conf.d/xdebug.ini

# Install git & mysql-client for running Drush
RUN apt update; \
apt install -y \
git \
mysql-client

# install the PHP extensions we need
RUN set -ex; \
\
if command -v a2enmod; then \
    a2enmod rewrite; \
fi; \
\
savedAptMark="$(apt-mark showmanual)"; \
\
apt-get update; \
apt-get install -y --no-install-recommends \
    libjpeg-dev \
    libpng-dev \
    libpq-dev \
    unzip \
    git \
; \
\
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer; \
\
docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr; \
docker-php-ext-install -j "$(nproc)" \
    gd \
    opcache \
    pdo_mysql \
    pdo_pgsql \
    zip \
; \
\
# reset apt-mark's "manual" list so that "purge --auto-remove" will remove all build dependencies
apt-mark auto '.*' > /dev/null; \
apt-mark manual $savedAptMark; \
ldd "$(php -r 'echo ini_get("extension_dir");')"/*.so \
    | awk '/=>/ { print $3 }' \
    | sort -u \
    | xargs -r dpkg-query -S \
    | cut -d: -f1 \
    | sort -u \
    | xargs -rt apt-mark manual; \
\
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
rm -rf /var/lib/apt/lists/*

# set recommended PHP.ini settings
# see https://secure.php.net/manual/en/opcache.installation.php
RUN { \
    echo 'opcache.memory_consumption=128'; \
    echo 'opcache.interned_strings_buffer=8'; \
    echo 'opcache.max_accelerated_files=4000'; \
    echo 'opcache.revalidate_freq=60'; \
    echo 'opcache.fast_shutdown=1'; \
    echo 'opcache.enable_cli=1'; \
} > /usr/local/etc/php/conf.d/opcache-recommended.ini

# Various packages required to run Gulp in the theme directory
# gnupg is require for nodejs
RUN apt update; \
apt install gnupg -y; \
apt install gnupg1 -y; \
apt install gnupg2 -y; \
cd ~; \
curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh; \
bash nodesource_setup.sh; \
apt install nodejs -y; \
npm install gulp-cli -g -y; \
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - ;\
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list; \
apt update && apt install yarn -y;

WORKDIR /var/www/html
COPY --from=0 /app ./web/themes/material_admin_mine
4

1 回答 1

6

当您的 Dockerfile 以以下结尾时:

WORKDIR /var/www/html
COPY --from=0 /app ./web/themes/material_admin_mine

这实际上应该将数据从第一个构建阶段复制到最终图像。但是当你启动容器时

volumes:
  - ./:/var/www/html:cached

目录树中的所有内容/var/www/html,包括最后的 COPY 步骤,都被隐藏并替换为主机上当前目录中的内容。如果你把它想象成一个副本,它是一个单向副本到容器中;以后的更改将被复制回主机,但是没有任何东西可以将映像中的内容与您之前在启动时目录中的内容同步。

Dockerfile 本质上不会影响主机文件系统内容。在您的情况下,听起来主机内容对于您的应用程序来说是次要的。考虑到第一阶段的内容,我只需yarn install在主机上运行该步骤并完成它(您甚至可能已经拥有可用的 Node 和 Yarn)。否则,您需要一个更有选择性的volumes:部分,仔细尝试避免覆盖该目录;您可能能够安装./web/src:/var/www/html/web/src仅包含您的应用程序代码并避免隐藏.../web/themes树的东西。

于 2019-01-02T11:00:57.580 回答