Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't install dependencies in docker container

I'm using poetry library to manage project dependencies, so when I use

docker build --tag=helloworld .

I got this error

[AttributeError]               
'NoneType' object has no attribute 'group'  

Installing breaks on umongo (2.1.0) package

Here is my pyproject.toml file

[tool.poetry.dependencies]
python = "^3.7.0"
asyncio = "^3.4"
aiohttp = "^3.4"
motor = "^2.0"
umongo = "^2.0"
pyyaml = "^3.13"

[tool.poetry.dev-dependencies]
pytest = "^3.4"
black = {version = "^18.3-alpha.0",allows-prereleases = true}
mypy = "^0.650.0"
wemake-python-styleguide = "^0.5.1"
pytest-mock = "^1.10"
pytest-asyncio = "^0.9.0"
pytest-aiohttp = "^0.3.0"

And poetry.lock https://pastebin.com/kUjAKJHM

Dockerfile:

FROM python:3.7.1-alpine

RUN mkdir -p /opt/project/todo_api
RUN pip --no-cache-dir install poetry

COPY ./pyproject.toml /opt/project
COPY poetry.lock /opt/project

RUN cd /opt/project && poetry install --no-dev

COPY ./todo_api /opt/project/todo_api
COPY ./todo_api.yml /opt/project/todo_api.yml

WORKDIR /opt/project

ENTRYPOINT poetry run python -m aiohttp.web todo_api.main:main
like image 322
Skufler Avatar asked Aug 02 '19 18:08

Skufler


1 Answers

Alternative approach

Don't install poetry into your deployment environment. It's a package management tool, which aims to improve development of and collaboration on libraries. If you want to deploy an application, you only need a package installer (read: pip) - and the opinionated stance of poetry regarding the build process and virtual environments is harmful rather than helpful there.

In this case, the artifacts you want to copy into your docker image are 1) your most recent build of the library you work on and 2) a wheelhouse of tested dependencies, as defined by poetry.lock.

The first one is easy, run poetry build -f wheel and you have a nicely portable wheel. The second one is not yet easy, because poetry doesn't support building wheelhouses (and maybe never will), and pip wheel does not accept poetry.lock's file format. So if you want go down this road, you need to work on a beta build of poetry (v1.0.0b7 is rather stable) that supports poetry export -f requirements.txt > requirements.txt, which lets you create a requirements.txt file equivalent to your current lockfile.

Once you got that, you can run pip wheel -w dist -r requirements.txt, and finally, you're done creating all the artifacts for the docker image. Now, the following will work:

FROM python:3.7.1-alpine

WORKDIR /opt/project

COPY dist dist

RUN pip install --no-index --find-links dist todo_api

ENTRYPOINT python -m aiohttp.web todo_api.main:main

Pros

  • no unnecessary dependency on poetry in your server (might be relevant, since it's still <v1.0)
  • you skip the virtualenv in your server and install everything right into the system (you might still choose to create a virtualenv on your own and install your app into that, since installing your application into the system python's side-packages can lead to problems)
  • your installation step doesn't run against pypi, so this deployment is guaranteed to work as far as you tested it (this is a very important point in many business settings)

Cons

  • it's a bit of a pain if you do it by hand each time, the target executor here should be a CI/CD and not a human
  • if the architecture of your workstation and the docker image differ, the wheels you build and copy over might not be compatible
like image 176
Arne Avatar answered Oct 09 '22 21:10

Arne