Skip to content

Commit 859c198

Browse files
authored
修复docker部署错误 (#44)
* build: fix docker deploy error * 更新依赖管理 * 更新docker相关配置 * 修复配置文件 * 优化docker脚本文件 * 添加fsm_server镜像版本
1 parent 0f17acc commit 859c198

13 files changed

+87
-96
lines changed

.pre-commit-config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ repos:
2626
- '-o'
2727
- 'requirements.txt'
2828
- '--without-hashes'
29+
- '-G'
30+
- 'lint'
2931
files: ^pdm.lock$
3032
- id: pdm-lock-check

Dockerfile

+14-7
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@ RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debi
88
&& sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list.d/debian.sources
99

1010
RUN apt-get update \
11-
&& apt-get install -y --no-install-recommends gcc python3-dev \
12-
&& rm -rf /var/lib/apt/lists/*
13-
14-
RUN pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple \
11+
&& apt-get install -y --no-install-recommends gcc python3-dev supervisor \
12+
&& rm -rf /var/lib/apt/lists/* \
13+
# 某些包可能存在同步不及时导致安装失败的情况,可更改为官方源:https://pypi.org/simple
14+
&& pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple \
1515
&& pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple \
16+
&& pip install gunicorn wait-for-it -i https://mirrors.aliyun.com/pypi/simple
17+
18+
ENV TZ="Asia/Shanghai"
19+
20+
RUN mkdir -p /var/log/fastapi_server \
21+
&& mkdir -p /var/log/supervisor \
22+
&& mkdir -p /etc/supervisor/conf.d
1623

17-
ENV TZ = Asia/Shanghai
24+
COPY deploy/supervisor.conf /etc/supervisor/supervisord.conf
1825

19-
RUN mkdir -p /var/log/fastapi_server
26+
COPY deploy/fastapi_server.conf /etc/supervisor/conf.d/
2027

2128
EXPOSE 8001
2229

23-
CMD ["uvicorn", "backend.main:app", "--host", "127.0.0.1", "--port", "8000"]
30+
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8001"]

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
fastapi dev main.py
4949
```
5050

51-
9. 浏览器访问: http://127.0.0.1:8000/api/v1/docs
51+
9. 浏览器访问: http://127.0.0.1:8000/docs
5252

5353
---
5454

@@ -59,12 +59,13 @@
5959
```shell
6060
cd deploy/docker-compose/
6161
62-
cp .env.server ../../../backend/.env
62+
cp .env.server ../../backend/.env
6363
```
6464

6565
2. 执行一键启动命令
6666

6767
```shell
68+
# 根据情况使用 sudo
6869
docker-compose up -d --build
6970
```
7071

backend/core/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
class Settings(BaseSettings):
13-
model_config = SettingsConfigDict(env_file=f'{BasePath}/.env', env_file_encoding='utf-8', case_sensitive=True)
13+
model_config = SettingsConfigDict(env_file=f'{BasePath}/.env', env_file_encoding='utf-8', extra='ignore')
1414

1515
# Env Config
1616
ENVIRONMENT: Literal['dev', 'pro']

deploy/docker-compose/.env.server

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Env: dev、pro
22
ENVIRONMENT='dev'
33
# MySQL
4-
MYSQL_HOST='fsm_mysql'
5-
MYSQL_PORT=3306
6-
MYSQL_USER='root'
7-
MYSQL_PASSWORD='123456'
4+
DATABASE_HOST='fsm_mysql'
5+
DATABASE_PORT=3306
6+
DATABASE_USER='root'
7+
DATABASE_PASSWORD='123456'
88
# Redis
99
REDIS_HOST='fsm_redis'
1010
REDIS_PORT=6379

deploy/docker-compose/docker-compose.yml

+27-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
version: "3.10"
2-
3-
networks:
4-
fsm_network:
5-
driver: bridge
6-
7-
volumes:
8-
fsm_mysql:
9-
fsm_redis:
10-
fsm_static:
11-
121
services:
132
fsm_server:
143
build:
154
context: ../../
165
dockerfile: Dockerfile
6+
image: fsm_server:latest
177
container_name: fsm_server
188
restart: always
199
depends_on:
@@ -27,8 +17,9 @@ services:
2717
- bash
2818
- -c
2919
- |
30-
wait-for-it -s mysql:3306 -s redis:6379 -t 300
31-
supervisord -c /fsm/deploy/supervisor.conf
20+
wait-for-it -s fsm_mysql:3306 -s fsm_redis:6379 -t 300
21+
supervisord -c /etc/supervisor/supervisord.conf
22+
supervisorctl restart
3223
3324
fsm_mysql:
3425
image: mysql:8.0.29
@@ -41,7 +32,7 @@ services:
4132
MYSQL_ROOT_PASSWORD: 123456
4233
TZ: Asia/Shanghai
4334
volumes:
44-
- fba_mysql:/var/lib/mysql
35+
- fsm_mysql:/var/lib/mysql
4536
networks:
4637
- fsm_network
4738
command:
@@ -64,13 +55,32 @@ services:
6455
- fsm_network
6556

6657
fsm_nginx:
67-
image: nginx
58+
image: nginx:stable
6859
ports:
6960
- "8000:80"
7061
container_name: fsm_nginx
7162
restart: always
63+
depends_on:
64+
- fsm_server
7265
volumes:
73-
- ./nginx.conf:/etc/nginx/nginx.conf:ro
74-
- fsm_static:/www/fsm/backend/static
66+
- ../nginx.conf:/etc/nginx/conf.d/default.conf:ro
67+
- fsm_static:/www/fsm_server/backend/static
7568
networks:
7669
- fsm_network
70+
71+
networks:
72+
fsm_network:
73+
name: fsm_network
74+
driver: bridge
75+
ipam:
76+
driver: default
77+
config:
78+
- subnet: 172.10.10.0/24
79+
80+
volumes:
81+
fsm_mysql:
82+
name: fsm_mysql
83+
fsm_redis:
84+
name: fsm_redis
85+
fsm_static:
86+
name: fsm_static

deploy/fastapi_server.conf

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[program:fastapi_server]
2+
directory=/fsm
3+
command=/usr/local/bin/gunicorn -c /fsm/deploy/gunicorn.conf.py main:app
4+
user=root
5+
autostart=true
6+
autorestart=true
7+
startretries=5
8+
redirect_stderr=true
9+
stdout_logfile=/var/log/fastapi_server/fsm_server.log

deploy/gunicorn.conf.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
# fmt: off
12
# 监听内网端口
23
bind = '0.0.0.0:8001'
34

45
# 工作目录
56
chdir = '/fsm/backend/'
67

78
# 并行工作进程数
8-
workers = 4
9+
workers = 1
910

1011
# 指定每个工作者的线程数
1112
threads = 4
@@ -41,6 +42,6 @@
4142
loglevel = 'debug'
4243

4344
# python程序
44-
pythonpath = '/usr/local/lib/python3.8/site-packages'
45+
pythonpath = '/usr/local/lib/python3.10/site-packages'
4546

4647
# 启动 gunicorn -c gunicorn.conf.py main:app

deploy/nginx.conf

+17-40
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
1-
# For more information on configuration, see:
2-
# * Official English Documentation: http://nginx.org/en/docs/
3-
# * Official Russian Documentation: http://nginx.org/ru/docs/
1+
server {
2+
listen 80 default_server;
3+
listen [::]:80 default_server;
4+
server_name 127.0.0.1;
45

5-
user nginx;
6-
worker_processes auto;
7-
error_log /var/log/nginx/error.log;
8-
pid /run/nginx.pid;
6+
root /fsm;
97

10-
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
11-
include /usr/share/nginx/modules/*.conf;
12-
13-
events {
14-
worker_connections 1024;
15-
}
16-
17-
http {
18-
19-
include /etc/nginx/mime.types;
20-
default_type application/octet-stream;
21-
22-
sendfile on;
238
client_max_body_size 5M;
249
client_body_buffer_size 5M;
2510

@@ -30,27 +15,19 @@ http {
3015

3116
keepalive_timeout 300;
3217

33-
server {
34-
listen 80 default_server;
35-
listen [::]:80 default_server;
36-
server_name 127.0.0.1;
37-
38-
root /fsm;
18+
location / {
19+
proxy_pass http://fsm_server:8001;
3920

40-
location / {
41-
proxy_pass http://fsm_server:8001;
42-
43-
proxy_set_header Host $http_host;
44-
proxy_set_header X-Real-IP $remote_addr;
45-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
46-
proxy_set_header X-Forwarded-Proto $scheme;
47-
proxy_connect_timeout 300s;
48-
proxy_send_timeout 300s;
49-
proxy_read_timeout 300s;
50-
}
21+
proxy_set_header Host $http_host;
22+
proxy_set_header X-Real-IP $remote_addr;
23+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
24+
proxy_set_header X-Forwarded-Proto $scheme;
25+
proxy_connect_timeout 300s;
26+
proxy_send_timeout 300s;
27+
proxy_read_timeout 300s;
28+
}
5129

52-
location /static {
53-
alias /www/fsm/backend/static;
54-
}
30+
location /static {
31+
alias /www/fsm_server/backend/static;
5532
}
5633
}

deploy/supervisor.conf

+3-13
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ file=/tmp/supervisor.sock ; the path to the socket file
4242
;password=123 ; default is no password (open server)
4343

4444
[supervisord]
45-
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
45+
logfile=/var/log/supervisor/supervisord.log ; main log file; default $CWD/supervisord.log
4646
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
4747
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
4848
loglevel=info ; log level; default info; others: debug,warn,trace
@@ -151,15 +151,5 @@ serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
151151
; interpreted as relative to this file. Included files *cannot*
152152
; include files themselves.
153153

154-
;[include]
155-
;files = relative/directory/*.ini
156-
157-
[program:fastapi_server]
158-
directory=/fsm/deploy
159-
command=/usr/local/bin/gunicorn -c gunicorn.conf.py main:app
160-
user=root
161-
autostart=true
162-
autorestart=true
163-
startretries=5
164-
redirect_stderr=true
165-
stdout_logfile=/var/log/fastapi_server/fastapi_server.log
154+
[include]
155+
files = /etc/supervisor/conf.d/*.conf

pdm.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ authors = [
66
{name = "Wu Clan", email = "jianhengwu0407@gmail.com"},
77
]
88
dependencies = [
9-
"aiofiles==23.2.1",
10-
"aiosmtplib==3.0.1",
119
"alembic==1.13.1",
12-
"asgiref==3.8.1",
1310
"asyncmy==0.2.9",
1411
"bcrypt==4.1.3",
1512
"cryptography==42.0.7",
@@ -36,12 +33,12 @@ requires-python = ">=3.10"
3633
readme = "README.md"
3734
license = {text = "MIT"}
3835

39-
[tool.pdm.dev-dependencies]
36+
[dependency-groups]
4037
lint = [
4138
"ruff>=0.8.2",
4239
]
43-
deploy = [
44-
"supervisor>=4.2.5",
40+
server = [
41+
"gunicorn==21.2.0",
4542
"wait-for-it>=2.2.2",
4643
]
4744

requirements.txt

-3
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,19 @@ rich==13.9.4
6767
rich-toolkit==0.12.0
6868
rsa==4.9
6969
ruff==0.8.2
70-
setuptools==75.6.0
7170
shellingham==1.5.4
7271
six==1.17.0
7372
sniffio==1.3.1
7473
sqlalchemy==2.0.30
7574
sqlalchemy-crud-plus==1.6.0
7675
starlette==0.37.2
77-
supervisor==4.2.5
7876
typer==0.15.1
7977
typing-extensions==4.12.2
8078
tzdata==2024.1
8179
ujson==5.10.0
8280
uvicorn[standard]==0.29.0
8381
uvloop==0.21.0; (sys_platform != "cygwin" and sys_platform != "win32") and platform_python_implementation != "PyPy"
8482
virtualenv==20.28.0
85-
wait-for-it==2.3.0
8683
watchfiles==1.0.0
8784
websockets==14.1
8885
win32-setctime==1.1.0; sys_platform == "win32"

0 commit comments

Comments
 (0)