简体   繁体   English

如何减小包含 numpy、scipy 和 scikit-learn 的最终 docker 镜像大小

[英]How to reduce final docker image size containing numpy, scipy, and scikit-learn

I have posted this question over at docker hub as well as I am not sure which community would have more of a response to my question.我已经在 docker hub 上发布了这个问题,但我不确定哪个社区会对我的问题有更多的回应。 I am new to creating docker images and have put together a Dockerfile that creates an image that works but the final image size is 600MB+ and would like somebody that is more advance than I to advise if there is anything I can do to reduce this.我是创建 docker 镜像的新手,并且已经组合了一个 Dockerfile 来创建一个有效的镜像,但最终镜像大小为 600MB+,并且希望有人比我更先进,如果我能做些什么来减少这种情况。 I have read many blogs about various strategies to do so and have gone the Python virtual environment route.我已经阅读了许多关于这样做的各种策略的博客,并且已经走上了 Python 虚拟环境路线。 I am really concerned about build times as I will not be building often but would like to see the final image size be a bit leaner than what it is.我真的很担心构建时间,因为我不会经常构建,但希望看到最终的图像尺寸比实际尺寸更精简。

What I am doing is building an image with a python application, fava which is a web gui front-end to the accounting program beancount.我正在做的是使用 python 应用程序构建图像,fava 是会计程序 beancount 的 Web gui 前端。 These two python applications alone are easy enough and the fava team even provide a Dockerfile based on alpine to build a light image.仅这两个 python 应用程序就足够简单了,fava 团队甚至提供了一个基于 alpine 的 Dockerfile 来构建轻量级镜像。 However, the issue I have is I want to extend this by including an extension to this python application (smart_importer) which provides some machine learning features that will automate aspects of the transaction importing process.但是,我遇到的问题是我想通过包含对此 python 应用程序 (smart_importer) 的扩展来扩展它,该应用程序提供了一些机器学习功能,可以自动执行事务导入过程的各个方面。 This extension depends on numpy, scipy, and scikit-learn and this is where the extra weight comes from.这个扩展依赖于 numpy、scipy 和 scikit-learn,这就是额外权重的来源。 I originally tried to extended the alpine approach that the fava team had but installing scipy on alpine fails horribly which I cannot resolve.我最初尝试扩展 fava 团队采用的 alpine 方法,但在 alpine 上安装 scipy 失败了,我无法解决。 Using python slim I can build a fairly small (<200mb) final image with just fava and beancount...but as I said this balloons with the introduction of the needed dependencies of smart_importer.使用 python slim 我可以只用 fava 和 beancount 构建一个相当小的(<200mb)最终图像......

Here is the Dockerfile I have currently, are there any changes I can easily make to get the final image size down that I am not seeing?这是我目前拥有的 Dockerfile,是否可以轻松进行任何更改以减小我没有看到的最终图像大小? Greatly appreciate any pointers one might have.非常感谢您可能有的任何指示。

FROM python:slim AS base

FROM base as builder

RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

#WORKDIR /install
COPY ./library-dependencies.txt /tmp/library-dependencies.txt
COPY ./requirements.txt /tmp/requirements.txt
#ENV PATH="/install:${PATH}"

RUN buildDeps='build-essential gcc gfortran python3-dev' \
    && apt-get update \
    && apt-get install -y $buildDeps --no-install-recommends \
    && cat /tmp/library-dependencies.txt | egrep -v "^\s*(#|$)" | xargs apt-get install -y \
    && pip3 install --upgrade pip \
    && CFLAGS="-g0 -Wl,--strip-all -I/usr/include:/usr/local/include -L/usr/lib:/usr/local/lib" \
        pip3 install \
#       --prefix="/install" \
        --no-cache-dir \
        --compile \
        --global-option=build_ext \
        --global-option="-j 6" \
        -r /tmp/requirements.txt \
    && apt-get purge -y --auto-remove $buildDeps \
    && rm -rf /var/lib/apt/lists/* \
    && rm -r \
    /tmp/requirements.txt \
        /tmp/library-dependencies.txt

FROM base
COPY --from=builder /opt/venv /opt/venv
COPY ./library-dependencies.txt /tmp/library-dependencies.txt

RUN apt-get update \
    && cat /tmp/library-dependencies.txt | egrep -v "^\s*(#|$)" | xargs apt-get install -y \
    && apt-get install -y libgomp1 --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

ENV PATH="/opt/venv/bin:$PATH"
ENV BEANCOUNT_FILE ""
ENV FAVA_OPTIONS ""
EXPOSE 5000
CMD fava --host 0.0.0.0 $FAVA_OPTIONS $BEANCOUNT_FILE

requirements.txt要求.txt

# numeric packages needed for smart_importer
Cython==0.28.5
numpy==1.15.1
scipy==1.1.0
scikit-learn

#fava
fava
smart_importer

library-dependencies.txt库-dependencies.txt

libopenblas-dev
liblapack-dev

libxml2-dev
libxslt1-dev
zlib1g-dev

使用 apk 安装 py3-scipy 包是否有效?

I would recommend Python on Alpine Linux.我会在 Alpine Linux 上推荐 Python。 Any Dockerfile changes you make are not going to get your image nearly as small as Alpine.您所做的任何 Dockerfile 更改都不会使您的映像几乎与 Alpine 一样小。

This looks like a good start: https://github.com/jfloff/alpine-python这看起来是一个好的开始: https : //github.com/jfoff/alpine-python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM