React项目Docker部署的简单配置

先远程服务器上已Docker部署后端服务,在此基础上(同一台机器)补充前端的Docker部署。

主要思路还是使用nginx转发后端请求,流程如下:

  1. 项目根目录新增Dockerfile, docker-compose.yml和nginx.conf
bash 复制代码
FROM nginx:alpine

# 复制静态资源
COPY ./dist /usr/share/nginx/html

# 复制 Nginx 配置
COPY ./nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
XML 复制代码
version: '3.8'

services:
  frontend:
    build: .
    ports:
      - "80:80"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    restart: unless-stopped
bash 复制代码
server {
    listen 80;
    server_name localhost;

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        try_files  $uri  $ uri/ /index.html;
    }

    location /api/v1/ {
        proxy_pass http://host.docker.internal:8000/api/v1/;  # 这里填你实际后端的端口+转发路径
        proxy_set_header Host  $host;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto  $scheme;
    }
}
  1. npm run build打包后将dist目录和这三个文件放到同一个文件夹下,将这个文件夹发到远程机器上,命令行进入该目录下,运行命令:
bash 复制代码
docker compose up -d
  1. 可进一步查看log,没报错后访问ip看到页面即部署成功

这只是个很基本的部署...

相关推荐
Agent手记1 分钟前
异常考勤智能预警与处理与流程优化方案 | 基于企业级Agent的超自动化实战教程
运维·人工智能·ai·自动化
cen__y27 分钟前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
dapeng-大鹏3 小时前
KVM+LVM 零停机在线扩容 Ubuntu 根分区:从磁盘添加到逻辑卷扩展完整
linux·运维·ubuntu·磁盘空间扩展
乐维_lwops3 小时前
案例解读|运维监控助力某大型卷烟厂构建高效运维监控体系
运维·运维案例
JiaWen技术圈3 小时前
网站用户注册行为验证码方案
运维·安全
仙柒4153 小时前
Docker存储原理
运维·docker·容器
DolphinDB3 小时前
漫长人工,耗费存储?用 BackupRestore 模块一站式解决跨环境数据同步难题
运维·后端·架构
神奇椰子6 小时前
[特殊字符] 服务器搭建网站完整教程
运维·服务器
慧都小妮子6 小时前
告别看图抓数据:DeviceXPlorer OPC Server 助力数据自动化管理
运维·物联网·自动化·takebishi·dxpserver·opc server
Wpa.wk6 小时前
APP自动化-Appium环境安装
运维·appium·自动化