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