Skip to content

Commit 0bd01f4

Browse files
authored
update Dockerfile and docker-compose.yml (#31)
* update Dockerfile and docker-compose.yml * Fix log file type-checking
1 parent 64f2a13 commit 0bd01f4

File tree

9 files changed

+47
-79
lines changed

9 files changed

+47
-79
lines changed

.env.docker

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Env: dev、pro
2+
ENVIRONMENT='dev'
3+
# MySQL
4+
DB_HOST='mysql'
5+
DB_PORT=3306
6+
DB_USER='root'
7+
DB_PASSWORD='123456'
8+
# Redis
9+
REDIS_HOST='redis'
10+
REDIS_PORT=6379
11+
REDIS_PASSWORD=''
12+
REDIS_DATABASE=0
13+
# APScheduler
14+
APS_REDIS_HOST='redis'
15+
APS_REDIS_PORT=6379
16+
APS_REDIS_PASSWORD=''
17+
APS_REDIS_DATABASE=1
18+
# Token
19+
TOKEN_SECRET_KEY='1VkVF75nsNABBjK_7-qz7GtzNy3AMvktc9TCPwKczCk'

Dockerfile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
FROM python:3.8-slim
1+
FROM python:3.10-slim
22

33
WORKDIR /fba
44

55
COPY . /fba
66

7+
RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list \
8+
&& sed -i s@/security.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list
9+
10+
RUN apt-get update \
11+
&& apt-get install -y --no-install-recommends gcc python3-dev \
12+
&& rm -rf /var/lib/apt/lists/*
13+
714
RUN pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple \
815
&& pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
916

1017
ENV TZ = Asia/Shanghai
1118

1219
RUN mkdir -p /var/log/fastapi_server
1320

21+
RUN cd /fba && touch .env
22+
1423
EXPOSE 8001
1524

1625
CMD ["uvicorn", "backend.app.main:app", "--host", "127.0.0.1", "--port", "8000"]

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ git clone https://github.com/wu-clan/fastapi_best_architecture.git
3535

3636
2. Create a database `fba`, choose utf8mb4 encode
3737
3. Install and start Redis
38-
4. Copy .env.example to .env and view `backend/app/core/conf.py`, update database configuration information
39-
5. Perform a database migration [alembic](https://alembic.sqlalchemy.org/en/latest/tutorial.html)
38+
4. create a `.env` file in the `backend/app/` directory
39+
```shell
40+
cd backend/app/
41+
touch .env
42+
```
43+
5. Copy .env.example to .env and view `backend/app/core/conf.py`, update database configuration information
44+
6. Perform a database migration [alembic](https://alembic.sqlalchemy.org/en/latest/tutorial.html)
4045
```shell
4146
cd backend/app/
4247
@@ -46,8 +51,8 @@ git clone https://github.com/wu-clan/fastapi_best_architecture.git
4651
# Perform the migration
4752
alembic upgrade head
4853
```
49-
6. Execute the backend/app/main.py file startup service
50-
7. Browser access: http://127.0.0.1:8000/v1/docs
54+
7. Execute the backend/app/main.py file startup service
55+
8. Browser access: http://127.0.0.1:8000/v1/docs
5156

5257
---
5358

backend/app/common/log.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
from __future__ import annotations
44

55
import os
6+
from typing import TYPE_CHECKING
67

7-
import loguru
88
from loguru import logger
99

1010
from backend.app.core import path_conf
1111
from backend.app.core.conf import settings
1212

13+
if TYPE_CHECKING:
14+
import loguru
15+
1316

1417
class Logger:
1518
@staticmethod

backend/app/database/db_mysql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
try:
2323
# 数据库引擎
24-
async_engine = create_async_engine(SQLALCHEMY_DATABASE_URL, echo=settings.DB_ECHO, future=True)
24+
async_engine = create_async_engine(SQLALCHEMY_DATABASE_URL, echo=settings.DB_ECHO, future=True, pool_pre_ping=True)
2525
# log.success('数据库连接成功')
2626
except Exception as e:
2727
log.error('❌ 数据库链接失败 {}', e)

docker-compose.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "3.8"
1+
version: "3.10"
22

33
services:
44
app:
@@ -12,7 +12,7 @@ services:
1212
- redis
1313
volumes:
1414
- ./backend:/fba/backend
15-
- ./docker_conf.py:/fba/backend/app/core/conf.py
15+
- .env.docker:/fba/backend/app/.env
1616
- fba_static:/fba/backend/app/static
1717
networks:
1818
- fba_network

docker_conf.py

Lines changed: 0 additions & 68 deletions
This file was deleted.

gunicorn.conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@
4141
loglevel = 'debug'
4242

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

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

nginx.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ http {
3737

3838
root /fba;
3939

40-
client_max_body_size 10m; #最大上传文件
40+
client_max_body_size 10m; # 最大上传文件
4141

4242
location / {
4343
proxy_pass http://app:8001;

0 commit comments

Comments
 (0)