使用 python 和 R 最小化 docker 映像



我有以下Dockerfile:

FROM ubuntu:latest
RUN apt-get update 
&& apt-get install -y python3-pip python3-dev 
&& cd /usr/local/bin 
&& ln -s /usr/bin/python3 python 
&& pip3 install --upgrade pip
# Setup the Python's configs
RUN pip install --upgrade pip && 
pip install --no-cache-dir matplotlib==3.0.2 pandas==0.23.4 numpy==1.16.3 && 
pip install --no-cache-dir pybase64 && 
pip install --no-cache-dir scipy && 
pip install --no-cache-dir dask[complete] && 
pip install --no-cache-dir dash==1.6.1 dash-core-components==1.5.1 dash-bootstrap-components==0.7.1 dash-html-components==1.0.2 dash-table==4.5.1 dash-daq==0.2.2 && 
pip install --no-cache-dir plotly && 
pip install --no-cache-dir adjustText && 
pip install --no-cache-dir networkx && 
pip install --no-cache-dir scikit-learn && 
pip install --no-cache-dir tzlocal
# Setup the R configs
RUN apt-get update
RUN apt-get install -y software-properties-common
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
RUN add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/'
RUN apt update
ENV DEBIAN_FRONTEND=noninteractive 
RUN apt install -y r-base
RUN pip install rpy2==2.9.4
RUN apt-get -y install libxml2 libxml2-dev libcurl4-gnutls-dev libssl-dev
RUN echo "r <- getOption('repos'); r['CRAN'] <- 'https://cran.r-project.org'; options(repos = r);" > ~/.Rprofile
RUN Rscript -e "install.packages('BiocManager')"
RUN Rscript -e "BiocManager::install('ggplot2')"
RUN Rscript -e "BiocManager::install('DESeq2')"
RUN Rscript -e "BiocManager::install('RColorBrewer')"
RUN Rscript -e "BiocManager::install('ggrepel')"
RUN Rscript -e "BiocManager::install('factoextra')"
RUN Rscript -e "BiocManager::install('FactoMineR')"
RUN Rscript -e "BiocManager::install('apeglm')"
WORKDIR /
# Copy all the necessary files of the app to the container
COPY ./ ./
# Install the slider-input component
WORKDIR /slider_input
RUN pip install --no-cache-dir slider_input-0.0.1.tar.gz
WORKDIR /
EXPOSE 8050
# Launch the app
CMD ["python", "./app.py"]

它用于运行使用 R 命令的破折号应用程序,并且工作正常。 问题是图像的大小。 我想尽可能减小图像的大小,但由于python和R的组合,我尝试的所有方法都没有成功。

您知道如何最小化此图像并提供相同的功能吗?

使用 docker-slim 来最小化和保护 docker 映像。docker-slim 将分析您的 docker 映像并丢弃您不需要的内容。

它已被用于Node.js,Python,Ruby,Java,Golang,Rust,Elixir和PHP(某些应用程序类型(,运行在Ubuntu,Debian,CentOS,Alpine甚至Distroless上。

docker-slim 已做好生产准备,但请考虑在将容器部署到生产环境之前对其进行测试。将 docker 映像缩小多达 30 倍,同时使其安全!

多阶段构建将允许您从最终图像中省略编译器工具链、标头等,仅包含生成的代码。

Python 的三部分教程专门从这里开始:https://pythonspeed.com/articles/smaller-python-docker-images/

还有通用的 Docker 文档:https://docs.docker.com/develop/develop-images/multistage-build/

最新更新