最小的 SciPy Dockerfile [英] Minimal SciPy Dockerfile

查看:21
本文介绍了最小的 SciPy Dockerfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的 Dockerfile,省略了应用代码:

I have a Dockerfile like the following, app code is omitted:

FROM python:3
# Binary dependencies
RUN apt update && apt install -y gfortran libopenblas-dev liblapack-dev
# Wanted Python packages
RUN python3 -m pip install mysqlclient numpy scipy pandas matplotlib

它工作正常,但生成的图像大小为 1.75 GB(而代码约为 50 MB).这么大的体积,怎么才能减少?

It works fine but produces an image of 1.75 GB in size (while code is about 50 MB). How can I reduce such huge volume??

我也尝试过使用 Alpine Linux,就像这样:

I also tried to use Alpine Linux, like this:

FROM python:3-alpine
# Binary dependencies for numpy & scipy; though second one doesn't work anyway
RUN apk add --no-cache --virtual build-dependencies \
    gfortran gcc g++ libstdc++ \
    musl-dev lapack-dev freetype-dev python3-dev
# For mysqlclient
RUN apk --no-cache add mariadb-dev
# Wanted Python packages
RUN python3 -m pip install mysqlclient numpy scipy pandas matplotlib

但是 Alpine 会导致许多不同的奇怪错误.来自上层代码的错误:

But Alpine leads to many different strange errors. Error from the upper code:

File "scipy/odr/setup.py", line 28, in configuration
    blas_info['define_macros'].extend(numpy_nodepr_api['define_macros'])
KeyError: 'define_macros'

那么,如何使用上述包获得最小可能(或至少更小)的 Python 3 映像?

So, how one can get minimal possible (or at least just smaller) image of Python 3 with mentioned packages?

推荐答案

您可以通过多种方式缩小 Docker 映像.

There are several things you can do to make your Docker image smaller.

  1. 使用 python:3-slim Docker 镜像作为基础.-slim 镜像不包含编译软件所需的包.
    • 固定 Python 版本,比方说 3.8.有些包还没有用于 python 3.9 的轮文件,所以你可能需要编译它们.一般来说,最好使用更具体的标签,因为 python:3-slim 标签会在不同的时间点指向不同版本的 python.
  1. Use the python:3-slim Docker image as a base. The -slim images do not include packages needed for compiling software.
    • Pin the Python version, let's say to 3.8. Some packages do not have wheel files for python 3.9 yet, so you might have to compile them. It is good practice, in general, to use a more specific tag because the python:3-slim tag will point to different versions of python at different points in time.

这是一个实现上述建议的 Dockerfile.它使 Docker 映像大 354 MB.

Here is a Dockerfile that implements the suggestions above. It makes a Docker image 354 MB large.

FROM python:3.8-slim

# Install mysqlclient (must be compiled).
RUN apt-get update -qq \
    && apt-get install --no-install-recommends --yes \
        build-essential \
        default-libmysqlclient-dev \
        # Necessary for mysqlclient runtime. Do not remove.
        libmariadb3 \
    && rm -rf /var/lib/apt/lists/* \
    && python3 -m pip install --no-cache-dir mysqlclient \
    && apt-get autoremove --purge --yes \
        build-essential \
        default-libmysqlclient-dev

# Install packages that do not require compilation.
RUN python3 -m pip install --no-cache-dir \
      numpy scipy pandas matplotlib

使用 alpine linux 是个好主意,但是 alpine 使用 muslc 而不是 glibc,因此它与大多数 pip wheel 不兼容.结果是你必须编译 numpy/scipy.

Using alpine linux was a good idea, but alpine uses muslc instead of glibc, so it is not compatible with most pip wheels. The result is that you would have to compile numpy/scipy.

这篇关于最小的 SciPy Dockerfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