Vue (NPM) +单个 docker 中的 NGINX

发布于 2025-01-13 13:26:39 字数 663 浏览 3 评论 0原文

最近,我发现许多网站提出了使用所谓的“多阶段”docker 将 NPM 和 NGINX 封装到单个 dockerfile 中的解决方案。


# first stage builds vue 
FROM mhart/alpine-node:12 as build-stage
WORKDIR /app
COPY . .
RUN npm ci 
RUN npm run build 

# second stage copies only the static dist files to nginx html dir
FROM nginx:stable-alpine as production-stage
VOLUME /var/log/nginx
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]

但我不清楚。毕竟,docker 应该只托管一个进程,而在相关示例中,它运行 NPM 服务器并单独运行 NGINX - 我是否正确阅读了 Dockerfile 中的这些说明?

当将其托管在 Kubernetes 或 AWS ECS 等服务上时,采用“side-car”方法不是更合理吗?

Quite recently I have found many websites proposing solution to encapsulate a NPM and NGINX into a single dockerfile using so-called: "multi-stages" docker.


# first stage builds vue 
FROM mhart/alpine-node:12 as build-stage
WORKDIR /app
COPY . .
RUN npm ci 
RUN npm run build 

# second stage copies only the static dist files to nginx html dir
FROM nginx:stable-alpine as production-stage
VOLUME /var/log/nginx
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]

But it's not clear to me. After all, docker should host only one process, while in the examples in question it runs the NPM server and separately NGINX - am I reading these instructions in the Dockerfile correctly?

Isn't it more reasonable to take a "side-car" approach when hosting this on a service like Kubernetes or AWS ECS?

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

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

发布评论

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

评论(1

玉环 2025-01-20 13:26:39

当下面的代码行运行时,

COPY --from=build-stage /app/dist /usr/share/nginx/html

您只需复制编译后的 JS/HTML,然后通过 nginx 托管它。所以这里没有两个进程。当您运行 npm start 时,它会正常运行开发服务器,您不需要为生产构建运行该服务器。

When the below line of code runs

COPY --from=build-stage /app/dist /usr/share/nginx/html

You are just copying the compiled JS/HTML and then hosting it via nginx. So there is are no trwo processes here. When you run npm start it run a dev server normally, which you don't need to run for production builds.

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