Skip to content

Commit

Permalink
重写Docker构建流程
Browse files Browse the repository at this point in the history
  • Loading branch information
HisAtri committed Jan 21, 2024
1 parent 451927d commit dfea294
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 2 additions & 0 deletions dockerignore → .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.git
.github
tests/
.deploy_tmp_path
32 changes: 25 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
# 基于Python3.9 alpine镜像
FROM python:3.9.17-alpine3.18
# 第一阶段:安装GCC
FROM python:3.12.1-alpine as gcc_installer

# 安装GCC
RUN apk add --no-cache gcc musl-dev

# 第二阶段:安装Python依赖
FROM gcc_installer as requirements_installer

# 设置工作目录
WORKDIR /app

# 将源代码复制到Docker镜像中
COPY ./LrcApi /app
# 只复制 requirements.txt,充分利用 Docker 缓存层
COPY ./LrcApi/requirements.txt /app/

# 安装Python依赖
RUN pip install --no-user --prefix=/install -r requirements.txt

# 第三阶段:运行环境
FROM python:3.12.1-alpine

# 安装Python项目依赖
RUN pip install -r /app/requirements.txt
# 设置工作目录
WORKDIR /app

# 复制Python依赖
COPY --from=requirements_installer /install /usr/local

# 复制项目代码
COPY ./LrcApi /app

# 设置启动命令
CMD ["python", "/app/app.py"]
CMD ["python", "/app/app.py"]
5 changes: 5 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
shutil.rmtree(cache_dir)
except FileNotFoundError:
pass
# 定义缓存逻辑为本地文件缓存,目录为cache_dir = './flask_cache'
cache = Cache(app, config={
'CACHE_TYPE': 'filesystem',
'CACHE_DIR': cache_dir
Expand Down Expand Up @@ -319,14 +320,18 @@ def login_api():


def main():
# Waitress WSGI 服务器
serve(app, host=args.ip, port=args.port, threads=32, channel_timeout=30)
# Debug服务器
# app.run(host='0.0.0.0', port=args.port)


if __name__ == '__main__':
# 日志配置
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger('')
logger.info("正在启动服务器")
# 注册 Blueprint 到 Flask 应用
app.register_blueprint(v1_bp)
# 启动
main()

0 comments on commit dfea294

Please sign in to comment.