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