services: # ================= 后端:FastAPI + SQLAlchemy ================= # 构建上下文是后端代码根目录(本文件在 vue/ 下,../.. 即 sqlalchemy_fastapi_demo_1) fastapi: build: context: ../.. dockerfile: Dockerfile image: student-system-api:1.0 container_name: fastapi-backend ports: - "8004:8004" environment: APP_HOST: 0.0.0.0 APP_PORT: 8004 APP_RELOAD: "false" # MySQL 跑在宿主机上:容器里的 localhost 指容器自己,必须用 host.docker.internal MYSQL_HOST: host.docker.internal MYSQL_PORT: 3306 MYSQL_USER: root MYSQL_PASSWORD: "123456" MYSQL_NAME: 0914tw extra_hosts: - "host.docker.internal:host-gateway" # Linux 需要这行,Win/Mac Docker Desktop 自带 restart: unless-stopped # ================= 前端:Vue + Nginx ================= vue-frontend: build: context: . dockerfile: Dockerfile image: student-system-vue:1.0 container_name: vue-frontend ports: - "8080:80" # 浏览器访问 http://localhost:8080 environment: # 填服务名,走 compose 内部网络;若后端在宿主机上直接跑,改成 host.docker.internal BACKEND_HOST: fastapi BACKEND_PORT: 8004 depends_on: - fastapi restart: unless-stopped