Skip to content

Instantly share code, notes, and snippets.

@caniko
Last active November 25, 2023 15:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caniko/af55570f9d855a3c88458b739f11ac93 to your computer and use it in GitHub Desktop.
Save caniko/af55570f9d855a3c88458b739f11ac93 to your computer and use it in GitHub Desktop.
My way of integrating poetry to docker container

Poetry docker integration

I landed on using archlinux as my base image. I use chaoti-aur to install the latest poetry from git, I can choose the exact Python version I want thanks to pyenv (made it conveniant by adding this as an env var, PYTHON_VERSION).

You can install your poetry package after copying it to your WORKDIR by poetry install --no-dev. Flush the poetry cache by poetry cache clear --all; no neat way to do this yet #887.

Advantages of archlinux + poetry in containarized production

  • Installing poetry through pacman installs the poetry as root. Poetry installer is rootless by default, which is not production-friendly; it is now! Don't forget to switch to a user wihtout write-access to root! Otherwise, you won't benefit from this safety feature.
  • Separation of Python used for production code, and poetry code. Poetry will run on the latest stable version of Python, while production code will run on whatever version it was designed for.
  • Less maintanance of dockerfile as the heavylifting for poetry compability is done by archlinux.

Stable release alternatives

Just replace pacman -Sy --noconfirm python-cleo-git python-poetry-git pyenv with pacman -Sy --noconfirm python-poetry pyenv.

FROM archlinux AS builder
RUN mkdir /etc/gnupg && \
echo "honor-http-proxy" > /etc/gnupg/dirmngr.conf && \
echo "honor-http-proxy" > /etc/pacman.d/gnupg/dirmngr.conf && \
pacman-key --init && \
pacman -Syu --noconfirm && \
pacman -Sy --noconfirm pyenv base-devel openssl zlib xz
ENV PYTHON_VERSION=3.10.3
RUN pyenv install $PYTHON_VERSION
FROM archlinux
RUN mkdir /etc/gnupg && \
echo "honor-http-proxy" > /etc/gnupg/dirmngr.conf && \
echo "honor-http-proxy" > /etc/pacman.d/gnupg/dirmngr.conf && \
pacman-key --init && \
pacman-key --recv-key 0706B90D37D9B881 FBA220DFC880C036 --keyserver keyserver.ubuntu.com && \
pacman-key --lsign-key 0706B90D37D9B881 FBA220DFC880C036 && \
pacman --noconfirm -U 'https://geo-mirror.chaotic.cx/chaotic-aur/chaotic-'{keyring,mirrorlist}'.pkg.tar.zst' && \
echo "[multilib]" >> /etc/pacman.conf && \
echo "Include = /etc/pacman.d/mirrorlist" >> /etc/pacman.conf && \
echo "[chaotic-aur]" >> /etc/pacman.conf && \
echo "Include = /etc/pacman.d/chaotic-mirrorlist" >> /etc/pacman.conf && \
pacman -Syu --noconfirm && \
pacman -Sy --noconfirm python-cleo-git python-poetry-git pyenv
COPY --from=builder /root/.pyenv/versions/$PYTHON_VERSION /root/.pyenv/versions/$PYTHON_VERSION
RUN pyenv rehash && pyenv global $PYTHON_VERSION
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
\
PIP_NO_CACHE_DIR=1 \
PIP_DEFAULT_TIMEOUT=100 \
\
POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment