• 构建Docker基础镜像(ubuntu20.04+python3.9.10+pytorch-gpu-cuda11.8)



    一、前置条件

    1.创建 ubuntu 镜像源文件【sources.list】

    内容如下

    deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
    
    deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
    
    deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
    
    deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
    
    deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    2.下载 python 安装包【Python-3.9.10.tgz】

    访问官网下载页 https://www.python.org/downloads/release/python-3910/
    下拉选择 Gzipped 包
    在这里插入图片描述


    二、构建方法

    1.构建目录

    |---baseIMG_ub2004py3910pytorchgpucuda118
    		|
    		|-----python
    		|		|-----Python-3.9.10.tgz
    		|
    		|-----ubuntu
    		|		|-----sources.list
    		|
    		|-----Dockerfile
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.创建DockerFile

    #FROM python:3.9
    FROM ubuntu:20.04
    
    # 作者
    MAINTAINER ps
    
    # 工作目录
    WORKDIR /baseIMG_ub2004py3910pytorchgpucuda118
    
    # 宿主机文件复制到镜像
    ADD . /baseIMG_ub2004py3910pytorchgpucuda118
    
    # 配置时区
    ENV TZ=Asia/Shanghai
    RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
    
    # RUN rm -f /etc/localtime
    # RUN  ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' > /etc/timezone
    
    
    # 更新ubuntu
    RUN mv ./ubuntu/sources.list /etc/apt/sources.list
    #RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 871920D1991BC93C
    RUN apt --fix-broken -y install
    RUN apt -y update
    RUN apt -y upgrade
    
    
    ## 安装python
    #RUN apt-get install python3.7
    
    
    # # 安装chrome
    # ## 安装依赖
    # RUN apt -y install libxss1 libappindicator1 libindicator7 fonts-liberation libasound2
    # RUN apt -y install libatk-bridge2.0-0 libatspi2.0-0 libcurl3-gnutls libcurl3-nss libcurl4
    # RUN apt -y install libdrm2 libgbm1 libgtk-3-0 libxkbcommon0 xdg-utils wget
    # RUN apt --fix-broken -y install
    # # 安装浏览器
    # RUN dpkg -i ./chrome/google-chrome-stable_current_amd64.deb
    # # 查看版本
    # RUN google-chrome --version
    # # Google Chrome 101.0.4951.64
    # 
    # # 安装chromedriver
    # # 安装unzip
    # RUN apt install unzip
    # # 解压
    # RUN unzip ./chrome/chromedriver_linux64.zip
    # # 移动并创建软连接到默认路径,后续启动selenium时就不需要指定chromedriver的路径了
    # RUN mv ./chromedriver /usr/local/share/chromedriver
    # RUN ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
    
    
    
    
    # 安装python
    RUN apt -y install build-essential libbz2-dev
    RUN apt -y install libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev
    RUN apt -y install zlib1g-dev
    RUN mv ./python /usr/local/bin/python
    RUN cd /usr/local/bin/python && tar -xzvf Python-3.9.10.tgz && cd Python-3.9.10 && ./configure --enable-optimizations && make && make install
    
    # 安装pytorch-gpu-cuda11.8
    RUN apt install -y libbz2-dev
    # 阿里源
    RUN pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/
    RUN python3 -m pip install -U pip setuptools wheel
    RUN pip3 install apscheduler
    
    ## CPU
    #RUN pip3 install torch torchvision torchaudio
    # GPU
    RUN pip3 install --timeout 18000 torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
    
    
    # linux下需要此步骤以防报错  ModuleNotFoundError: No module named '_bz2'
    # RUN cp /usr/lib/python3.8/lib-dynload/_bz2.cpython-38-x86_64-linux-gnu.so /usr/local/lib/python3.7/lib-dynload/_bz2.cpython-37m-x86_64-linux-gnu.so
    
    
    
    # 镜像打包命令
    # docker build -t ub2004py371chm101chmdr101:latest .
    
    
    • 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
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84

    3.打包镜像

    docker build -t ub2004py3910pytorchgpucuda118:latest .
    
    • 1

    ps:创建镜像名为 ub2004py3910pytorchgpucuda118 标签为 latest 的镜像,从当前路径下的 DockerFile 文件打包

  • 相关阅读:
    【vue3源码】八、reactive——Collection的响应式实现
    概率基础——几何分布
    [iOS开发]iOS持久化
    【JAVASE系列】02_运算符与控制语句
    C#异步和多线程
    Java实战项目之图书借阅管理系统【源码+课后指导】
    JVM垃圾回收系列之垃圾收集算法
    Python多线程
    从Docker初识K8S
    Flink之状态TTL机制
  • 原文地址:https://blog.csdn.net/weixin_43721000/article/details/134378773