You can try the python:{version}-alpine version. It's much smaller:

>> docker image ls |grep python
python    3.6-alpine     89.4 MB
python    3.6            689 MB
python    3.5            689 MB
python    3.5.2          687 MB
python    3.4            833 MB
python    2.7            676 MB

At time of writing it looks like the official image supports -alpine on all python versions.

https://hub.docker.com/_/python/

Answer from toast38coza on Stack Overflow
🌐
Docker Hub
hub.docker.com › _ › python
python - Official Image | Docker Hub
$ 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.
Discussions

Why is the python docker image so big (~750 MB)? - Stack Overflow
As a comparison, have a look at the 'pythonX.Y-slim' variants and the size of those. There isn't though an onbuild variant for the slim images. You could also look at my own Docker images for Python with bundled Apache/mod_wsgi support. These are trimmed and rely on additional packages being ... More on stackoverflow.com
🌐 stackoverflow.com
Alpine vs python-slim for deploying python data science stack?
Dev 2 is right. Alpine is slightly smaller as a base. The installed python packages will be many times the size of either one. The difference in a final image size between the two, I am guessing, would be tiny. Nobody publishes Alpine wheels, so you have to get stuff to compile yourself, using a standard library the app authors didn't plan for. Even if you can make it work, there may be inconsistency in the behavior from what the library authors intend/test/experience. Size has zero bearing on your computer bill here. Anecdotally, Alpine packages are usually slower (require more compute, not less). It is highly unlikely that the cost of storage of one image vs the other is more than a couple hours engineering work. More on reddit.com
🌐 r/docker
25
35
April 21, 2020
Breaking change introduced in python:3.8-slim
Hey all, I have a series of Docker containers running python:3.8-slim that I deploy from my local machine to AWS ECS. The last successful push was at July 22, 2024, 15:20:36, UTC. I went to deploy ... More on github.com
🌐 github.com
9
July 23, 2024
How to make lightweight docker image for python app with pipenv - Stack Overflow
Or can you offer your production Dockerfile ideas? ... 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. ... FROM python:3.7-slim WORKDIR /app # ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
ECR Public Gallery
gallery.ecr.aws › docker › library › python
Amazon ECR Public Gallery - Docker/library/python
Unless you are working in an environment where only the python image will be deployed and you have space constraints, we highly recommend using the default image of this repository. This image is based on the popular Alpine Linux project, available in the alpine official image. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.
Top answer
1 of 6
22

You can try the python:{version}-alpine version. It's much smaller:

>> docker image ls |grep python
python    3.6-alpine     89.4 MB
python    3.6            689 MB
python    3.5            689 MB
python    3.5.2          687 MB
python    3.4            833 MB
python    2.7            676 MB

At time of writing it looks like the official image supports -alpine on all python versions.

https://hub.docker.com/_/python/

2 of 6
13

Alpine Linux is a very lean distro avaliable for Docker. Without Python, it's around 5MB. With Python I'm getting images between 60 and 120 MB. The following Dockerfile yields a 110 MB image.

FROM alpine:3.4

