Build Angular + docker – Why you need node.js

huangapple go评论84阅读模式
英文:

Build Angular + docker - Why you need node.js

问题

I want to create a Docker image with an Angular project.
我想创建一个包含 Angular 项目的 Docker 镜像。

I run the command "ng build" and the project is built in the dist folder.
我运行命令 "ng build",项目会构建在 dist 文件夹中。

I have seen a lot of tutorials on how to run an Angular project in docker. All tutorials say and show that you need to add node.js to the Docker image.
我看过很多关于如何在 Docker 中运行 Angular 项目的教程。所有教程都说需要将 node.js 添加到 Docker 镜像中。

Example:
示例:

services:
app:
image: node:18-alpine
服务:
应用程序:
镜像:node:18-alpine

I don’t understand why add node.js if you can build the project, add it to the image from the dist folder along with Nginx and the project will work.
我不明白为什么要添加 node.js,如果你可以构建项目,将它与 Nginx 一起从 dist 文件夹添加到镜像中,项目将正常运行。

Can you please explain why adding node.js to a Docker image?
你能解释一下为什么要将 node.js 添加到 Docker 镜像中吗?

I apologize if the question really seemed stupid to you.
如果这个问题让你觉得愚蠢,我道歉。

英文:

I want to create a Docker image with an Angular project.
I run the command "ng build" and the project is built in the dist folder.

I have seen a lot of tutorials on how to run an Angular project in docker. All tutorials say and show that you need to add node.js to the Docker image.

Example:

services:
   app:
     image: node:18-alpine

I don’t understand why add node.js if you can build the project, add it to the image from the dist folder along with Nginx and the project will work.

Can you please explain why adding node.js to a Docker image?

I apologize if the question really seemed stupid to you.

答案1

得分: 1

You need Node.JS to build the Angular project. You do not need it to serve the build files from the dist folder, if these are just static assets. Any webserver capable of serving static files will do.

Some thoughts:

However: For a production image, you probably want reproducible builds, that do not depend on a "correct" build on the Docker host.

  • So for development: build on the host if you like.

  • For production:

  1. Use a build step with a Node image and build the Angular application.
  2. Copy the built files (the dist directory) in a separate build step to a desired destination.
  3. Serve the files with any webserver.

Minimal prod example:

#stage 1

FROM node:latest as node
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build --prod

#stage 2

FROM nginx:alpine
COPY --from=node /app/dist/your-app /usr/share/nginx/html
英文:

You need Node.JS to build the Angular project. You do not need it to serve the build files from the dist folder, if these are just static assets. Any webserver capable of serving static files will do.

Some thoughts:

However: For a production image, you probably want reproducible builds, that do not depend on a "correct" build on the Docker host.

  • So for development: build on the host if you like.

  • For production:

  1. Use a build step with a Node image and build the Angular application.
  2. Copy the built files (the dist directory) in a seperate build step to a desired destination.
  3. Serve the files with any webserver.

Minimal prod example:

#stage 1

FROM node:latest as node
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build --prod

#stage 2

FROM nginx:alpine
COPY --from=node /app/dist/your-app /usr/share/nginx/html

huangapple
  • 本文由 发表于 2023年1月5日 05:57:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75011788.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定