FastApi dockers部署

99 阅读1分钟

Dockerfile

FROM python:3.9  
LABEL authors="liuhuo"  
  
# 2 将当前工作目录设置为 /code  
# 这是防止 requirements.txt 文件和应用程序目录的地方  
WORKDIR /code  
  
# 3. 先复制requirements.txt  
# 由于就这个文件不经常更改, DOcker 会检测它并在这一步使用缓冲, 也为下一步启动缓冲  
COPY ./requirements.txt /code/requirements.txt  
  
# 4. 运行 pip 命令安装依赖项  
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com  
# 5. 复制 FastAPI 项目代码  
COPY ./ /code/fastpro  
WORKDIR /code/fastpro  
  
RUN PYTHONPATH=/code/fastpro  
# 6。 运行服务  
CMD ["python", "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0","--port", "8080"]

编译

docker build -t myimage .

运行

docker run -d --name mycontainer -p 8090:8080 myimage