Skip to content

Commit d4f4f3f

Browse files
authored
Update docker one-click deployment (#88)
* Update the docker-compose deployment directory and internals * missed updates * update readme style
1 parent e6640e7 commit d4f4f3f

File tree

9 files changed

+112
-75
lines changed

9 files changed

+112
-75
lines changed

Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM python:3.10-slim
22

33
WORKDIR /fba
44

5-
COPY . /fba
5+
COPY . .
66

77
RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list \
88
&& sed -i s@/security.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list
@@ -11,15 +11,16 @@ RUN apt-get update \
1111
&& apt-get install -y --no-install-recommends gcc python3-dev \
1212
&& rm -rf /var/lib/apt/lists/*
1313

14-
RUN pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple \
15-
&& pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
14+
# 某些包可能存在同步不及时导致安装失败的情况,可选择备用源
15+
# 清华源(国内快,也可能同步不及时):https://pypi.tuna.tsinghua.edu.cn/simple
16+
# 官方源(国外慢,但永远都是最新的):https://pypi.org/simple
17+
RUN pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple \
18+
&& pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
1619

1720
ENV TZ = Asia/Shanghai
1821

1922
RUN mkdir -p /var/log/fastapi_server
2023

21-
RUN cd /fba && touch .env
22-
2324
EXPOSE 8001
2425

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

README.md

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
English | [简体中文](./README.zh-CN.md)
44

5-
This is a base project of the FastAPI framework, in production
5+
This is a basic project of the FastAPI framework, using a pseudo three-tier architecture, **still in production**
66

7-
It‘s purpose is to allow you to develop your project directly with it
8-
as your base project
7+
It‘s purpose is to allow you to develop your project directly with it as your base project
98

109
Support python3.10 and above
1110

@@ -15,6 +14,7 @@ Support python3.10 and above
1514
- [x] Pydantic
1615
- [x] SQLAlchemy
1716
- [x] Alembic
17+
- [x] Casbin
1818
- [x] MySQL
1919
- [x] Redis
2020
- [x] APScheduler
@@ -32,49 +32,62 @@ git clone https://github.com/wu-clan/fastapi_best_architecture.git
3232

3333
1. Install dependencies
3434

35-
```shell
36-
pip install -r requirements.txt
37-
```
35+
```shell
36+
pip install -r requirements.txt
37+
```
3838

3939
2. Create a database `fba`, choose utf8mb4 encode
4040
3. Install and start Redis
4141
4. create a `.env` file in the `backend/app/` directory
4242

43-
```shell
44-
cd backend/app/
45-
46-
touch .env
47-
```
43+
```shell
44+
cd backend/app/
4845

49-
5. Copy `.env.example` to `.env` and view `backend/app/core/conf.py`, update database configuration information
46+
touch .env
47+
```
48+
49+
5. Copy `.env.example` to `.env`
50+
51+
```shell
52+
cp .env.example .env
53+
```
54+
5055
6. Perform a database migration [alembic](https://alembic.sqlalchemy.org/en/latest/tutorial.html)
5156

52-
```shell
53-
cd backend/app/
54-
55-
# Generate the migration file
56-
alembic revision --autogenerate
57-
58-
# Perform the migration
59-
alembic upgrade head
60-
```
57+
```shell
58+
cd backend/app/
6159

60+
# Generate the migration file
61+
alembic revision --autogenerate
62+
63+
# Perform the migration
64+
alembic upgrade head
65+
```
66+
6267
7. Execute the `backend/app/main.py` file startup service
6368
8. Browser access: http://127.0.0.1:8000/v1/docs
6469

6570
---
6671

6772
### 2:Docker
6873

69-
1. Run the one-click start command in the directory where the `docker-compose.yml` file is located
74+
1. Go to the directory where the `docker-compose.yml` file is located and create the environment variable file `.env`
7075

71-
```shell
72-
docker-compose up -d --build
73-
```
76+
```shell
77+
cp .env.server ../../backend/app/.env
7478

75-
2. Wait for the command to finish automatically
79+
# This command is optional
80+
cp .env.docker .env
81+
```
82+
83+
2. Execute the one-click start command
7684

77-
3. Browser access: http://127.0.0.1:8000/v1/docs
85+
```shell
86+
docker-compose up -d -build
87+
```
88+
89+
3. Wait for the command to finish automatically
90+
4. Browser access: http://127.0.0.1:8000/v1/docs
7891

7992
## Init the test data
8093

@@ -85,13 +98,12 @@ Execute the `backend/app/init_test_data.py` file
8598
Perform tests via pytest
8699

87100
1. Create a database `fba_test`, choose utf8mb4 encode
88-
89101
2. First, go to the app directory
90102

91103
```shell
92104
cd backend/app/
93105
```
94-
106+
95107
3. Init the test data
96108

97109
```shell

README.zh-CN.md

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
简体中文 | [English](./README.md)
44

5-
这是 FastAPI 框架的一个基础项目,在制作中
5+
这是 FastAPI 框架的一个基础项目,使用伪三层架构,**目前还在制作中**
66

77
它的目的是让你直接用它作为你的基础项目来开发你的项目
88

@@ -14,6 +14,7 @@
1414
- [x] Pydantic
1515
- [x] SQLAlchemy
1616
- [x] Alembic
17+
- [x] Casbin
1718
- [x] MySQL
1819
- [x] Redis
1920
- [x] APScheduler
@@ -37,12 +38,20 @@ git clone https://github.com/wu-clan/fastapi_best_architecture.git
3738
2. 创建一个数据库`fba`,选择 utf8mb4 编码
3839
3. 安装并启动 Redis
3940
4. 在`backend/app/`目录下创建一个`.env`文件
41+
4042
```shell
4143
cd backend/app/
4244
touch .env
4345
```
44-
5. 复制 `.env.example``.env` 并查看`backend/app/core/conf.py`,更新数据库配置信息
46+
47+
5. 复制 `.env.example``.env`
48+
49+
```shell
50+
cp .env.example .env
51+
```
52+
4553
6. 进行数据库迁移[alembic](https://alembic.sqlalchemy.org/en/latest/tutorial.html)
54+
4655
```shell
4756
cd backend/app/
4857
@@ -52,22 +61,31 @@ git clone https://github.com/wu-clan/fastapi_best_architecture.git
5261
# 执行迁移
5362
alembic upgrade head
5463
```
64+
5565
7. 执行 `backend/app/main.py` 文件启动服务
5666
8. 浏览器访问:http://127.0.0.1:8000/v1/docs
5767

5868
---
5969

6070
### 2:Docker
6171

62-
1. 在 `docker-compose.yml` 文件所在的目录中运行一键启动命令
72+
1. 进入 `docker-compose.yml` 文件所在目录,创建环境变量文件`.env`
73+
74+
```shell
75+
cp .env.server ../../backend/app/.env
76+
77+
# 此命令为可选
78+
cp .env.docker .env
79+
```
80+
81+
2. 执行一键启动命令
6382

6483
```shell
6584
docker-compose up -d -build
6685
```
67-
68-
2. 等待命令自动完成
6986

70-
3. 浏览器访问:http://127.0.0.1:8000/v1/docs
87+
3. 等待命令自动完成
88+
4. 浏览器访问:http://127.0.0.1:8000/v1/docs
7189

7290
## 初始化测试数据
7391

@@ -78,13 +96,12 @@ git clone https://github.com/wu-clan/fastapi_best_architecture.git
7896
通过 pytest 进行测试
7997

8098
1. 创建一个数据库`fba_test`,选择 utf8mb4 编码
81-
8299
2. 首先,进入app目录
83100

84101
```shell
85102
cd backend/app/
86103
```
87-
104+
88105
3. 初始化测试数据
89106

90107
```shell

deploy/docker-compose/.env.docker

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Docker
2+
DOCKER_DB_MAP_PORT=13306
3+
DOCKER_REDIS_MAP_PORT=16379

.env.docker renamed to deploy/docker-compose/.env.server

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Env: dev、pro
22
ENVIRONMENT='dev'
33
# MySQL
4-
DB_HOST='mysql'
4+
DB_HOST='fba_mysql'
55
DB_PORT=3306
66
DB_USER='root'
77
DB_PASSWORD='123456'
88
# Redis
9-
REDIS_HOST='redis'
9+
REDIS_HOST='fba_redis'
1010
REDIS_PORT=6379
1111
REDIS_PASSWORD=''
1212
REDIS_DATABASE=0
1313
# APScheduler
14-
APS_REDIS_HOST='redis'
14+
APS_REDIS_HOST='fba_redis'
1515
APS_REDIS_PORT=6379
1616
APS_REDIS_PASSWORD=''
1717
APS_REDIS_DATABASE=1

docker-compose.yml renamed to deploy/docker-compose/docker-compose.yml

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,51 @@
11
version: "3.10"
22

3+
networks:
4+
fba_network:
5+
driver: bridge
6+
ipam:
7+
driver: default
8+
config:
9+
- subnet: 172.10.10.0/24
10+
11+
volumes:
12+
fba_mysql:
13+
fba_redis:
14+
fba_static:
15+
316
services:
4-
app:
17+
fba_server:
518
build:
6-
context: .
19+
context: ../../
720
dockerfile: Dockerfile
821
container_name: "fba_server"
922
restart: always
1023
depends_on:
11-
- mysql
12-
- redis
24+
- fba_mysql
25+
- fba_redis
1326
volumes:
14-
- ./backend:/fba/backend
15-
- .env.docker:/fba/backend/app/.env
1627
- fba_static:/fba/backend/app/static
1728
networks:
1829
- fba_network
1930
command:
2031
- bash
2132
- -c
2233
- |
23-
wait-for-it -s mysql:3306 -s redis:6379 -t 300
24-
supervisord -c supervisor.conf
34+
wait-for-it -s fba_mysql:3306 -s fba_redis:6379 -t 300
35+
supervisord -c /fba/deploy/supervisor.conf
2536
26-
mysql:
37+
fba_mysql:
2738
image: mysql:8.0.29
2839
ports:
29-
- "3306:3306"
30-
container_name: "mysql"
40+
- "${DOCKER_DB_MAP_PORT:-3306}:3306"
41+
container_name: "fba_mysql"
3142
restart: always
3243
environment:
3344
MYSQL_DATABASE: fba
3445
MYSQL_ROOT_PASSWORD: 123456
3546
TZ: Asia/Shanghai
3647
volumes:
37-
- /var/lib/mysql
48+
- fba_mysql:/var/lib/mysql
3849
networks:
3950
- fba_network
4051
command:
@@ -43,36 +54,29 @@ services:
4354
--collation-server=utf8mb4_general_ci
4455
--lower_case_table_names=1
4556

46-
redis:
57+
fba_redis:
4758
image: redis:6.2.7
4859
ports:
49-
- "6379:6379"
50-
container_name: "redis"
60+
- "${DOCKER_REDIS_MAP_PORT:-6379}:6379"
61+
container_name: "fba_redis"
5162
restart: always
5263
environment:
5364
- TZ=Asia/Shanghai
5465
volumes:
55-
- /var/lib/redis
66+
- fba_redis:/var/lib/redis
5667
networks:
5768
- fba_network
5869

59-
nginx:
70+
fba_nginx:
6071
image: nginx
6172
ports:
6273
- "8000:80"
63-
container_name: "nginx"
74+
container_name: "fba_nginx"
6475
restart: always
6576
depends_on:
66-
- app
77+
- fba_server
6778
volumes:
68-
- ./nginx.conf:/etc/nginx/nginx.conf:ro
79+
- ../nginx.conf:/etc/nginx/nginx.conf:ro
6980
- fba_static:/www/fba/backend/app/static
7081
networks:
7182
- fba_network
72-
73-
74-
networks:
75-
fba_network:
76-
77-
volumes:
78-
fba_static:
File renamed without changes.

nginx.conf renamed to deploy/nginx.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ http {
4040
client_max_body_size 10m; # 最大上传文件
4141

4242
location / {
43-
proxy_pass http://app:8001;
43+
proxy_pass http://fba_server:8001;
4444

4545
proxy_set_header Host $http_host;
4646
proxy_set_header X-Real-IP $remote_addr;

supervisor.conf renamed to deploy/supervisor.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
156156

157157
[program:fastapi_server]
158158
directory=/fba
159-
command=/usr/local/bin/gunicorn -c gunicorn.conf.py main:app
159+
command=/usr/local/bin/gunicorn -c /fba/deploy/gunicorn.conf.py main:app
160160
user=root
161161
autostart=true
162162
autorestart=true
163163
startretries=5
164164
redirect_stderr=true
165-
stdout_logfile=/var/log/fastapi_server/fastapi_server.log
165+
stdout_logfile=/var/log/fastapi_server/fba_server.log

0 commit comments

Comments
 (0)