Dockerfile 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # 使用官方 Python 3.11 作为基础镜像
  2. FROM python:3.11
  3. # 定义构建参数,用户名称为 app
  4. ARG USER=app
  5. # 定义构建参数,应用目录为 /app
  6. ARG APP_DIR=/app
  7. # 设置环境变量 APP_DIR
  8. ENV APP_DIR=${APP_DIR}
  9. # 创建用户组和用户,并设置家目录
  10. RUN groupadd -g 61000 ${USER} \
  11. && useradd -g 61000 -u 61000 -ms /bin/bash -d ${APP_DIR} ${USER}
  12. # 设置工作目录为 /app
  13. WORKDIR ${APP_DIR}
  14. # 复制 fastapi 目录下的所有代码到镜像中
  15. COPY ./fastapi_demo ./fastapi_demo
  16. # 安装 fastapi 目录下的依赖 使用国内源更快拉依赖
  17. RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r ./fastapi_demo/requirements.txt
  18. # 复制启动脚本到镜像中
  19. COPY ./entrypoint.sh ./entrypoint.sh
  20. # 修改 /app 目录及其内容的所有者为 app 用户
  21. RUN chown -R ${USER}:${USER} ${APP_DIR}
  22. # 切换到非 root 用户,提升安全性
  23. USER ${USER}
  24. # 设置容器启动时执行的脚本
  25. ENTRYPOINT ["/app/entrypoint.sh"]
  26. # 默认传递给 entrypoint.sh 的参数(可选)
  27. # 在entrypoint脚本中如果传入参数为chill会进入空转状态,如果有写别的命令则会覆盖这条
  28. CMD ["chill"]