4

Note: I have just started trying to learn docker, so I'm a beginner

Currently, I'm using poetry and pyenv to make my python project. I'm using pyenv for my python version, and poetry for creating and managing my python projects. But, I want to also use docker. So, how can I can I integrate all 3 of these into a python project?

CrazyVideoGamer
  • 754
  • 8
  • 22
  • https://github.com/python-poetry/poetry/discussions/1879 – sinoroc Feb 12 '21 at 17:56
  • 1
    @sinoroc uh ok what about pyenv? – CrazyVideoGamer Feb 12 '21 at 18:00
  • 2
    I don't know. I don't think you need _pyenv_ in the container. I also don't think you need _poetry_ in the container from my personal point of view. -- I mean, this is kind of an off-topic question for _stackoverflow_ since all answers will be very much based on personal opinions. – sinoroc Feb 12 '21 at 19:01
  • @sinoroc I'm just using pyenv because sometimes the default system python isn't up to date. – CrazyVideoGamer Feb 12 '21 at 21:03
  • @sinoroc and poetry is the package manager – CrazyVideoGamer Feb 12 '21 at 21:03
  • Sure. But I don't know how to help you. The question "how can I can I integrate all 3 of these [poetry, pyenv, docker] into a python project?" is way too broad to answer here. You need to pick a concrete question, that can be answered objectively. – sinoroc Feb 12 '21 at 23:09
  • @sinoroc you can make some edits then if you want – CrazyVideoGamer Feb 16 '21 at 04:30
  • Typically in a docker container you only want one Python interpreter, so there is no need for pyenv. And typically in a docker container you only install pre-built wheels (look up "_wheel house_"), so no need for poetry. -- If you need multiple Python in docker containers, then you might want to have multiple docker images, 1 for each Python interpreter. -- Also try to figure out how to build a _wheel house_ of pre built wheels of your dependencies, and in your docker image only install those wheels with pip (no need for poetry at this point). – sinoroc Feb 16 '21 at 07:33

1 Answers1

4

Not really a perfect solution, but I made it.

FROM debian:buster-slim

RUN apt-get update
RUN apt-get install -y --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

ENV HOME="/root"
WORKDIR ${HOME}
RUN apt-get install -y git
RUN git clone --depth=1 https://github.com/pyenv/pyenv.git .pyenv
ENV PYENV_ROOT="${HOME}/.pyenv"
ENV PATH="${PYENV_ROOT}/shims:${PYENV_ROOT}/bin:${PATH}"

ENV PYTHON_VERSION=3.8.6
RUN pyenv install ${PYTHON_VERSION}
RUN pyenv global ${PYTHON_VERSION}

RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -

WORKDIR /app

RUN apt-get install -y mecab-ipadic-utf8
RUN touch /usr/local/etc/mecabrc

COPY poetry.lock pyproject.toml ./
RUN /bin/bash -c 'source $HOME/.poetry/env && POETRY_VIRTUALENVS_IN_PROJECT=true poetry install --no-dev --no-root'

COPY . .

ENTRYPOINT [ "/bin/bash", "-c", "source .venv/bin/activate && uvicorn server:app --host=0.0.0.0" ]

See also this post.

Why pyenv

Apparently, apt-get install python3 installs python 3.7, which is also incomplete (no distutil). You cannot even pin .python-version to 3.8 or 3.9 or conda; which may error in one, but not another.

Why poetry

Poetry is just another neat way to manage dependencies, although alternate ways would be Pipfile; or just virtualenv with requirements.txt.

Polv
  • 1,918
  • 1
  • 20
  • 31
  • 3
    ***A.*** There are Docker images for specific Python versions, instead of _apt-installing_ things: https://hub.docker.com/_/python/ -- ***B.*** I would recommend avoiding installing _poetry_ in the docker container. Many possibilities. ***1.*** would be to `poetry export` outside of the container and then `python -m pip install -r requirements.txt` inside the container. ***2.*** Would be to create the _wheels_ outside of the container and `python -m pip install --no-index wheels/*.whl` inside the container. – sinoroc Feb 16 '21 at 17:40
  • @sinoroc Yes, I forgot about that. That is, if Debian or Alpine is enough. Or if you use only Python in that image. As you can see, I also installed MeCab, a native dep. – Polv Feb 17 '21 at 06:12