有几件事你可以做,以使你的Docker镜像更小。
python:3-slim Docker image as a base. The -slim images do not include packages needed for compiling software.
python:3-slim tag will point to different versions of python at different points in time.gfortran, libopenblas-dev, and liblapack-dev. Those packages are necessary for building numpy/scipy, but if you install the wheel files, which are pre-compiled, you do not need to compile any code.--no-cache-dir in pip install to disable the cache. If you do not include this, then pip's cache counts toward the Docker image size.mysqlclient, so you will have to compile it. You can install build dependencies, install the package, then remove build dependencies in a single RUN instruction. Keep in mind that libmariadb3 is a runtime dependency of this package.下面是一个实现上述建议的Docker文件。它使一个Docker镜像达到354MB大。
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
