使用Docker多阶段构建



我有两个容器用于两个独立的站点和一个容器用于nginx。我所要做的就是将每个站点的构建文件复制到nginx容器中的/usr/share/nginx/html/<site_name>/。我想为每个站点保持单独的Dockerfile,并将站点容器命名为builder_onebuilder_two,以便从nginxDockerfile中复制文件:

FROM nginx:latest
COPY ./conf.d/ /etc/nginx/conf.d/
RUN mkdir /usr/share/nginx/html/site_one
RUN mkdir /usr/share/nginx/html/site_two
COPY --from=builder_one /usr/src/site_one/build/ /usr/share/nginx/html/site_one/
COPY --from=builder_two /usr/src/site_two/build/ /usr/share/nginx/html/site_two/

然而,我得到一个错误:

------
> [dockernginx-nginx] FROM docker.io/library/builder_two:latest:
------
failed to solve: failed to load cache key: pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed

当运行COPY命令时失败,因为它试图从Docker注册表中拉容器。为什么?我如何正确地将文件复制到nginx容器?


也许有另一种方法我可以做到这一点?

您在注释中澄清您有三个独立的dockerfile,并以FROM base-image AS builder_one语法开始前两个文件。别名语法只适用于单个Dockerfile;别名不会作为镜像的一部分被持久化,也不能从其他dockerfile中引用。

COPY --from也可以采用完整的图像名称。对于您描述的特定设置,您需要使用已知标记

构建每个组件图像。
docker build -t myname/builder-one ./site_one
docker build -t myname/builder-two ./site_two

,然后在最终的Dockerfile中,使用这些图像名称

FROM nginx:latest
COPY ./conf.d/ /etc/nginx/conf.d/
COPY --from=myname/builder-one /usr/src/site_one/build/ /usr/share/nginx/html/site_one/
COPY --from=myname/builder-two /usr/src/site_two/build/ /usr/share/nginx/html/site_two/

组件图像不需要FROM ... AS语法,您通常可以删除Dockerfile最后阶段的FROM行中的AS alias部分。

您需要在同一个docker文件中声明以前构建的映像名称。

在你的例子中:

FROM <builder_one_image_name> as builder_one
FROM <builder_two_image_name> as builder_two
FROM nginx:latest
COPY ./conf.d/ /etc/nginx/conf.d/
RUN mkdir /usr/share/nginx/html/site_one
RUN mkdir /usr/share/nginx/html/site_two
COPY --from=builder_one /usr/src/site_one/build/ /usr/share/nginx/html/site_one/
COPY --from=builder_two /usr/src/site_two/build/ /usr/share/nginx/html/site_two/

相关内容

  • 没有找到相关文章

最新更新