RUN apk --update add \
      build-base python-dev \
      ca-certificates python \
      ttf-droid \
      py-pip \
      py-jinja2 \
      py-twisted \
      py-dateutil \
      py-tz \
      py-requests \
      py-pillow \
      py-rrd && \
    pip install --upgrade arrow \
                          pymongo \
                          websocket-client \
                          XlsxWriter && \
    apk del build-base python-dev && \
    rm -rf /var/cache/apk/* && \
    adduser -D -u 1001 noroot

USER noroot

CMD ["/bin/sh"]

Also, it's very well mantained.


A word of warning, though. Alpine uses musl libc instead of glibc, and some Python modules rely on glibc, but this usually isn't a problem.

A bigger issue is, that because of this, manylinux wheels are not avaliable for Alpine, and therefore the modules need to be compiled upon installation (pip install). In some cases this can make a difference in build time between 20 seconds on Debian and 9 minutes or more on Alpine. The grpcio-module is notorious for that; it takes forever to compile.

There is a (somewhat unreliable) workaround where you tell Python that it is manylinux compatible.

Find elsewhere
🌐
Reddit
reddit.com › r/docker › alpine vs python-slim for deploying python data science stack?
r/docker on Reddit: Alpine vs python-slim for deploying python data science stack?
April 21, 2020 -

The problem

I work on a small data science team in healthcare. We've been using Docker to deploy our data science models and ETL pipelines for some time now. Our team's senior engineer (dev1) has pushed us to use alpine due to its perceived size and security benefits.

We've recently received feedback from an experienced python/docker developer (dev2) that we should use python-slim instead of alpine. Dev2 says that alpine python images tend to be bigger and slower. Dev2 points out that wheels aren't built for alpine which means everything has to be built from scratch. Dev2 suggests that if you are concerned about security python-slim should be just as good as alpine. Dev2 points to the infamous Using Alpine can make Python Docker builds 50× slower as definitive proof that alpine should not be used for python docker images.

Dev1 insists that this blog post was contrived. Dev1 says that the reason for the additional slowness is that they haven't built optimized muslc wheels for their benchmarks which causes the performance loss. Dev1 has spent a lot of time trying to build custom alpine images with all of the libraries we need. Dev1 has not produced tangible results after many months trying to work through the technical challenges.

The details

  1. Because we're deploying mostly sklearn models, and sklearn models are typically distributed as pickle files, we need to pin the versions of our Python libraries (NumPy, scipy, sklearn, pandas).

  2. Data is typically ingested from an MsSQL server so we need to also package closed source client libraries in our containers, we'd like to figure out a way to publish our containers publicly so that we can open source our work but we haven't yet figured out a way to do this and include client libraries for the various closed source database drivers.

  3. Each docker image needs to come in matching pairs. One image that is for development that has jupyter in it so that data scientists can create models and a matching production image that only has the minimum set of libraries for deploying these models into production.

  4. We do have limited computing resources so the size of the image does matter, but probably not as much as security.

The question

Who's right, dev1 or dev2? What's the best option for us given the requirements outlined? Thanks for your help!

🌐
Medium
medium.com › @faruk13 › alpine-slim-bullseye-bookworm-noble-differences-in-docker-images-explained-d9aa6efa23ec
Alpine, Slim, Bullseye, Bookworm, Noble — Different Docker Images Explained | by Umar Faruk | Medium
September 26, 2025 - Docker images are in format <image>:<tag> - “image” can be replaced by python, ubuntu, nodejs - “tag” includes their versions, codenames and any other distinct nomenclatures, eg 3.10, 3.10-slim, 3.10-buster-slim(for python), 21-bullseye, 21-slim(for node)
🌐
Docker Hub
hub.docker.com › r › arm64v8 › python
arm64v8/python - Docker Image
$ docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp arm64v8/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.
🌐
GitHub
github.com › docker-library › python › issues › 944
Breaking change introduced in python:3.8-slim · Issue #944 · docker-library/python
July 23, 2024 - I went to deploy one of the containers this morning - having made exactly 0 changes to the Dockerfile, the docker-compose.yml, the requirements.txt, or any other config file, and it failed out with the following message: ------ > [container-name 4/5] RUN pip install pip --upgrade pip install -r requirements.txt: #0 2.035 Requirement already satisfied: pip in /usr/local/lib/python3.8/site-packages (23.0.1) #0 2.295 Collecting pip #0 2.467 Downloading pip-24.1.2-py3-none-any.whl (1.8 MB) #0 2.701 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Author   alecbw
🌐
Python⇒Speed
pythonspeed.com › articles › base-image-python-docker-images
The best Docker base image for your Python application (February 2026)
February 4, 2026 - Debian “Trixie” 13, with a slim variant. This lacks the common packages’ layers, and so the image itself is much smaller, but if you use many other “official” Docker images the overall 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.
🌐
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 - FROM python:3.11-slim as build ENV PIP_DEFAULT_TIMEOUT=100 \ # Allow statements and log messages to immediately appear PYTHONUNBUFFERED=1 \ # disable a pip version check to reduce run-time & log-spam PIP_DISABLE_PIP_VERSION_CHECK=1 \ # cache is useless in docker image, so disable to reduce image size PIP_NO_CACHE_DIR=1 \ POETRY_VERSION=1.3.2 WORKDIR /app COPY pyproject.toml poetry.lock ./ RUN pip install "poetry==$POETRY_VERSION" \ && poetry install --no-root --no-ansi --no-interaction \ && poetry export -f requirements.txt -o requirements.txt ### Final stage FROM python:3.11-slim as final WORKDIR /app COPY --from=build /app/requirements.txt .
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 › 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