关于alpine:还在用Alpine作为你Docker的Python开发基础镜像其实Ubuntu更好一点

8次阅读

共计 3229 个字符,预计需要花费 9 分钟才能阅读完成。

原文转载自「刘悦的技术博客」https://v3u.cn/a_id_173

个别状况下,当你想为你的 Python 开发环境抉择一个根底镜像时,大多数人都会抉择 Alpine,为什么?因为它太小了,仅仅只有 5 MB 左右(比照 Ubuntu 系列镜像靠近 100 MB),但事实的假相是,咱们抉择根底镜像并不是为了体验一下 Python 语法而已,在此基础上,咱们须要调试和装置各种扩大,可能会装置很多三方依赖,甚至预设更多服务,在这种环境下,Alpine 就并非是一个很好的抉择了,本次咱们就来别离在 Alpine 和 Ubuntu 上来体验一下装置和编译 Python 的区别。

首先别离拉取 Alpine 和 Ubuntu 的镜像:

docker pull ubuntu:18.04  
docker pull alpine

拉取结束后,能够看到,体积上的确差距显著:

REPOSITORY                  TAG                   IMAGE ID            CREATED             SIZE  
ubuntu                      18.04                 6526a1858e5d        2 weeks ago         64.2MB  
alpine                      latest                a24bb4013296        3 months ago        5.57MB

ubuntu 占用 64mb,而 alpine 仅仅 5.57mb。

然而先别着急,假如咱们的 python 利用须要做一些科学计算,并且将数据以图形的形式展现进去,这时候就须要 matplotlib 和 pandas 这两个库的帮忙了,先用 ubuntu 来装置这俩个库,编写 Dockerfile.ubuntu

FROM python:3.7-slim  
RUN pip install --no-cache-dir matplotlib pandas

而后运行镜像脚本:

docker build -f Dockerfile.ubuntu -t 'ubuntu-mat' .

能够看到,编译好的镜像从原先的 60mb 暴涨到了 263mb。

liuyue:blog liuyue$ docker images  
REPOSITORY                  TAG                   IMAGE ID            CREATED              SIZE  
ubuntu-mat                  latest                401f0425ce63        About a minute ago   263MB

应用起来没有什么问题。

当初,咱们来试试 Alpine,看看速度和体积上有没有比 Ubuntu 更具劣势

编写 Dockerfile.alpine:

FROM python:3.7-alpine  
RUN pip install --no-cache-dir matplotlib pandas

编译镜像脚本

docker build -f Dockerfile.alpine -t 'alpine-mat' .

在编译过程中,咱们会发现报错了:

liuyue:blog liuyue$ docker build -f Dockerfile.alpine -t 'alpine-mat' .  
Sending build context to Docker daemon  112.1kB  
Step 1/2 : FROM python:3.7-alpine  
3.7-alpine: Pulling from library/python  
df20fa9351a1: Pull complete   
36b3adc4ff6f: Pull complete   
4db9de03f499: Pull complete   
cd38a04a61f4: Pull complete   
6bbb0c43b470: Pull complete   
Digest: sha256:d1375bf0b889822c603622dc137b24fb7064e6c1863de8cc4262b61901ce4390  
Status: Downloaded newer image for python:3.7-alpine  
 ---> 078114edb6be  
Step 2/2 : RUN pip install --no-cache-dir matplotlib pandas  
 ---> Running in 6d3c44420e5c  
Collecting matplotlib  
  Downloading matplotlib-3.3.1.tar.gz (38.8 MB)  
    ERROR: Command errored out with exit status 1:  
     command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] ='"'"'/tmp/pip-install-40p0g06u/matplotlib/setup.py'"'"'; __file__='"'"'/tmp/pip-install-40p0g06u/matplotlib/setup.py'"'"';f=getattr(tokenize,'"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"','"'"'\n'"'"');f.close();exec(compile(code, __file__,'"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-zk64hzam  
         cwd: /tmp/pip-install-40p0g06u/matplotlib/

这是怎么搞的?如果你认真看下面基于 Ubuntu 的构建,你会发现它下载三方库的安装包是 matplotlib-3.1.2-cp38-cp38-manylinux1_x86_64.whl,这是一个预编译的二进制安装包。而 Alpine 则只能下载源代码(matplotlib-3.1.2.tar.gz)的压缩包,这就是 Alpine 的致命问题:规范的 Linux 安装包在 Alpine Linux 上根本无法应用。

大多数 Linux 发行版都应用 GNU 版本的规范 C 库(glibc),简直所有基于 C 语言的脚本语言都须要这个库,包含 Python。但 Alpine Linux 应用的是 musl,那些二进制安装包是针对 glibc 编译的,因而 Alpine 禁用了 Linux 安装包反对。当初大多数 Python 包都在 PyPI 上蕴含了二进制安装包,大大放慢了安装时间。然而如果你应用的是 Alpine Linux,你须要编译你应用的每一个 Python 包中的所有 C 源码。

这也就意味着你须要本人弄清楚每一个零碎库的依赖性。当时编译好须要的依赖,从新改写 Dockerfile.alpine:

FROM python:3.7-alpine  
RUN apk --update add gcc build-base freetype-dev libpng-dev openblas-dev  
RUN pip install --no-cache-dir matplotlib pandas

再次编译:

docker build -f Dockerfile.alpine -t 'alpine-mat' .

通过了漫长的编译装置,大概半个小时左右,因为咱们都晓得从源码编译装置要远远慢于通过安装包装置,此时查看编译好的镜像:

REPOSITORY                  TAG                   IMAGE ID            CREATED              SIZE  
alpine-mat                  latest                601f0425ce63        About a minute ago   873MB

能够看到体积曾经变成 873mb 了,Alpine 最引以为傲的体积小轻便等个性也曾经依然如故。

尽管从实践上讲,Alpine 应用的 musl 内核与其余 Linux 发行版应用的 glibc 大多是兼容的,但在实际操作中,这种差别可能会造成各种问题。而当这些问题真的产生时,想解决它们就没那么简略了,比如说 Alpine 的线程默认堆栈容量较小,这会导致 Python 解体,同时也会影响 python 利用的运行速度。

结语:在本地环境,如果你只是想“玩一玩”,那么根底镜像抉择 Alpine 无可非议,然而如果你想要将你的 python 利用部署到生产环境时,特地是部署分布式系统须要屡次编译的场景下,抉择老牌的 Ubuntu 显然更加的理智。

原文转载自「刘悦的技术博客」https://v3u.cn/a_id_173

正文完
 0