docker-compose.yml 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. version: '3.7'
  2. services:
  3. # ==========================================
  4. # Frontend (Vite Dev Server)
  5. # ==========================================
  6. frontend:
  7. build:
  8. context: ./frontend
  9. ports:
  10. - "5173:5173" # Vite default port
  11. depends_on:
  12. - backend
  13. # Hot Reload requires binding volume locally
  14. volumes:
  15. - ./frontend:/app
  16. - /app/node_modules # Avoid overwriting node_modules
  17. environment:
  18. # Browser needs to access backend directly for CORS requests in Dev mode
  19. - VITE_API_BASE_URL=http://localhost:8000/api/v1
  20. restart: always
  21. # ==========================================
  22. # Nginx (Frontend Production)
  23. # ==========================================
  24. nginx:
  25. build:
  26. context: ./frontend
  27. target: production-stage
  28. ports:
  29. - "80:80"
  30. - "443:443"
  31. depends_on:
  32. - backend
  33. volumes:
  34. - certs_data:/etc/nginx/certs
  35. restart: always
  36. # ==========================================
  37. # Backend (FastAPI)
  38. # ==========================================
  39. backend:
  40. build:
  41. context: ./backend
  42. ports:
  43. - "8000:8000"
  44. environment:
  45. - TZ=Asia/Shanghai
  46. - MYSQL_SERVER=db
  47. - MYSQL_PORT=3306
  48. - MYSQL_USER=uap_user
  49. - MYSQL_PASSWORD=uap_pass
  50. - MYSQL_DB=uap_db
  51. - REDIS_HOST=redis
  52. - REDIS_PORT=6379
  53. - HYDRA_ADMIN_URL=http://hydra:4445
  54. # CORS: Add * to allow debugging from any origin if localhost fails
  55. # Also explictly allow 127.0.0.1 and localhost with port 5173
  56. - BACKEND_CORS_ORIGINS=["http://localhost:5173", "http://127.0.0.1:5173", "http://frontend:5173"]
  57. depends_on:
  58. db:
  59. condition: service_healthy
  60. redis:
  61. condition: service_healthy
  62. hydra:
  63. condition: service_started
  64. db-migration:
  65. condition: service_completed_successfully
  66. volumes:
  67. - ./backend:/app # Hot Reload for Backend too
  68. - certs_data:/app/certs
  69. restart: always
  70. # ==========================================
  71. # Database (MySQL)
  72. # ==========================================
  73. db:
  74. image: mysql:8.0
  75. container_name: uap_mysql
  76. command: --default-authentication-plugin=mysql_native_password
  77. restart: always
  78. environment:
  79. TZ: Asia/Shanghai
  80. MYSQL_ROOT_PASSWORD: root_password
  81. MYSQL_DATABASE: uap_db
  82. MYSQL_USER: uap_user
  83. MYSQL_PASSWORD: uap_pass
  84. ports:
  85. - "3308:3306"
  86. volumes:
  87. - db_data:/var/lib/mysql
  88. # - ./config/my.cnf:/etc/mysql/conf.d/my.cnf
  89. healthcheck:
  90. test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
  91. interval: 10s
  92. timeout: 5s
  93. retries: 5
  94. # ==========================================
  95. # Database Version Migration (Flyway)
  96. # ==========================================
  97. db-migration:
  98. image: flyway/flyway:9-alpine
  99. container_name: uap_migration
  100. depends_on:
  101. db:
  102. condition: service_healthy
  103. command: -connectRetries=60 -baselineOnMigrate=true migrate
  104. environment:
  105. FLYWAY_URL: jdbc:mysql://db:3306/uap_db
  106. FLYWAY_USER: root
  107. FLYWAY_PASSWORD: root_password
  108. volumes:
  109. - ./sql:/flyway/sql
  110. # ==========================================
  111. # Database Auto Backup (Sidecar)
  112. # ==========================================
  113. db-backup:
  114. image: fradelg/mysql-cron-backup
  115. container_name: uap_backup
  116. restart: always
  117. depends_on:
  118. - db
  119. environment:
  120. - MYSQL_HOST=db
  121. - MYSQL_PORT=3306
  122. - MYSQL_USER=root
  123. - MYSQL_PASS=root_password
  124. - CRON_TIME=0 3 * * *
  125. - MAX_BACKUPS=7
  126. - GZIP_COMPRESSION=true
  127. volumes:
  128. - ./backups:/backup
  129. # ==========================================
  130. # Redis
  131. # ==========================================
  132. redis:
  133. image: redis:alpine
  134. ports:
  135. - "6379:6379"
  136. restart: always
  137. healthcheck:
  138. test: ["CMD", "redis-cli", "ping"]
  139. interval: 10s
  140. timeout: 5s
  141. retries: 5
  142. # ==========================================
  143. # Ory Hydra Services
  144. # ==========================================
  145. hydra-migrate:
  146. image: oryd/hydra:v2.2.0
  147. environment:
  148. - TZ=Asia/Shanghai
  149. - DSN=postgres://hydra:secret@postgresd:5432/hydra?sslmode=disable&max_conns=20&max_idle_conns=4
  150. command: migrate sql -e --yes
  151. depends_on:
  152. postgresd:
  153. condition: service_healthy
  154. restart: on-failure
  155. hydra:
  156. image: oryd/hydra:v2.2.0
  157. depends_on:
  158. hydra-migrate:
  159. condition: service_completed_successfully
  160. ports:
  161. - "4444:4444"
  162. - "4445:4445"
  163. - "5555:5555"
  164. command: serve all --dev
  165. environment:
  166. - TZ=Asia/Shanghai
  167. - DSN=postgres://hydra:secret@postgresd:5432/hydra?sslmode=disable&max_conns=20&max_idle_conns=4
  168. - URLS_SELF_ISSUER=http://127.0.0.1:4444
  169. - URLS_CONSENT=http://localhost:5173/consent
  170. - URLS_LOGIN=http://localhost:5173/login
  171. - URLS_LOGOUT=http://localhost:5173/login
  172. - SECRETS_SYSTEM=youReallyNeedToChangeThis
  173. - OIDC_SUBJECT_IDENTIFIERS_SUPPORTED_TYPES=public,pairwise
  174. - OIDC_SUBJECT_IDENTIFIERS_PAIRWISE_SALT=youReallyNeedToChangeThis
  175. - SERVE_COOKIES_SAME_SITE_MODE=Lax
  176. - SERVE_COOKIES_SAME_SITE_LEGACY_WORKAROUND=true
  177. - SERVE_PUBLIC_CORS_ENABLED=true
  178. - SERVE_PUBLIC_CORS_ALLOWED_ORIGINS=*
  179. - SERVE_PUBLIC_CORS_ALLOWED_METHODS=POST,GET,PUT,DELETE,PATCH,OPTIONS
  180. - SERVE_PUBLIC_CORS_ALLOWED_HEADERS=Authorization,Content-Type
  181. - SERVE_ADMIN_CORS_ENABLED=true
  182. - SERVE_ADMIN_CORS_ALLOWED_ORIGINS=*
  183. postgresd:
  184. image: postgres:15
  185. environment:
  186. - TZ=Asia/Shanghai
  187. - POSTGRES_USER=hydra
  188. - POSTGRES_PASSWORD=secret
  189. - POSTGRES_DB=hydra
  190. volumes:
  191. - postgres_data:/var/lib/postgresql/data
  192. healthcheck:
  193. test: ["CMD-SHELL", "pg_isready -U hydra"]
  194. interval: 10s
  195. timeout: 5s
  196. retries: 5
  197. start_period: 10s
  198. volumes:
  199. db_data:
  200. postgres_data:
  201. certs_data: