The problem comes when you need things like ciso8601, or some libraries, requiring build process. Build tools are not "incorporated" into the both slim and alpine variants, for low-size footprint.

So to install deps, you'll have to:

  • Install build tools
  • Deploy dependencies from Pipfile.lock system-wide
  • Uninstall build tools and clean caches

And do that 3 actions inside a single RUN layer, like following:

FROM python:3.7-slim

WORKDIR /app

# both files are explicitly required!
COPY Pipfile Pipfile.lock ./

RUN pip install pipenv && \
  apt-get update && \
  apt-get install -y --no-install-recommends gcc python3-dev libssl-dev && \
  pipenv install --deploy --system && \
  apt-get remove -y gcc python3-dev libssl-dev && \
  apt-get autoremove -y && \
  pip uninstall pipenv -y

COPY app ./

CMD ["python", "app.py"]
  • Manipulating build system would cost you around 300MiB and some extra time
  • Uninstalling pipenv would save you another 20MiB (which is 10% of resulting size).
  • Separating RUN commands would not delete data from layers, and would result in ~500MiB image. That's docker specifics.

So that would result in perfectly working ~200MiB sized image, which is

  • 5 times less than original python:3.7, (that is >1.0GiB)
  • Has no alpine incompabilities (these are typically tied to glibc replacement)

At the time, we're fine with slim (debian buster) build variants, preferring slim over alpine (for most compatibility). If you're really up to further size optimization, I'd recommend you to take a look at some excellent builds of these guys:

  • Alpine Python
  • 12.7MiB MariaDB
Answer from xakepp35 on Stack Overflow
🌐
Docker Hub
hub.docker.com › _ › python
python - Official Image | Docker Hub
For many simple, single file projects, you may find it inconvenient to write a complete Dockerfile. In such cases, you can run a Python script by using the Python Docker image directly: $ docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3 python your-daemon-or-script.py Copy ... $ docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:2 python your-daemon-or-script.py Copy · In the non-slim variants there will be an additional (distro-provided) python executable at /usr/bin/python (and/or /usr/bin/python3) while the desired image-provided /usr/local/bin/python is the default choice in the $PATH.
🌐
KDnuggets
kdnuggets.com › how-to-create-minimal-docker-images-for-python-applications
How To Create Minimal Docker Images for Python Applications - KDnuggets
June 25, 2024 - # Use the official lightweight Python 3.11-slim image FROM python:3.11-slim # Set the working directory WORKDIR /app # Install dependencies COPY requirements.txt requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Copy the ...
Top answer
1 of 5
21

The problem comes when you need things like ciso8601, or some libraries, requiring build process. Build tools are not "incorporated" into the both slim and alpine variants, for low-size footprint.

So to install deps, you'll have to:

  • Install build tools
  • Deploy dependencies from Pipfile.lock system-wide
  • Uninstall build tools and clean caches

And do that 3 actions inside a single RUN layer, like following:

FROM python:3.7-slim

WORKDIR /app

# both files are explicitly required!
COPY Pipfile Pipfile.lock ./

RUN pip install pipenv && \
  apt-get update && \
  apt-get install -y --no-install-recommends gcc python3-dev libssl-dev && \
  pipenv install --deploy --system && \
  apt-get remove -y gcc python3-dev libssl-dev && \
  apt-get autoremove -y && \
  pip uninstall pipenv -y

COPY app ./

CMD ["python", "app.py"]
  • Manipulating build system would cost you around 300MiB and some extra time
  • Uninstalling pipenv would save you another 20MiB (which is 10% of resulting size).
  • Separating RUN commands would not delete data from layers, and would result in ~500MiB image. That's docker specifics.

So that would result in perfectly working ~200MiB sized image, which is

  • 5 times less than original python:3.7, (that is >1.0GiB)
  • Has no alpine incompabilities (these are typically tied to glibc replacement)

At the time, we're fine with slim (debian buster) build variants, preferring slim over alpine (for most compatibility). If you're really up to further size optimization, I'd recommend you to take a look at some excellent builds of these guys:

  • Alpine Python
  • 12.7MiB MariaDB
2 of 5
8

How about,

FROM python:3.7-alpine

WORKDIR /myapp

COPY Pipfile* ./

RUN pip install --no-cache-dir pipenv && \
    pipenv install --system --deploy --clear

COPY src .
CMD ["python3", "app.py"]
  1. It utilises the smaller Alpine version.
  2. You won't have any unnecessary cache files left over using --no-cache-dir option for pip and --clear option for pipenv.
  3. You also deploy outside of venv.

You can also add && pip uninstall pipenv -y after pipenv install --system --deploy --clear in the same RUN command to eliminate space taken by pipenv if that extra image size bothers you.

🌐
GitHub
github.com › tiangolo › uvicorn-gunicorn-docker › blob › master › docker-images › python3.11-slim.dockerfile
uvicorn-gunicorn-docker/docker-images/python3.11-slim.dockerfile at master · tiangolo/uvicorn-gunicorn-docker
FROM python:3.11-slim · · ENV PYTHONDONTWRITEBYTECODE=1 · ENV PYTHONUNBUFFERED=1 · · LABEL maintainer="Sebastian Ramirez <tiangolo@gmail.com>" · COPY requirements.txt /tmp/requirements.txt · RUN pip install --no-cache-dir -r /tmp/requirements.txt ·
Author   tiangolo
Find elsewhere
🌐
GitHub
github.com › eht16 › python3-docker
GitHub - eht16/python3-docker: A slim Python 3 image for Docker · GitHub
A slim Python 3 image for Docker. Contribute to eht16/python3-docker development by creating an account on GitHub.
Starred by 9 users
Forked by 10 users
Languages   Dockerfile 62.0% | Python 38.0%
🌐
Python⇒Speed
pythonspeed.com › articles › base-image-python-docker-images
The best Docker base image for your Python application (February 2026)
February 4, 2026 - This lacks the common packages’ ... disk usage will be somewhat higher. For comparison purposes, the download size of python:3.14-slim-trixie is 41MB, and python:3.14-alpine3.23 is 17MB....
🌐
Karim's Blog
elatov.github.io › 2022 › 03 › using-docker-slim-to-reduce-the-size-a-docker-image
Using docker slim to reduce the size of a docker image | Karim's Blog
March 27, 2022 - So at this point you can check for running docker processes and exec into the one docker-slim is using: > docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7e0d5c7f15a7 library/python-slim:0.0.37 "/opt/dockerslim/bin…" About a minute ago Up About a minute 0.0.0.0:55015->65501/tcp, 0.0.0.0:55014->65502/tcp dockerslimk_17458_20220317163527 > docker exec -it 7e0d5c7f15a7 /bin/bash root@7e0d5c7f15a7:/app# ls python requirements.txt root@7e0d5c7f15a7:/app# exit
🌐
Medium
medium.com › vantageai › how-to-make-your-python-docker-images-secure-fast-small-b3a6870373a0
How to make your Python Docker images secure, fast & small | by Björn van Dijkman | VantageAI | Medium
February 20, 2023 - Multistage builds can be utilized to exclude Poetry from the final Docker image, because it enables the creation of multiple images from a single Dockerfile. Instead of installing the dependencies directly using Poetry, Poetry can also export the necessary dependencies to a requirements.txt file during the build stage. This file can be copied to the final stage, and be used by the pip to install the dependencies. FROM python:3.11-slim as build .
🌐
GitHub
github.com › slimtoolkit › slim
GitHub - slimtoolkit/slim: Slim(toolkit): Don't change anything in your container image and minify it by up to 30x (and for compiled languages even more) making it secure too! (free and open source) · GitHub
If you don't provide a custom tag ... container image name (slim-tmp-fat-image.<pid_of_slim>.<current_timestamp>.slim). Take a look at this python examples to see how it's using the --dockerfile flag....
Starred by 23.1K users
Forked by 826 users
Languages   Go
🌐
GitHub
github.com › slimtoolkit › examples
GitHub - slimtoolkit/examples: Minified application examples with different languages and stacks for DockerSlim
# All example types make # run example (semi-automatic) make test-e2e # run the acceptance test suite make clean # remove all artifacts created by this example # 'CLI application' example type make fat-build # build the "fat" version of the image make fat-run-interactive # run the CLI app using the "fat" image make slim-build # turn the "fat" image into a "slim" one make slim-run-interactive # run the CLI app using the "slim" image make slim-build-from-dockerfile # build the "slim" version of the image using the "fat" Dockerfile # 'Web Service' example type make fat-build # build the "fat" ver
Starred by 193 users
Forked by 23 users
Languages   Shell 31.6% | Ruby 30.5% | Makefile 11.1% | Elixir 6.3% | Dockerfile 5.2% | Java 4.7% | Shell 31.6% | Ruby 30.5% | Makefile 11.1% | Elixir 6.3% | Dockerfile 5.2% | Java 4.7%
🌐
GitHub
github.com › nginx › unit › issues › 1352
Docker: for python based images, use the -slim version · Issue #1352 · nginx/unit
July 5, 2024 - After a conversation with @MichaelMcAleer and our docker initiatives, he suggested we use the -slim variant python images as base when creating the unit-python dockerfiles. Main reason is the slim image has a lot fewer packages and there...
Author   javorszky
🌐
ZetCode
zetcode.com › python › docker
Python Docker - using Docker for Python
July 8, 2023 - In the next example, we run a simple Flask application in a Docker container. ... #!/usr/bin/python from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello there!' This is a simple Flask application with one home route. ... FROM python:slim COPY app.py /app/ WORKDIR /app RUN pip install flask RUN export FLASK_APP=app.py EXPOSE 5000 CMD ["/usr/local/bin/flask", "run", "--host", "0.0.0.0"] This is the Dockerfile.