英文:
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:
- Use a build step with a Node image and build the Angular application.
- Copy the built files (the dist directory) in a separate build step to a desired destination.
- 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:
- Use a build step with a Node image and build the Angular application.
- Copy the built files (the dist directory) in a seperate build step to a desired destination.
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论