Docker学习过程 (8-实战-Python)

前言: 今天的内容是用容器搭建一个 Python 容器。以前在电脑里安装 Python2.7,Python3.6,Python3.7,有时还有 Anaconda,结果就是在控制台运行 pip 或 pip3 的时候常常达不到自己想要的结果。那现在就来一次实践,把 Python 安在容器里,要是时就启动对应容器,这样就方便多了。
  要安装 Python,就需要平台,我们用的策略是在一个微型操作系统的容器下安装 Python,这里的微型操作系统选 ubuntu,它是 Linux 系统的一个分支。里面更换一下源,因为安装 Python 过程中要安装一些工具,换国内阿里源快得多,通过 ADD 命令把文件加入镜像构造的过程。
  下面我将演示整个操作流程,我的操作系统是 Ubuntu18。只要是 Ubuntu系统都可以。我的编辑工具用 Vim,用其他文本编辑器也可以,把写好的 Dockerfile 放到指定文件夹下就可以了。

步骤

准备

Ctrl + Alt + T 打开终端,新建文件夹 DPython 并进入,并编写 Dockerfile 文件。下面两条命令写完后回车开始编写 Dockerfile。

mkdir DPython
cd DPython
vim Dockerfile   	#里面输入Dockerfile 的内容。输好后保存,关闭
Dockerfile 内容
# Python
# VERSION 0.0.1

# 基础镜像
FROM ubuntu
# 维护者信息
MAINTAINER Geng Li <[email protected]>

ADD source.list /root

# 安装Python
RUN buildDeps="libffi-dev libssl-dev libreadline-dev wget gcc make autoconf tcl-dev tk-dev ca-certificates" \	
    && cp -f /root/source.list /etc/apt/source.list \
    && rm /root/source.list \
    && export DEBIAN_FRONTEND=noninteractive \
    && apt-get update && apt-get install -y --no-install-recommends $buildDeps tcl tk \
    && wget -O python.tgz "https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz" \
    && mkdir -p /usr/src/python \
    && tar -zxf python.tgz -C /usr/src/python --strip-components=1 \
    && cd /usr/src/python \
    && ./configure --enable-shared \
    && make \
    && make install \
    && cp libpython3.7m.so.1.0 /usr/lib/ \
    && rm -f python.tgz \
    && rm -rf /usr/src/python \
    && apt-get remove -y $buildDeps

# 打印版本信息
CMD ["python3", "--version"]
阿里源
vim source.list 	#里面放阿里Ubuntu18的源,把下面内容复制进去就行了

阿里源

#Ubuntu18 阿里源. source.list 文件的内容
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

下面看一下效果:
大概内容如下,python.tgz 是在运行 Dockerfile 中自动下载的。我的文件是成功完成后的结果。
在这里插入图片描述

Dockerfile 内容讲解

在构建镜像的过程中,遇见几个坑。中间有点小错不要紧,最后输出有 Successfully 就行了。

# Python
# VERSION 0.0.1

# 基础镜像
FROM ubuntu 							#指定基础镜像
# 维护者信息
MAINTAINER Geng Li <[email protected]>	#维护者信息,这个信息邮箱和QQ号是真的

ADD source.list /root					#把容器之外的和Dockerfile在同一个文件夹下的 source.list 添加到容器的 /root 目录下,换成其他目录也可以的 

# 安装Python
	# 添加必要的编译Python的工具,微型的Ubuntu系统里面只有最基本的一些工具和命令
	# 报错 ModuleNotFoundError: No module named '_ctypes' 时注意看有没有 libffi-dev libssl-dev libreadline-dev
RUN buildDeps="libffi-dev libssl-dev libreadline-dev wget gcc make autoconf tcl-dev tk-dev ca-certificates" \
	# 把 ADD 引进的文件覆盖 /etc/apt/source.list,-f 参数强制覆盖
    && cp -f /root/source.list /etc/apt/source.list \
 	# 移除/root/source.list
    && rm /root/source.list \
    # 不添下面这句会出现一个交互要你输入所在地址的界面,让无人看守的自动化梦想毁灭
    # Please select the geographic area in which you live...具体参见https://blog.csdn.net/taiyangdao/article/details/80512997
    && export DEBIAN_FRONTEND=noninteractive \
    # 更新软件源
    && apt-get update && apt-get install -y --no-install-recommends $buildDeps tcl tk \
    # 下载Python3.7 包,这里下载有点慢,多等待
    && wget -O python.tgz "https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz" \
    && mkdir -p /usr/src/python \
    #解压Python包 python.tgz
    && tar -zxf python.tgz -C /usr/src/python --strip-components=1 \
    && cd /usr/src/python \
    && ./configure --enable-shared \
    # 编译 Python
    && make \
    && make install \
    && cp libpython3.7m.so.1.0 /usr/lib/ \
    && rm -f python.tgz \
    && rm -rf /usr/src/python \
    # 移除无用的工具,让镜像在满足要求的情况下尽可能纯洁
    && apt-get remove -y $buildDeps

# 打印版本信息
# 在启动 Python 容器的时候执行
CMD ["python3", "--version"]

安装过程很慢,因为微型的操作系统没有构建 Python 的工具,要慢慢下载!如果你电脑只为了运行 Python,显然没必要这样来安装,我们现在只是在学习。也可以直接安装别人做好的 Python 容器,如果已经安装了Docker 的话。

构建镜像
sudo docker build -t gengli/python37:0.0.1 ./DPython

其中构建成功的镜像完整名字是 gengli/python37:0.0.1,也可以简单写成 amao 等。
成功后最后会有 Successfully 字样。本来想截图,结果显示结果被我清楚了。

查看与测试

查看镜像

sudo docker images		#可以看到 gengli/python37:0.0.1,也就是在 Dockerfile 里面写的那个
sudo docker run -it --name python gengli/python37:0.0.1 #启动容器,查看输出

在这里插入图片描述

下面写一个 first.py 文件测一下镜像,文件放在 /home/用户名 目录下,用户名是你自己的用户:
first.py 内容如下:

print ("I Love Docker and Python")

我们先删了启动的 python 容器,因为要把外面文件挂载到容器里,我记得运行的容器不能挂载文件夹和文件,直接在启动时候挂载。

sudo docker rm python
sudo docker run --rm -t --name python -v /home/gengli/first.py:/first.py gengli/python37:0.0.1 python3 first.py

在这里插入图片描述
里面的参数解析如下:

sudo docker run gengli/python37:0.0.1 	#由镜像 gengli/python37:0.0.1 启动容器
-t 										#分配一个伪终端,可以去掉
--name python 							#把镜像命名成 python
-v /home/gengli/first.py:/first.py		#把主机 /home/gengli 下的文件 first.py 挂载在容器根目录 / 下
										#注意,此处主机文件必须是绝对路径
python3 first.py 						#指定启动容器的方式和参数(first.py),也可写成 /first.py

至此,Python容器的构建就结束了!其实我们可以可以看到,不光要懂 Dockerfile 指令,还要懂 Linux 的 Shell 指令才行。事实上,Shell 脚本很值得一学,大数据如 Hadoop 和 Dockerfile 的很多命令都是仿照 Shell 脚本命令的。
当然,这个容器还不能很好地满足交互等需要,等学的更多了,再完善!!!

猜你喜欢

转载自blog.csdn.net/gengli2017/article/details/86495786
今日推荐