
python3.6 的 Ubuntu16.04 Docker 镜像制作
公司环境用的是 Ubuntu 16.04 和 Python 3.6,为了方便部署 Web 应用和离线部署 AI 分析服务,我动手做了个基础 Docker 镜像。本文记录了换国内 apt 源、装 Python 3.6、配置 pip 清华源到构建镜像的全过程,很简单。
出于某些原因,公司使用 ubuntu16.04(xenial) 和 python3.6,为了方便部署写的 web 应用或 AI 分析服务,尤其是某些情况下的离线部署,有必要搞个 Docker 镜像。因为公司使用环境大部分类似,有必要先弄一个满足基本需求的镜像,后续再定制相应环境镜像即可。
本文主要讲一下制作这个基础镜像的过程,灰常简单。
需求
先理一下我们的需求:
- ubuntu16.04(xenial),apt-get 使用国内源,安装基本的工具
- 最新的 python3.6,目前最新的为 python3.6
- 最新的 pip3,而且要更改配置为国内的 pypi 源
准备工作
我们使用 Dockerfile 制作镜像,这里需要准备一些文件,所以先创建个目录,在此目录下进行操作,比如目录是 docker_ubuntu_python3.6
1 2 3mkdir docker_ubuntu_python3.6 # 进入目录 cd docker_ubuntu_python3.6
此目录下后面我们需要创建三个文件,最好是不要包含其它文件:
- sources.list
- pip.conf
- Dockerfile
注:
这个目录不要再有其它文件,否则还要将多余的文件加到 .dockerignore
sources.list
为更改镜像中的 apt-get 源为国内源而准备。
新建文件 sources.list,内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties deb http://archive.canonical.com/ubuntu xenial partner deb-src http://archive.canonical.com/ubuntu xenial partner deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
这里使用的是阿里云 ubuntu16.04 的源,可选择其它源。
pip.conf
为加速 pip 安装第三方依赖的速度而准备。
创建文件 pip.conf,内容如下:
1 2 3 4 5 6[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host=pypi.tuna.tsinghua.edu.cn disable-pip-version-check = true timeout = 6000
这里使用的清华的 pypi 源,可选择使用其它源!
Dockerfile
最后是制作 Docker 镜像的 Dokerfile,新建文件,名为 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 26FROM ubuntu:xenial AS ubuntu16_04-with-python3_6 LABEL author="5km" # 用ubuntu国内源替换默认源 RUN rm /etc/apt/sources.list COPY sources.list /etc/apt/sources.list # 安装python3.6必要的包。源镜像太精简了,ip ifconfig之类的都没有。后续安装python pip也需要一些。但是build_essential似乎不必须,先去了。如果后面安装numpy之类需要gcc了,再加上 RUN apt-get update \ && apt-get install -y libapt-pkg5.0 apt-transport-https iproute2 net-tools ca-certificates curl wget software-properties-common \ && apt-get clean # 安装python3.6 来自第三方 RUN add-apt-repository ppa:deadsnakes/ppa \ && apt-get update \ && apt-get install -y python3.6 python3.6-dev python3-pip \ && apt-get clean # 和自带的3.5共存 RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1 \ && update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2 \ && update-alternatives --config python3 # 更改 pip 源为清华源,加速 RUN mkdir /root/.pip COPY pip.conf /root/.pip/pip.conf # 更新 pip3 RUN pip3 install --upgrade pip \ && rm -rf /root/.cache/pip #print()时在控制台正常显示中文 ENV PYTHONIOENCODING=utf-8
内容中有注释,所以很容易明白过程!!!
准备工作大功告成,看一下当前目录下的文件结构:
1 2 3 4 5 6 7 8irecog@irecog [01:59:36 PM] [~/docker_ubuntu_python3.6] -> % tree . . ├── Dockerfile ├── pip.conf └── sources.list 0 directories, 3 files
制作镜像
一条命令,完成制作:
1docker build -t ubuntu16_04-with-python3_6 .
理论上不到一杯咖啡过后,就能看到制作完成的镜像:
1 2 3 4irecog@irecog [02:00:40 PM] [~/docker_ubuntu_python3.6] -> % docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu16_04-with-python3_6 latest 7883d49a0394 47 minutes ago 623MB
过程回顾
下面是整个操作过程的 asciinema 录制,可参考录制进行操作:
如果上面过程录制加载很慢,可自行安装 asciinema ,使用 asciinema 播放 docker-ubuntu1604-python36.json
1asciinema play docker-ubuntu1604-python36.json



