Skip to content

Instantly share code, notes, and snippets.

@pdonorio
Last active January 31, 2020 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdonorio/6c3e0609dc4c39c3bae55e13a6e397e6 to your computer and use it in GitHub Desktop.
Save pdonorio/6c3e0609dc4c39c3bae55e13a6e397e6 to your computer and use it in GitHub Desktop.
Poetry packaging inside docker container

poetry and docker

why

I've found difficult to have poetry setup ready in a docker image. And even harder to create my own. I figure out I'd share a working version.

  • I tend to use always the official python image as the base, and usually alpine is my favorite flavor.
  • Following poetry's docs, the recommended way to install is to curl their script. That's definitely not the best thing to do inside a docker build...
  • In fact I preferred the usual pip way, but the cryptography python library dependency was quite a pain inside alpine.
  • I've managed to also add auto completion for the poetry binary.

build

# build and specify the Python release you need
docker build --build-arg PYTHON_RELEASE=3.8.1 -t poetry .

post

Next things you can do:

  • use poetry to start your project (needed only the first time)
  • add the toml file and the lock file then, to build for production or to package the app in general
  • skip dev dependencies in production with a variable
  • finally add the code and the command to start your app

e.g.

COPY poetry.lock pyproject.toml /app/
WORKDIR /app
RUN poetry install $(test "$ENVIRONMENT" == production && echo "--no-dev")
COPY . /app
CMD ["python", "-m", "your_package"]
ARG PYTHON_RELEASE
FROM python:${PYTHON_RELEASE}-alpine
ENV POETRY_VERSION=1.0.3 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
RUN apk update \
&& apk add --no-cache --virtual build-deps \
build-base gcc libffi-dev openssl-dev wget git \
&& apk add --no-cache bash bash-completion \
&& echo "source /usr/share/bash-completion/bash_completion" > ~/.bashrc \
&& pip install poetry==$POETRY_VERSION \
&& apk del build-deps \
&& rm -rf /var/cache/apk/* /tmp/*
RUN poetry config virtualenvs.create false \
&& poetry completions bash > /usr/share/bash-completion/completions/poetry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment