1、docker pull nginx
2、Dockerfile
FROM nginx:alpine# 维护者信息 LABEL maintainer="Your Name <your@email.com>"# 将构建好的前端文件复制到Nginx的默认静态文件目录 COPY dist/ /usr/share/nginx/html/# 将自定义的Nginx配置文件复制到Nginx的配置目录 COPY nginx.conf /etc/nginx/conf.d/default.conf# 暴露80端口 EXPOSE 80# 启动Nginx服务 CMD ["nginx", "-g", "daemon off;"]
3、nginx.conf
server {listen 80;server_name localhost;# 根目录指向前端项目root /usr/share/nginx/html;index index.html;# 支持前端路由(如React Router、Vue Router的history模式)location / {try_files $uri $uri/ /index.html;}# 静态文件缓存配置location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {expires 30d;add_header Cache-Control "public, max-age=2592000";}# 禁止访问隐藏文件location ~ /\. {deny all;} }
4、docker build -t my-frontend-app:latest .
5、docker run -d -p 8080:80 --name frontend-container my-frontend-app:latest