Docker 构建图像需要很长时间

Docker image taking long to build

这是 Dockerfile 的内容,我从 alpine 镜像更改为 slim-buster 镜像。我真的很难理解为什么要花这么长时间,我认为这与我在 apt-get 更新中更新和安装的所有 ap 有关。我可能正在重新安装我不需要的软件包或做一些我不需要的事情,有什么方法可以加快速度吗?

# pull official base image
FROM python:3.8-slim-buster

# set work directory
WORKDIR /opt/workspace

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Need this crap for wkhtml old install because any version after this doesn't work with charts and javascript
RUN echo "deb http://security.debian.org/debian-security jessie/updates main" >> /etc/apt/sources.list

# Pillow and Psycopg Dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        wget \ 
        libpq-dev \
        libpng-dev \
        libjpeg-dev \
        python-dev \
        postgresql-client \
        python3-pip \
        python3-setuptools \
        python3-wheel \
        python3-cffi \
        libssl1.0.0 \
        libpng12-0 \
        xfonts-base \
        xfonts-75dpi \
        libcairo2 \
        libpango-1.0-0 \
        libpangocairo-1.0-0 \
        libgdk-pixbuf2.0-0 \
        libffi-dev \
        shared-mime-info\
        gcc \
        musl-dev \
        python3-dev \
        tk-dev \
        uuid-dev \ 
    && rm -rf /var/lib/apt/lists/*

# fetch wait for it script
RUN wget -q -O /usr/bin/wait-for-it https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh && \
    chmod +x /usr/bin/wait-for-it

RUN pip install psycopg2

# bunch of wkhtmltopdf shit only works with charts on this image.
RUN wget http://archive.ubuntu.com/ubuntu/pool/main/libj/libjpeg-turbo/libjpeg-turbo8_2.0.3-0ubuntu1_amd64.deb
RUN dpkg -i libjpeg-turbo8_2.0.3-0ubuntu1_amd64.deb

RUN wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.2.1/wkhtmltox-0.12.2.1_linux-trusty-amd64.deb
RUN dpkg -i wkhtmltox-0.12.2.1_linux-trusty-amd64.deb

# Install Dependencies
COPY requirements.txt /opt/workspace/requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# copy entrypoint.sh
COPY ./entrypoint.sh /opt/workspace/entrypoint.sh

# copy project
COPY . /opt/workspace

# run entrypoint.sh
ENTRYPOINT ["/opt/workspace/entrypoint.sh"]

编辑: 收到答复后,我已将 dockerfile 更新为以下内容。请记住,我需要使用 apt-get 安装的唯一软件包是 wkhtmltopdf 需要 运行 的软件包! 运行现在速度快多了,而且工作得更好。

FROM python:3.8-slim-buster

# set work directory
WORKDIR /opt/workspace

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBIAN_FRONTEND=noninteractive

# Need this crap for wkhtml old install
RUN echo "deb http://security.debian.org/debian-security jessie/updates main" >> /etc/apt/sources.list

RUN apt-get update && apt-get install -y --no-install-recommends \
        wget \
        fontconfig \
        xfonts-base \
        xfonts-75dpi \
        libssl1.0.0 \
        libpq-dev \
        libpng-dev \
        libjpeg-dev \
        libffi-dev \
        libpng12-0 \
        libxext6 \
        libx11-6 \
        libxrender1 \ 
        gcc \
    && rm -rf /var/lib/apt/lists/*

# fetch wait for it script
RUN wget -q -O /usr/bin/wait-for-it https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh && \
    chmod +x /usr/bin/wait-for-it

# bunch of wkhtmltopdf shit only works with charts on this image.
RUN wget http://archive.ubuntu.com/ubuntu/pool/main/libj/libjpeg-turbo/libjpeg-turbo8_2.0.3-0ubuntu1_amd64.deb
RUN dpkg -i libjpeg-turbo8_2.0.3-0ubuntu1_amd64.deb

RUN wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.2.1/wkhtmltox-0.12.2.1_linux-trusty-amd64.deb
RUN dpkg -i wkhtmltox-0.12.2.1_linux-trusty-amd64.deb

# Install Dependencies
COPY requirements.txt /opt/workspace/requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# copy entrypoint.sh
COPY ./entrypoint.sh /opt/workspace/entrypoint.sh

# copy project
COPY . /opt/workspace

# run entrypoint.sh
ENTRYPOINT ["/opt/workspace/entrypoint.sh"]

这里有很多问题。首先,有些包你不需要:

  1. 您正在安装 python 两次。您正在安装 pythonDebian Python 软件包,但是 Docker python 映像有自己的 [=38 版本=](在 /usr/local 中),dev headers 已经存在。你不需要这个,它会导致混淆,因为你最终会得到两个版本的 Python (https://pythonspeed.com/articles/importerror-docker/).

  2. musl-dev 是不必要的。 Debian 使用 glibc,而不是 musl。我怀疑这是 Alpine 遗留下来的。

  3. 您正在安装一个编译器,通常是一大堆 C headers,所有那些 *-dev 包。您很可能根本不需要!在 Alpine 上,您需要编译所有内容,因为 Alpine 不能使用普通的二进制轮子。 (https://pythonspeed.com/articles/alpine-docker-python/) 由于您使用的是 Debian,很可能您所有的依赖项都有二进制轮子。您仍然需要一个编译器来处理您自己的代码,但如果它是纯的 Python 很可能不需要。

所以我的建议是:删除整个 apt-get 行。很有可能它会在没有它的情况下工作。