Skip to content

Commit

Permalink
Optimize docker deploy settings and scripts (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan authored Jan 13, 2025
1 parent e0606d1 commit 1e855db
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 133 deletions.
12 changes: 8 additions & 4 deletions backend/backend.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debi
&& sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list.d/debian.sources

RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc python3-dev \
&& apt-get install -y --no-install-recommends gcc python3-dev supervisor \
&& rm -rf /var/lib/apt/lists/* \
# 某些包可能存在同步不及时导致安装失败的情况,可更改为官方源:https://pypi.org/simple
&& pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple \
&& pip install -r backend/requirements.txt -i https://mirrors.aliyun.com/pypi/simple \
&& pip install gunicorn aio_pika supervisor wait-for-it -i https://mirrors.aliyun.com/pypi/simple
&& pip install gunicorn aio_pika wait-for-it -i https://mirrors.aliyun.com/pypi/simple

ENV TZ="Asia/Shanghai"

RUN mkdir -p /var/log/fastapi_server
RUN mkdir -p /var/log/fastapi_server \
&& mkdir -p /var/log/supervisor \
&& mkdir -p /etc/supervisor/conf.d

COPY deploy/backend/supervisor.conf /etc/supervisor/supervisord.conf

COPY deploy/backend/fastapi_server.conf /etc/supervisor/conf.d/

EXPOSE 8001

CMD ["uvicorn", "backend.main:app", "--host", "127.0.0.1", "--port", "8000"]
CMD ["uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8000"]
10 changes: 7 additions & 3 deletions backend/celery.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/debi
&& sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list.d/debian.sources

RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc python3-dev \
&& apt-get install -y --no-install-recommends gcc python3-dev supervisor \
&& rm -rf /var/lib/apt/lists/* \
# 某些包可能存在同步不及时导致安装失败的情况,可更改为官方源:https://pypi.org/simple
&& pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple \
&& pip install -r backend/requirements.txt -i https://mirrors.aliyun.com/pypi/simple \
&& pip install gunicorn aio_pika supervisor wait-for-it -i https://mirrors.aliyun.com/pypi/simple
&& pip install aio_pika wait-for-it -i https://mirrors.aliyun.com/pypi/simple

ENV TZ="Asia/Shanghai"

RUN mkdir -p /var/log/celery
RUN mkdir -p /var/log/celery \
&& mkdir -p /var/log/supervisor \
&& mkdir -p /etc/supervisor/conf.d

COPY deploy/backend/supervisor.conf /etc/supervisor/supervisord.conf

COPY deploy/backend/celery.conf /etc/supervisor/conf.d/

Expand Down
1 change: 0 additions & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ lint = [
server = [
"gunicorn==21.2.0",
"aio_pika==9.4.3",
"supervisor>=4.2.5",
"wait-for-it>=2.2.2",
]

Expand Down
23 changes: 0 additions & 23 deletions backend/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deploy/backend/celery.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ stdout_logfile=/var/log/celery/fba_celery_beat.log

[program:celery_flower]
directory=/fba/backend
command=/usr/local/bin/celery -A app.task.celery flower --port=8555 --basic-auth=admin:123456
command=/usr/local/bin/celery -A app.task.celery flower --port=8555 --url-prefix=flower --basic-auth=admin:123456
user=root
autostart=true
autorestart=true
Expand Down
118 changes: 58 additions & 60 deletions deploy/backend/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
networks:
fba_network:
name: fba_network
driver: bridge
ipam:
driver: default
config:
- subnet: 172.10.10.0/24

volumes:
fba_mysql:
name: fba_mysql
fba_redis:
name: fba_redis
fba_static:
name: fba_static
fba_rabbitmq:
name: fba_rabbitmq

services:
fba_server:
build:
Expand All @@ -38,8 +19,7 @@ services:
- -c
- |
wait-for-it -s fba_mysql:3306 -s fba_redis:6379 -t 300
mkdir -p /var/log/supervisor/
supervisord -c /fba/deploy/backend/supervisor.conf
supervisord -c /etc/supervisor/supervisord.conf
supervisorctl restart
fba_mysql:
Expand All @@ -62,20 +42,20 @@ services:
--collation-server=utf8mb4_general_ci
--lower_case_table_names=1

# fba_postgres:
# image: postgres:16
# ports:
# - "${DOCKER_POSTGRES_MAP_PORT:-5432}:5432"
# container_name: fba_postgres
# restart: always
# environment:
# POSTGRES_DB: fba
# POSTGRES_PASSWORD: 123456
# TZ: Asia/Shanghai
# volumes:
# - fba_postgres:/var/lib/postgresql/data
# networks:
# - fba_network
# fba_postgres:
# image: postgres:16
# ports:
# - "${DOCKER_POSTGRES_MAP_PORT:-5432}:5432"
# container_name: fba_postgres
# restart: always
# environment:
# POSTGRES_DB: fba
# POSTGRES_PASSWORD: 123456
# TZ: Asia/Shanghai
# volumes:
# - fba_postgres:/var/lib/postgresql/data
# networks:
# - fba_network

fba_redis:
image: redis:6.2.7
Expand All @@ -99,33 +79,33 @@ services:
depends_on:
- fba_server
volumes:
- ../nginx.conf:/etc/nginx/nginx.conf:ro
- ../nginx.conf:/etc/nginx/conf.d/default.conf:ro
- fba_static:/www/fba_server/backend/static
networks:
- fba_network

# fba_ui:
# build:
# context: /root/fastapi_best_architecture_ui
# dockerfile: Dockerfile
# image: fba_ui:latest
# ports:
# - "80:80"
# - "443:443"
# container_name: fba_ui
# restart: always
# depends_on:
# - fba_server
# command:
# - nginx
# - -g
# - daemon off;
# volumes:
# # - local_ssl_pem_path:/etc/ssl/xxx.pem
# # - local_ssl_key_path:/etc/ssl/xxx.key
# - fba_static:/www/fba_server/backend/static
# networks:
# - fba_network
# fba_ui:
# build:
# context: /root/fastapi_best_architecture_ui
# dockerfile: Dockerfile
# image: fba_ui:latest
# ports:
# - "80:80"
# - "443:443"
# container_name: fba_ui
# restart: always
# depends_on:
# - fba_server
# command:
# - nginx
# - -g
# - daemon off;
# volumes:
# # - local_ssl_pem_path:/etc/ssl/xxx.pem
# # - local_ssl_key_path:/etc/ssl/xxx.key
# - fba_static:/www/fba_server/backend/static
# networks:
# - fba_network

fba_rabbitmq:
hostname: fba_rabbitmq
Expand Down Expand Up @@ -161,6 +141,24 @@ services:
- -c
- |
wait-for-it -s fba_rabbitmq:5672 -t 300
mkdir -p /var/log/supervisor/
supervisord -c /fba/deploy/backend/supervisor.conf
supervisord -c /etc/supervisor/supervisord.conf
supervisorctl restart
networks:
fba_network:
name: fba_network
driver: bridge
ipam:
driver: default
config:
- subnet: 172.10.10.0/24

volumes:
fba_mysql:
name: fba_mysql
fba_redis:
name: fba_redis
fba_static:
name: fba_static
fba_rabbitmq:
name: fba_rabbitmq
75 changes: 34 additions & 41 deletions deploy/backend/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 127.0.0.1;

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
root /fba;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {

include /etc/nginx/mime.types;
default_type application/octet-stream;

sendfile on;
client_max_body_size 5M;
client_body_buffer_size 5M;

Expand All @@ -30,29 +15,37 @@ http {

keepalive_timeout 300;

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 127.0.0.1;
location / {
proxy_pass http://fba_server:8001;

root /fba;

client_max_body_size 10m; # 最大上传文件

location / {
proxy_pass http://fba_server:8001;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
}
location /flower/ {
proxy_pass http://fba_server:8555;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
proxy_redirect off;

# WebSocket 支持
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

location /static/ {
alias /www/fba_server/backend/static;
}
location /static {
alias /www/fba_server/backend/static;
}
}

0 comments on commit 1e855db

Please sign in to comment.