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 and specify the Python release you need
docker build --build-arg PYTHON_RELEASE=3.8.1 -t poetry .
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"]