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

文章目录

  • 一、前置条件
    • 1.创建 ubuntu 镜像源文件【sources.list】
    • 2.下载 python 安装包【Python-3.9.10.tgz】
  • 二、构建方法
    • 1.构建目录
    • 2.创建DockerFile
    • 3.打包镜像


一、前置条件

  1. 配置一下 ubuntu 的镜像源
  2. 下载 python 安装包

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

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

访问官网下载页 https://www.python.org/downloads/release/python-3910/
下拉选择 Gzipped 包
构建Docker基础镜像(ubuntu20.04+python3.9.10+pytorch-gpu-cuda11.8)_第1张图片


二、构建方法

1.构建目录

|---baseIMG_ub2004py3910pytorchgpucuda118
		|
		|-----python
		|		|-----Python-3.9.10.tgz
		|
		|-----ubuntu
		|		|-----sources.list
		|
		|-----Dockerfile

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


# 更新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 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 .


3.打包镜像

docker build -t ub2004py3910pytorchgpucuda118:latest .

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

你可能感兴趣的:(Python,Docker,docker,pytorch,容器)