docker+gunicorn+fastapi部署
一、准备工作
1、先确保项目可以正常运行
2、使用pip freeze导出第三方库
3、在项目根目录新建pip.conf文件,写入一下内容
[global] index-url=http://pypi.douban.com/simple # 我之前用的阿里云的,但是镜像打包的时候提示代理问题,建议使用豆瓣的 [install] trusted-host=pypi.douban.com
二、编写Dockerfile文件,生成镜像
FROM python MAINTAINER tester ADD ./ /opt/rms-rcs WORKDIR /opt/rms-rcs COPY pip.conf /root/.pip/pip.conf #复制到目标服务器,这样pip安装的速度会比较快 RUN /usr/local/bin/python -m pip install --upgrade pip && \ pip3 install -r requirements.txt CMD ["gunicorn", "-c", "gunicorn.py", "main:app"] EXPOSE 8000
docker build -t test:v1.0.0 . # 注意最后的那个“.”,说明是当前目录下
三、运行镜像及调试
docker run -it --name test -p 8080:8080 test:v1.0.0
一开始不建议-d直接后台运行,这样报错的话不利于调试,确保没有问题后再进行后台