简体   繁体   English

如何在 docker 多阶段构建中从一个图像复制到另一个图像?

[英]How to copy from one image to another in a docker multistaged build?

I want to copy the vendor folder from the composer image to another php image during a multistaged build.我想在多阶段构建期间将vendor文件夹从作曲家图像复制到另一个 php 图像。

My Dockerfile looks like this:我的 Dockerfile 看起来像这样:

FROM composer
WORKDIR /tmp/composer-vendors/
COPY composer.lock composer.json ./
RUN composer install --ignore-platform-reqs
RUN pwd && ls

FROM php:7.3-fpm-alpine
WORKDIR /var/www/html
RUN pwd && ls
COPY --from=composer /tmp/composer-vendors/vendor ./vendor
CMD ["php-fpm"]

The RUN pwd && ls is only there to show that the files are indeed there. RUN pwd && ls仅用于显示文件确实存在。

Yet the copy --from=composer fails stating:然而, copy --from=composer未能说明:

Step 9/10 : COPY --from=composer /tmp/composer-vendors/vendor ./vendor
COPY failed: stat /var/lib/docker/overlay2/c0cece8b4ffcc3ef3f6ed26c3131ae94813acffd5034b359c2ea6aed922f56ee/merged/tmp/composer-vendors/vendor: no such file or directory

What am I doing wrong?我究竟做错了什么?


My example composer.json :我的示例composer.json

{
  "name": "kopernikus/multistage-copy-issue",
  "require": {
    "nesbot/carbon": "^2.36"
  }
}

You have to alias the image as otherwise docker uses the base image as provided by composer and not the image you built.您必须为图像别名,否则 docker 使用composer提供的基本图像,而不是您构建的图像。

This means you need to change your FROM statement:这意味着您需要更改 FROM 语句:

FROM composer as BUILDER

and reference your image:并参考您的图片:

COPY --from=BUILDER /tmp/composer-vendors/vendor ./vendor

The alias could be anything, I used BUILDER as an example.别名可以是任何东西,我以BUILDER为例。 In fact, you could even reuse the name:事实上,你甚至可以重用这个名字:

FROM composer AS composer

though that might lead to unexpected behavior if people are not expecting a modified image.尽管如果人们不期待修改后的图像,这可能会导致意外行为。

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

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