node.js 加 nginx docker file
时间: 2023-11-03 14:54:15 浏览: 36
以下是一个使用 Node.js 和 Nginx 的 Dockerfile 示例:
```
# 基础镜像
FROM node:12-alpine AS builder
# 设置工作目录
WORKDIR /app
# 复制 package.json 和 package-lock.json 到工作目录中
COPY package*.json ./
# 安装依赖
RUN npm install --production
# 复制源码到工作目录中
COPY . .
# 执行构建命令
RUN npm run build
# 生成 Nginx 镜像
FROM nginx:alpine
# 复制 Nginx 配置文件到容器中
COPY nginx.conf /etc/nginx/nginx.conf
# 复制构建产物到 Nginx 的静态文件目录
COPY --from=builder /app/build /usr/share/nginx/html
# 暴露 80 端口
EXPOSE 80
# 启动 Nginx 服务
CMD ["nginx", "-g", "daemon off;"]
```
在上面的 Dockerfile 中,我们首先使用 Node.js 镜像作为构建环境,安装依赖并构建出静态资源,接着使用 Nginx 镜像作为应用运行环境,将构建产物复制到 Nginx 的静态文件目录中,并将容器的 80 端口暴露出来,最后启动 Nginx 服务。
在构建 Docker 镜像时,可以使用以下命令:
```
docker build -t my-app .
```
其中,`my-app` 是你要创建的镜像的名称,`.` 表示 Dockerfile 所在的目录。