86·实战项目进阶

Web 应用容器化

Web 应用容器化

项目结构

my-web-app/
├── Dockerfile
├── docker-compose.yml
├── nginx.conf
├── src/
└── public/

Dockerfile

FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

nginx.conf

server {
    listen 80;
    root /usr/share/nginx/html;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

docker-compose.yml

services:
  web:
    build: .
    ports:
      - "80:80"
    restart: unless-stopped

小结

步骤说明
构建Node.js 构建静态文件
运行nginx 托管
SPAtry_files 配置

练习编辑器

bash
Loading...