Files
schoolNews/docker/Dockerfile.web
2025-11-24 13:36:03 +08:00

89 lines
2.7 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ====================================
# 前端服务镜像 - School News Web
# 基于school-news-base-webNginx Alpine
# 注意dist目录需要在主机中先构建好
# ====================================
FROM school-news-base-web:latest
# 创建前端目录
RUN mkdir -p /usr/share/nginx/html/schoolNewsWeb
# 从主机复制已构建的dist目录
COPY schoolNewsWeb/dist/ /usr/share/nginx/html/schoolNewsWeb/
# 复制配置文件模板(可整个替换)
COPY schoolNewsWeb/public/app-config.js /app/config/app-config.js.template
# 确保dist中有默认配置文件如果build时没有复制
RUN if [ ! -f /usr/share/nginx/html/schoolNewsWeb/app-config.js ]; then \
cp /app/config/app-config.js.template /usr/share/nginx/html/schoolNewsWeb/app-config.js; \
fi
# 创建Nginx配置
RUN echo 'server {\n\
listen 80;\n\
server_name localhost;\n\
\n\
# 日志配置\n\
access_log /app/logs/nginx-access.log;\n\
error_log /app/logs/nginx-error.log;\n\
\n\
# 根路径重定向\n\
location = / {\n\
return 301 /schoolNewsWeb/;\n\
}\n\
\n\
# 前端应用\n\
location /schoolNewsWeb/ {\n\
alias /usr/share/nginx/html/schoolNewsWeb/;\n\
try_files $uri $uri/ /schoolNewsWeb/index.html;\n\
\n\
# 静态资源缓存\n\
location ~* \\.(js|css|png|jpg|jpeg|gif|ico|svg)$ {\n\
expires 1y;\n\
add_header Cache-Control "public, immutable";\n\
}\n\
}\n\
\n\
# 后端API代理\n\
location /schoolNewsServ/ {\n\
proxy_pass http://school-news-serv:8081;\n\
proxy_set_header Host $host;\n\
proxy_set_header X-Real-IP $remote_addr;\n\
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\
proxy_set_header X-Forwarded-Proto $scheme;\n\
\n\
# WebSocket支持\n\
proxy_http_version 1.1;\n\
proxy_set_header Upgrade $http_upgrade;\n\
proxy_set_header Connection "upgrade";\n\
\n\
# 超时设置\n\
proxy_connect_timeout 300s;\n\
proxy_send_timeout 300s;\n\
proxy_read_timeout 300s;\n\
}\n\
\n\
# 错误页面\n\
error_page 404 /schoolNewsWeb/index.html;\n\
error_page 500 502 503 504 /50x.html;\n\
location = /50x.html {\n\
root /usr/share/nginx/html;\n\
}\n\
}\n\
' > /etc/nginx/conf.d/default.conf
# 复制启动脚本
COPY schoolNewsWeb/docker/start.sh /app/start.sh
RUN chmod +x /app/start.sh
# 暴露端口
EXPOSE 80
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
CMD wget --quiet --tries=1 --spider http://localhost/schoolNewsWeb/ || exit 1
# 启动应用
CMD ["/app/start.sh"]