Docker Apt Install for Python+UV

A Proposal for Hynek's Docker Recipe

While reviewing Hynek’s awesome “Production-ready Python Docker Containers with uv” article, I made some changes to the final container. Here’s my proposal for changes.

Towards the bottom of the original article is a chunk with the prefix:

# Note how the runtime dependencies differ from build-time ones.
# Notably, there is no uv either!

I’ve changed my version of it to:

# Note how the runtime dependencies differ from build-time ones.
# Notably, there is no uv either!
RUN <<EOT
DEBIAN_FRONTEND=noninteractive
apt-get update -qy
apt-get install -qyy --no-install-recommends --no-install-suggests \
    python3.12 \
    libpython3.12 \
    libpcre3 \
    libxml2

apt-get clean
apt-get autoremove --yes
rm -rf /var/lib/{apt,dpkg,cache,log}/
EOT

The “DEBIAN_FRONTEND” makes sure that the apt update doesn’t ask any questions using an interactive UI, for things like “What services should I restart”. This change probably should be in the other apt-get update sections of the recipe as well.

I changed the -o APT options to just directly using the apt provided options, but this is largely a stylistic choice (I find this less ugly).

I added the apt-get autoremove, because sometimes there are packages that are installed that may no longer be necessary, say if the update pulls in a new version of a package that no longer has an upstream dependency, or if you were to add a apt remove line. This produces the most compact resulting container image.

I added a few /var/lib directories that can be cleaned up.

I removed the removal of the tmp directories because an ls -l showed that they were not necessary, nothing was being written into the tmp directories, during the install. However having this is innocuous, it just made me wonder what Hynek knew that I didn’t.

Python  Docker  uv