soof-golan / dockerizing-python

A step-by-step reference on dockerizing a python application
https://www.youtube.com/watch?v=PorXZuDiD0k
MIT License
5 stars 0 forks source link

Poetry and Monorepos and Virtualenv #1

Open yordis opened 10 months ago

yordis commented 10 months ago

Hey there, YouTube just recommended me your video, probably because I was researching how to dockerize a python project int a monorepo using Poetry.

Disclaimer, I have zero experience in the Python ecosystem, I am helping a friend as much as I can, but I honestly do not understand what I am doing, I am just making it work. Second, I hope you do not mind that I am abusing your Issues to get some support on it, my intent is to document publicly what I am doing, and hopefully you get to create even more amazing content around it.

I am working on a monorepo with the following structure:

.
├── packages
│   └── core
│       ├── core
│       │   ├── __init__.py
│       ├── poetry.lock
│       ├── poetry.toml
│       ├── pyproject.toml
├── poetry.lock
├── poetry.toml
├── pyproject.toml
├── services
│   ├── my_app
│   │   ├── Dockerfile
│   │   ├── Makefile
│   │   ├── README.md
│   │   ├── my_app
│   │   │   ├── main.py
│   │   ├── poetry.lock
│   │   ├── poetry.toml
│   │   ├── pyproject.toml

I have the following Dockerfile, I am gonna document things inline:

FROM python:3.10.13-slim as builder

# Is it a good idea to use virtualenv so in the multi-stage I just copy the
# virtualenv folder?
RUN pip install poetry==1.7.1 && poetry config virtualenvs.in-project true

WORKDIR /myapp
COPY ./packages ./packages
COPY ./services/my_app ./services/my_app

WORKDIR /myapp/services/my_app
RUN poetry install --no-interaction --no-cache --only main

FROM python:3.10.13-slim as runner

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PYTHONFAULTHANDLER=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1

WORKDIR /myapp
COPY --from=builder /myapp ./

EXPOSE 8080

# I do not know how to avoid the problem of poetry hard-coding the path to the
# virtualenv in the shebang of the generated scripts.
CMD ["/myapp/services/my_app/.venv/bin/python", "-m", "my_app.main"]

I would appreciate if you have any idea of how I could properly setup the monorepo + poetry + docker in a way that is simple enough to maintain, or at the very least predictable. I tried my best, and this what I came up with.

Hopefully having a monorepo setup will create more content in the ecosystem since I wish your video + monorepo existed when I had to research this, and I still do not know if this is a good way since I am not a python dev.

Thank you in advance.

soof-golan commented 10 months ago

Thank you for writing this! I'll followup up with a "blessed" monorepo version in the (not internet-legally binding) near future!