在多阶段 docker 构建期间访问 nginx 端点

发布于 2025-01-11 00:39:25 字数 548 浏览 0 评论 0原文

我正在尝试通过 HTTP 抓取静态 html 站点,作为构建 docker 映像的一部分。当我尝试 curl http://localhost 时,连接被拒绝。当我删除 curl 语句并运行容器时,静态内容将按预期在本地主机上可用(包括在容器内运行 curl)。

有没有办法在构建过程中访问 nginx 端点?

FROM ubuntu AS indexer
RUN apt-get update && apt-get install -y nginx
RUN apt-get update && apt-get install -y curl
COPY --from=builder /workdir/build /usr/share/nginx/html
RUN service nginx start
RUN curl http://localhost > /tmp/index.html

我尝试在运行 curl 之前等待端口 80 可用,但这没有什么区别。

I'm trying to crawl a static html site via HTTP as part of building a docker image. When I try to curl http://localhost I get connection refused. When I remove the curl statement and run the container the static content is available at localhost as expected (including running curl inside the container).

Is there any way to access an nginx endpoint during a build?

FROM ubuntu AS indexer
RUN apt-get update && apt-get install -y nginx
RUN apt-get update && apt-get install -y curl
COPY --from=builder /workdir/build /usr/share/nginx/html
RUN service nginx start
RUN curl http://localhost > /tmp/index.html

I've tried waiting for port 80 to be available before I run curl but it makes no difference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上青苔 2025-01-18 00:39:25

在每个 RUN 语句之间,构建“机器”的状态被破坏,并为下一个 RUN 语句启动新的状态。因此,当您在一个 RUN 语句中启动 nginx 时,当您到达下一个 RUN 语句时,它就会消失。

为了完成您想要做的事情,nginx 需要在与爬行相同的 RUN 语句中启动。

像这样的事情

FROM ubuntu AS indexer
RUN apt-get update && apt-get install -y nginx
RUN apt-get update && apt-get install -y curl
COPY --from=builder /workdir/build /usr/share/nginx/html
RUN service nginx start && sleep 10 && curl http://localhost > /tmp/index.html

通过 nginx 而不是文件系统访问文件确实看起来有点奇怪。在您发布的(诚然有限的)示例中,您可以

RUN cp /usr/share/nginx/html/index.html /tmp/

这样做。

Between each RUN statement, the state of the build 'machine' is destroyed and a new one is started for the next RUN statement. So when you start nginx in one RUN statement, it's gone when you reach the next one.

To do what you're trying to do, nginx needs to be started in the same RUN statement as you do your crawling.

Something like this

FROM ubuntu AS indexer
RUN apt-get update && apt-get install -y nginx
RUN apt-get update && apt-get install -y curl
COPY --from=builder /workdir/build /usr/share/nginx/html
RUN service nginx start && sleep 10 && curl http://localhost > /tmp/index.html

It does seem a little weird that you access the files through nginx rather than the file system. In the (admittedly limited) example you've posted, you could just do

RUN cp /usr/share/nginx/html/index.html /tmp/

instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文