这是Dockerfile的内容,我从高山映像更改为瘦身映像。我真的很想知道为什么要花这么长时间,我认为这与所有aps im更新和apt-get update中的安装有关。我可能正在重新安装可能不需要的软件包或不需要做的事情,有没有办法加快速度?
# 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"]
答案 0 :(得分:2)
这里有很多问题。首先,有一些您不需要的软件包:
您要安装python两次。您正在安装python
( Debian Python软件包),但是Docker python
映像具有自己的Python版本(在/ usr / local中),并且已经有dev标头。您不需要它,并且会导致混乱,因为您最终获得了两个版本的Python(https://pythonspeed.com/articles/importerror-docker/)。
musl-dev是不必要的。 Debian使用glibc,而不是musl。我怀疑这是来自Alpine的延期。
您正在安装编译器,通常是一堆C头文件,所有这些* -dev软件包。您完全有可能根本不需要!在Alpine上,您需要编译所有内容,因为Alpine无法使用普通的二进制轮子。 (https://pythonspeed.com/articles/alpine-docker-python/)由于您使用的是Debian,因此很可能您所有的操作都具有二进制轮子。您仍然需要自己的代码使用编译器,但是如果使用的是纯Python,则很有可能。
所以我的建议是:删除整个apt-get
行。没有它的机会很大。