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?

8
  • 1
    @sinoroc uh ok what about pyenv?
    – crxyz
    Feb 12, 2021 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, 2021 at 19:01
  • @sinoroc I'm just using pyenv because sometimes the default system python isn't up to date.
    – crxyz
    Feb 12, 2021 at 21:03
  • @sinoroc and poetry is the package manager
    – crxyz
    Feb 12, 2021 at 21:03

1 Answer 1

6

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.

2
  • 3
    A. There are Docker images for specific Python versions, instead of apt-installing things: 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, 2021 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, 2021 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.