nginx 使用Docker多阶段构建

2mbi3lxu  于 2023-04-20  发布在  Nginx
关注(0)|答案(2)|浏览(214)

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

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容器?
也许还有其他方法可以实现这一点。

5q4ezhmt

5q4ezhmt1#

您在注解中澄清了您有三个独立的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部分。

koaltpgm

koaltpgm2#

你需要在同一个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/

相关问题