概述
功能需求,把python腳本移植到docker中。
因為python腳本中有使用freeswitch的ESL接口,所以需要安裝python-ESL依賴庫。
本文記錄在python:3.10.14-slim的docker鏡像上編譯安裝python-ESL依賴庫的流程。
環境
docker engine: Version 24.0.6
docker images: python:3.10.14-slim
docker準備
docker hub拉取python鏡像。
sudo docker pull python:3.10.14-slim
啟動docker容器,版本使用centos7。
sudo docker run -itd --name python.3.10.14-httpapi python:3.10.14-slim
sudo docker exec -it python.3.10.14-httpapi bash
以下步驟均為docker容器“python.3.10.14-httpapi”內部操作。
系統工具
在python容器中安裝相關工具。
apt-get update
apt-get -y install swig gcc g++
安裝失敗
在python容器中直接使用pip安裝“python-ESL”庫會失敗。
pip install python-ESL
Using cached python-ESL-1.4.18.tar.gz (40 kB)
...
error: subprocess-exited-with-error
...
swig -python -classic -c++ -DMULTIPLICITY -threads -I. -o ESL_wrap.cpp ESL.i
Deprecated command line option: -classic. This option is no longer available.
error: command '/usr/bin/swig' failed with exit code 1
...
ERROR: Failed building wheel for python-ESL
從上面的報錯信息中,新版本的swig不支持參數“-classic”,也嘗試了稍低版本的Debian,swig3也一樣不支持該參數了。
修改源碼包
搜索源碼包“python-ESL-1.4.18.tar.gz”。
下載地址:https://pypi.org/project/python-ESL/
wget https://files.pythonhosted.org/packages/26/41/a4396267f6700ce4356425343d57fc0dc1bd5f7700b7dbc6b03c5d2be3af/python-ESL-1.4.18.tar.gz
tar -zxvf python-ESL-1.4.18.tar.gz
cd python-ESL-1.4.18
vi setup.py ##刪除行中的'-classic'選項
swig_opts=['-classic', '-c++', '-DMULTIPLICITY',
保存退出,重新對python-ESL-1.4.18目錄打包。
tar -zcvf python-ESL-1.4.18-2.tar.gz python-ESL-1.4.18
重新安裝
ESL依賴庫,將修改后的源碼包“python-ESL-1.4.18-2.tar.gz”拷貝進python-docker容器內,執行安裝。
宿主機執行拷貝。
sudo docker cp python-ESL-1.4.18-2.tar.gz python.3.10.14-httpapi:/root/
容器內執行安裝。
pip3 install /root/python-ESL-1.4.18-2.tar.gz
Installing collected packages: python-ESL
Successfully installed python-ESL-1.4.18
檢查pip列表。
pip list
Package Version
---------- -------
pip 23.0.1
python-ESL 1.4.18
setuptools 65.5.1
wheel 0.43.0
ESL庫安裝完成,就可以在容器中執行自己的python腳本了。
鏡像清理
apt-get -y remove swig gcc g++
apt-get -y autoremove
總結
python基礎鏡像更換為python:3.10.14-slim,鏡像大小只有150M左右。
使用python鏡像來運行外部腳本,脫離fs本身的docker容器限制。
空空如常
求真得真