python开发 --- FastAPI服务docker方式部署:Ubuntu18.04+python3.6.11+gunicorn+uvicorn

pull ubuntu18容器镜像:

docker pull ubuntu:18.04

 
服务器端创建代码目录:
mkdir /home/aeotrade/fast_api_map_data/
代码文件:at_xml_exchange/xml_data_exchange/…..
 
启动容器:
docker run -d -p 8000:8000 --name fast_api  -v /home/aeotrade/fast_api_map_data/at_xml_exchange:/project -t ubuntu:latest
安装常用包:
root@74c2e787de43:/# apt-get update
apt-get install zip
apt-get install vim
 
ubuntu18更换之前要先备份之前的源:
cp /etc/apt/sources.list /etc/apt/sources.list.bak
 
 
apt-get update
 
下载安装python3.6
 
 
先安装python依赖环境:
apt-get install zlib1g-dev libbz2-dev libssl-dev libncurses5-dev libsqlite3-dev libreadline-dev tk-dev libgdbm-dev libdb-dev libpcap-dev xz-utils libexpat1-dev liblzma-dev libffi-dev libc6-dev
 
--时间较长,耐心等待
 
将python3.6源码包上传到服务器容器内部:
 
注意是在容器内部操作!!!  
解压:
 tar -zxvf Python-3.6.11rc1.tgz
 
 
建立本地安装目录:
mkdir -p /usr/local/python3
 
进入刚才的python源码解压目录:
cd  /project/Python-3.6.11rc1
 
 
./configure --prefix=/usr/local/python3 --enable-optimizations
 
 
make
 
make install



创建软连接:
ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3

验证版本:
python3 --version
pip3 --version

为保证安装步骤,将上述容器commit

 docker commit 5b5866f7551c ubuntu18_python36:2.0

进入容器接着操作:
安装gunicorn:
pip3 install gunicorn==19.9.0 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

pip3 install fastapi -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

pip3 install uvicorn -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

pip3 install sqlalchemy -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

pip3 install apscheduler -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

pip3 install xmltodict -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

pip3 install lxml==4.5.2 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

pip3 install psycopg2==2.7.4 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com #注意版本,需要安装2.7.4

pip3 install psycopg2-binary -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com 


—当前psycopg2提供的版本:
ERROR: Could not find a version that satisfies the requirement psycopg2==2.2.7 (from versions: 2.0.10, 2.0.11, 2.0.12, 2.0.13, 2.0.14, 2.2.0, 2.2.1, 2.2.2, 2.3.0, 2.3.1, 2.3.2, 2.4, 2.4.1, 2.4.2, 2.4.3, 2.4.4, 2.4.5, 2.4.6, 2.5, 2.5.1, 2.5.2, 2.5.3, 2.5.4, 2.5.5, 2.6, 2.6.1, 2.6.2, 2.7, 2.7.1, 2.7.2, 2.7.3, 2.7.3.1, 2.7.3.2, 2.7.4, 2.7.5, 2.7.6, 2.7.6.1, 2.7.7, 2.8, 2.8.1, 2.8.2, 2.8.3, 2.8.4, 2.8.5, 2.8.6)


***************
异常处理:
root@74c2e787de43:/project/xml_data_exchange/app# gunicorn
bash: gunicorn: command not found

原因:未配置环境变量

解决:这里环境安装gunicorn用的pip3,所以找到python3的bin目录,将入环境变量即可

export PATH=$PATH:/usr/local/python3/bin

每次退出容器需要再次进入容器后,需要再执行环境变量导入。


编码格式-环境变量:
export LC_ALL=C.UTF-8
export LANG=C.UTF-8


启动服务两种方式:

方式一: 
uvicorn main:app --reload --host=0.0.0.0 --port=8000 

方式二:
python3 main.py


apt-get install net-tools
netstat -napt
 
netstat -ap | grep 8080
 
 
进入容器:
docker exec -u root -it fast_api /bin/bash 
 
容器内需要安装的包:
Package     Version
----------- -------
APScheduler 3.6.3
click       7.1.2
fastapi     0.61.1
gunicorn    19.9.0
h11         0.11.0
lxml        4.5.2
pip         20.2.1
psycopg2    2.8
pydantic    1.6.1
pytz        2020.1
setuptools  49.2.1
six         1.15.0
SQLAlchemy  1.3.20
starlette   0.13.6
tzlocal     2.1
uvicorn     0.12.1
wheel       0.35.1
xmltodict   0.12.0
 
 
 
*****************************************
系统环境变量,默认添加:
export PATH=$PATH:/usr/local/python3/bin
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
 
/etc/profile 是所有用户的环境变量
/etc/enviroment是系统的环境变量 
 
在登录Linux时要执行文件的过程如下:
在刚登录Linux时,首先启动/etc/profile 文件,然后再启动用户目录下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile文件中的其中一个,执行的顺序为:~/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile文件存在的话,一般还会执行 ~/.bashrc文件.
 
方式一:
vi /etc/profile 添加上述3个环境变量配置,保存退出
执行:source /etc/profile 让上述配置生效。
 
方式二:
vi .profile  添加上述3个环境变量配置,保存退出
source .profile
 
分别尝试了上述两种.profile和/etc/profile
发现没有效果,退出容器,再进入容器时候,发现环境变量又没有了。
 
原因可能是该配置文件并没有生效,即使重启容器也没有加载。
 
接着尝试:
删掉当前容器,run的时候,指定环境变量,如下: 还是没有效果。
docker run -d -p 8000:8000 --name fast_api  -e "export PATH=$PATH:/usr/local/python3/bin" -e "export LC_ALL=C.UTF-8"  -e "export LANG=C.UTF-8" -v /home/aeotrade/fast_api_map_data/at_xml_exchange:/project -t ubuntu18_python36:3.0
 
 
直到发现这篇文章:
镜像启动时,自动执行的是~/.bashrc文件,所以,环境变量需要配置在该文件内,这样镜像启动时,可自动执行该文件,使环境变量生效。
 
vi ~/.bashrc
 
执行:source ~/.bashrc 让修改的环境变量配置生效
试了一把,居然可以了;
然后试着重启容器,进入容器查看环境变量,生效了。
OK , 就它了!!!
 
*****************************************
自己制作的镜像重新运行:
docker run -d -p 8000:8000 --name fast_api  -v /home/aeotrade/fast_api_map_data/at_xml_exchange:/project -t ubuntu18_python36:3.0
 
更新代码的时候,上传到服务器:
/home/aeotrade/fast_api_map_data/at_xml_exchange 路径下
 
uvicorn启动服务: --reload 是方便开发模式下,修改后端代码后,自动重载服务让配置生效
uvicorn main:app --reload --host=0.0.0.0 --port=8008

利用gunicorn管理uvicorn,以守护进程方式启动
参考:https://www.cnblogs.com/itBlogToYpl/p/13153785.html

# 启动命令
gunicorn main:app -b 0.0.0.0:8000 -w 2 -k uvicorn.workers.UvicornWorker  # -D 守护启动 -c 配置文件
 
pip3 install uvloop -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com


pip3 install httptools -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com


# 后台运行
nohup gunicorn main:app -b 0.0.0.0:8000 -w 2 -k uvicorn.workers.UvicornWorker &


# 启动定时任务程序
容器里边:进入 /project/xml_data_exchange/utils

启动:
nohup python3 start.py &


博客推荐:
https://www.cnblogs.com/zhangliang91/p/13388200.html

gunicorn配置文件:
https://blog.csdn.net/sinat_42483341/article/details/103007231
原文地址:https://www.cnblogs.com/hellojesson/p/13820171.html