Docker 多阶段构建不在阶段之间复制

Docker multi-stage build not copying between stages

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

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

docker-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"

Docker 文件:

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  }' \
    | 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

当您的 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 树。