想在容器中使用python,但python的官方镜像太过于精简,里面不管是yum还是vi 还是vim都没有安装,使用起来比较不方便,于是决定在centos基础镜像中安装python环境,具体步骤和遇到的问题如下:
下载centos官方镜像
[root@lingg ~]# docker pull centos
运行centos镜像
[root@lingg ~]# docker run -it 镜像ID /bin/bash
安装python环境
yum -y install python3
查看python版本
[root@3e3dc19e8e08 /]# python3
Python 3.6.8 (default, Mar 19 2021, 05:13:41)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
导入包cv2失败
[root@3e3dc19e8e08 /]# python3
Python 3.6.8 (default, Mar 19 2021, 05:13:41)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "" , line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
安装opencv-python
[root@3e3dc19e8e08 /]# pip3 install opencv-python
WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead.
Collecting opencv-python
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(, ' Connection to pypi.python.org timed out. (connect timeout=15)')': /simple/opencv-python/
解决:
可以在使用pip的时候在后面加上-i参数,指定pip源
eg: pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
mkdir ~/.pip
vim ~/.pip/pip.conf
提示vim :command not found的话换成vi ~/.pip/pip.conf
然后在pip.conf中输入
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
报错:
Collecting opencv-python
Downloading http://pypi.doubanio.com/packages/01/9b/be08992293fb21faf35ab98e06924d7407fcfca89d89c5de65442631556a/opencv-python-4.5.3.56.tar.gz (89.2MB)
100% |████████████████████████████████| 89.2MB 13.9MB/s
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "" , line 1, in <module>
File "/tmp/pip-build-s_bpkw_2/opencv-python/setup.py", line 10, in <module>
import skbuild
ModuleNotFoundError: No module named 'skbuild'
解决:
pip3 install scikit-build
报错:
Building wheels for collected packages: opencv-python
Running setup.py bdist_wheel for opencv-python ... error
Complete output from command /usr/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-nnxlnrca/opencv-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmp2i6xp72gpip-wheel- --python-tag cp36:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/skbuild/setuptools_wrap.py", line 564, in setup
cmkr = cmaker.CMaker(cmake_executable)
File "/usr/local/lib/python3.6/site-packages/skbuild/cmaker.py", line 95, in __init__
self.cmake_version = get_cmake_version(self.cmake_executable)
File "/usr/local/lib/python3.6/site-packages/skbuild/cmaker.py", line 82, in get_cmake_version
"Problem with the CMake installation, aborting build. CMake executable is %s" % cmake_executable)
Problem with the CMake installation, aborting build. CMake executable is cmake
----------------------------------------
Failed building wheel for opencv-python
Running setup.py clean for opencv-python
Failed to build opencv-python
Installing collected packages: numpy, opencv-python
Running setup.py install for opencv-python ... error
Complete output from command /usr/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-nnxlnrca/opencv-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-tuz2u8xk-record/install-record.txt --single-version-externally-managed --compile:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/skbuild/setuptools_wrap.py", line 564, in setup
cmkr = cmaker.CMaker(cmake_executable)
File "/usr/local/lib/python3.6/site-packages/skbuild/cmaker.py", line 95, in __init__
self.cmake_version = get_cmake_version(self.cmake_executable)
File "/usr/local/lib/python3.6/site-packages/skbuild/cmaker.py", line 82, in get_cmake_version
"Problem with the CMake installation, aborting build. CMake executable is %s" % cmake_executable)
Problem with the CMake installation, aborting build. CMake executable is cmake
----------------------------------------
Command "/usr/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-nnxlnrca/opencv-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-tuz2u8xk-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-nnxlnrca/opencv-python/
解决:升级pip
pip3 install --upgrade pip
报错:
[root@3e3dc19e8e08 /]# python3
Python 3.6.8 (default, Mar 19 2021, 05:13:41)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "" , line 1, in <module>
File "/usr/local/lib64/python3.6/site-packages/cv2/__init__.py", line 5, in <module>
from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
解决:
pip3 uninstall opencv-python
pip3 install opencv-python-headless
同时按住Ctrl+p+q退出容器,保证容器不关闭。
然后执行docker ps
命令查看当前运行中的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e3dc19e8e08 5d0da3dc9764 "/bin/bash" 29 minutes ago Up 29 minutes
将配置好环境的docker commit成一个新的镜像
docker commit 3e3dc19e8e08 lingg/centos:3.6.8
执行docker images
命令查看刚刚commit的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
lingg/centos 3.6.8 898e55b46a3d 27 seconds ago 639MB
向/root/centos-py/目录下拷贝两张照片
[root@lingg ~]# cp images/275cd4eb-8825-49ad-b2fa-44d9aa67c0c6.png images/67f7b6b4-5324-4e2d-935f-7d04b6d8a85a.png ./centos-py/
启动新镜像 将/root/centos-py/目录挂载到镜像里面的/usr/lcoal/src目录下
[root@lingg ~]# docker run -it -v /root/centos-py/:/usr/local/src --name python -p 10000:8080 898e55b46a /bin/bash
查看照片shape属性
[root@c3dadddc745e src]# python3
Python 3.6.8 (default, Mar 19 2021, 05:13:41)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> image = cv2.imread("/usr/local/src/275cd4eb-8825-49ad-b2fa-44d9aa67c0c6.png")
>>> print(image.shape)
(80, 320, 3)
成功