引自免费微信小程序:皆我百晓生
根据您提供的信息,我发现了几个可能导致问题的地方。让我们一步步解决这些问题。
问题1:Python 版本不匹配
看起来您的 Dockerfile 中尝试安装了 Python 3.10.9,但在容器内部运行的却是 Python 3.6。这可能是因为 CentOS 7 的默认软件仓库并没有包含 Python 3.10。您可以选择从 EPEL(Extra Packages for Enterprise Linux)仓库安装 Python 3.10 或者构建自定义的 Python 镜像。
下面是使用 EPEL 安装 Python 3.10 的方法:
# 添加 EPEL 源
RUN rpm --import https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7
RUN yum-config-manager --add-repo http://download.fedoraproject.org/pub/epel/7/x86_64/
# 更新并安装 Python 3.10
RUN yum update -y && yum install -y python310u python310u-devel python310u-pip
RUN alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
# 将 python 和 pip 指向 Python 3.10
RUN ln -s /usr/bin/python3.10 /usr/bin/python3
RUN ln -s /usr/bin/pip3.10 /usr/bin/pip3
# 这里继续您原来的 RUN 命令...
问题2:安装依赖失败
对于安装依赖包的问题,可能是由于网络连接问题导致的。尝试将 pip
的源更改为国内的镜像源,例如清华大学的镜像源。您已经部分做了这个操作,但有些依赖可能没有指定镜像源。确保所有 pip
命令都使用了镜像源,如下所示:
# ...
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple flask
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple uuid
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple pymysql
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple dbutils
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple baidu-aip
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple chardet
# ...
更新后的 Dockerfile:
#Dockerfile
# Base images 基础镜像
FROM centos:7.9.2009
#MAINTAINER 维护者信息
LABEL maintainer w
# 工作目录
WORKDIR /data/www/counseling_v1/
# 拷贝文件至工作目录
COPY counseling_v1 /data/www/counseling_v1
# 添加 EPEL 源
RUN rpm --import https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-7
RUN yum-config-manager --add-repo http://download.fedoraproject.org/pub/epel/7/x86_64/
# 更新并安装 Python 3.10
RUN yum update -y && yum install -y python310u python310u-devel python310u-pip
RUN alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
# 将 python 和 pip 指向 Python 3.10
RUN ln -s /usr/bin/python3.10 /usr/bin/python3
RUN ln -s /usr/bin/pip3.10 /usr/bin/pip3
# 安装依赖
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt
# 安装其他依赖
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple flask
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple uuid
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple pymysql
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple dbutils
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple baidu-aip
RUN pip3 install --index-url https://pypi.tuna.tsinghua.edu.cn/simple chardet
# 创建目录
RUN mkdir -p /data/www
# 容器启动时执行命令
CMD ["python3", "chat_service.py"]
保存修改后的 Dockerfile,然后在项目根目录下执行以下命令来构建新的镜像:
docker build -t counseling:v1 .
接着,启动一个新的容器:
docker run -it --name counseling_container counseling:v1
这样应该可以解决您遇到的两个问题。如果还有任何问题,请随时告诉我。