星辰儿啊 2024-04-24 17:35 采纳率: 43.1%
浏览 7

关于docker部署的问题

宿主机:centos7.9
docker版本:25.0.2
基础镜像:centos:7.9.2009

我打算使用docker部署一个flask项目,
首先:我基于centos:7.9.2009镜像,编写了一个dockerfile,进行自定义镜像;
镜像成功创建,容器也正常启动,

但是通过dockerfile,自定义镜像中有2个问题:
1,dockerfile中,我指定了python版本为3.10,但是容器启动,进入容器运行python3之后,
发现python版本为:3.6!这里不知道是什么原因
2,另外在docjerfile,运用安装python依赖文件之后也会报错,成功不了

辛苦帮我解决下。
其中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


#RUN 执行以下命令
RUN yum update -y
RUN yum install python3.10.9  python3-pip -y
RUN pip3 install -r requirements.txt

RUN pip3 install flask
RUN pip3 install uuid
RUN pip3 install pymysql
RUN pip3 install dbutils
RUN pip3 install baidu-aip  -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip3 install chardet  -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN mkdir -p /data/www

#容器启动时执行命令
CMD ["python3","chat_service.py"]

  • 写回答

5条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-04-24 17:36
    关注

    引自免费微信小程序:皆我百晓生

    根据您提供的信息,我发现了几个可能导致问题的地方。让我们一步步解决这些问题。

    问题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
    

    这样应该可以解决您遇到的两个问题。如果还有任何问题,请随时告诉我。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月24日

悬赏问题

  • ¥15 远程访问linux主机超时
  • ¥15 odoo17存货管理优势于中国国内该行业传统ERP或MES的详细解读和举例
  • ¥15 CPU卡指令整合指令数据都在图片上
  • ¥15 火车票关联12306问题
  • ¥15 odoo17处理受托加工产品
  • ¥15 如何用MATLAB编码图三的积分
  • ¥15 圆孔衍射光强随孔径变化
  • ¥15 MacBook pro m3max上用vscode运行c语言没有反应
  • ¥15 ESP-PROG配置错误,ALL ONES
  • ¥15 结构功能耦合指标计算