CentOS安装Python及Dockerfile中配置Python环境

前言

通过yum安装Python3.7目前测试的都失败了,本文使用的是服务器上编译的方式。

CentOS中安装

CentOS中安装Python 3.7,可以按照以下步骤进行:

CentOS中更新系统和安装依赖

首先更新系统并安装必要的依赖:

1
2
yum update -y
yum install -y gcc openssl-devel bzip2-devel libffi-devel wget make

下载并安装Python 3.7

接下来,下载Python 3.7的源码包并解压:

1
cd /root && curl -o Python-3.7.9.tgz https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz && tar xzf Python-3.7.9.tgz

进入解压后的目录并编译安装Python 3.7:

1
2
3
cd Python-3.7.9
./configure --enable-optimizations
make altinstall

make altinstall命令用于防止替换系统默认的Python版本。

验证Python 3.7的安装

安装完成后,验证Python 3.7是否安装成功:

1
python3.7 --version

如果显示Python 3.7的版本号,则表示安装成功。

设置Python 3.7为默认Python解释器(可选)

如果你希望将Python 3.7设置为默认的Python解释器,可以使用以下命令创建一个符号链接:

1
ln -s /usr/local/bin/python3.7 /usr/bin/python3

然后验证默认Python版本:

1
python --version

安装pip(可选)

如果你需要使用pip来安装Python包,可以运行以下命令安装pip

1
2
curl https://bootstrap.pypa.io/pip/3.7/get-pip.py -o get-pip.py
python3.7 get-pip.py

验证pip是否安装成功:

1
pip --version

Dockerfile中

1
2
3
4
5
6
RUN yum install -y gcc openssl-devel bzip2-devel libffi-devel wget make
RUN cd /root && curl -o Python-3.7.9.tgz https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz && tar xzf Python-3.7.9.tgz
RUN cd /root/Python-3.7.9 && ./configure --enable-optimizations && make altinstall
RUN ln -s /usr/local/bin/python3.7 /usr/bin/python3
RUN curl https://bootstrap.pypa.io/pip/3.7/get-pip.py -o get-pip.py
RUN python3.7 get-pip.py

Python项目运行报错

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

原因是OpenCV因为有图像化显示的功能,所以需要图形化的库。

安装

1
RUN yum install -y mesa-libGL-devel

完整配置

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Pull base image
FROM centos:7

MAINTAINER psvmc "psvmc@outlook.com"

# Set Charset
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

# Set TimeZone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# YUM镜像
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.huaweicloud.com/repository/conf/CentOS-7-anon.repo
RUN yum makecache

# 安装图形化库
RUN yum install -y mesa-libGL-devel

# 安装Python
RUN yum install -y gcc openssl-devel bzip2-devel libffi-devel wget make
RUN cd /root && curl -o Python-3.7.9.tgz https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz && tar xzf Python-3.7.9.tgz
RUN cd /root/Python-3.7.9 && ./configure --enable-optimizations && make altinstall
RUN ln -s /usr/local/bin/python3.7 /usr/bin/python3
RUN curl https://bootstrap.pypa.io/pip/3.7/get-pip.py -o get-pip.py
RUN python3.7 get-pip.py
# pip镜像
RUN echo "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple" > /etc/pip.conf
# RUN pip install --upgrade pip
RUN pip install pipenv

# 项目配置
RUN mkdir /opt/z-card-recognize/
COPY . /opt/z-card-recognize/
RUN chmod 755 /opt/z-card-recognize/main.py
RUN chmod 755 /opt/z-card-recognize/startup.sh

# Define WORKDIR.
WORKDIR /opt/z-card-recognize/
# Install Dependence
RUN pipenv install --skip-lock

# Expose ports.
EXPOSE 8000

ENTRYPOINT /opt/z-card-recognize/startup.sh

startup.sh

1
pipenv run python3.7 main.py

注意

尽量把项目的配置放在最后,这样如果只是项目配置变更,再次构建的时候可以利用缓存减少构建时间。

构建运行

构建运行

构建

1
docker build -t psvmc/z-card-recognize .

如果构建一直失败可以禁用缓存

1
docker build --no-cache -t psvmc/z-card-recognize .

运行

1
docker run -d -p 8000:8000 --name z-card-recognize --restart=always psvmc/z-card-recognize

查看是否启动成功

1
2
lsof -i:8000
curl http://localhost:8000

查看启动日志

1
docker logs z-card-recognize

删除

1
2
docker stop z-card-recognize
docker rm z-card-recognize

容器内操作

进入容器环境

1
docker exec -t -i z-card-recognize /bin/bash

退出容器环境

1
exit

地址访问

容器内能访问 http://localhost:8000 容器外不行

这一般是因为

内部的服务应该监听在 0.0.0.0:8000 上,而不是 localhost:8000

如果容器内有 netstat 工具,没有的话安装

1
yum install -y net-tools

运行以下命令:

1
netstat -tuln

输出类似于:

1
2
3
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN

注意看 Local Address 列。

如果你的服务正确监听在 0.0.0.0:8000 上,你会看到 0.0.0.0:8000

删除临时构建

查看所有镜像

1
docker images

您可以使用以下命令删除所有 REPOSITORYTAG 都是 <none> 的镜像:

查询临时镜像

1
docker images -f "dangling=true"

使用 docker rmi

1
docker rmi $(docker images -f "dangling=true" -q)

解释:

docker images -f "dangling=true":过滤出所有没有标签的镜像(即 dangling 镜像)。

删除未使用的镜像

1
docker image prune -f

这条命令会删除所有悬挂的(dangling)镜像,即 celles没有被标记并且没有被任何容器使用的镜像。

删除所有未使用的镜像,包括悬挂的和被标记的

如果你想要删除所有未被任何容器使用的镜像,可以使用 -a 选项:

1
docker image prune -a -f

有的镜像无法删除,是因为已生成容器

获取指定镜像生成的容器列表

1
2
3
4
docker ps -a --filter "ancestor=image_id"

docker ps -a --filter "ancestor=e3e3640f0c5f"
docker ps -a --filter "ancestor=eeb6ee3f44bd"

查看镜像的继承关系

1
docker inspect 006eb1b6163b

查看容器列表

1
docker ps -a --format "{{.Names}}: {{.Image}} [{{.Status}}] {{.Ports}}"