35

I have a Dockerfile which installs a few packages via pip. Some of them are requiring grpcio, and it takes a few minutes only to build this part.

Does anyone have a tip to speed up this part?

Installing collected packages: python-dateutil, azure-common, azure-nspkg, azure-storage, jmespath, docutils, botocore, s3transfer, boto3, smmap2, gitdb2, GitPython, grpcio, protobuf, googleapis-common-protos, grpc-google-iam-v1, pytz, google-api-core, google-cloud-pubsub
Found existing installation: python-dateutil 2.7.3
  Uninstalling python-dateutil-2.7.3:
    Successfully uninstalled python-dateutil-2.7.3
Running setup.py install for grpcio: started
Running setup.py install for grpcio: still running...
Running setup.py install for grpcio: still running...
Running setup.py install for grpcio: still running...

Thanks.

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Indigo Ping
  • 418
  • 1
  • 4
  • 8
  • What docker image are you using, `alpine`? Also, what is the target Python version in the image? – hoefling Oct 20 '18 at 10:43
  • please use the cache and you dont need to build thoese layers everytime , put new things in the end of dockerfile – Ijaz Ahmad Oct 20 '18 at 12:55
  • Having the same problem building the wheel for grpcio, see code here: https://github.com/ohduran/Promenade/blob/master/instarest/compose/local/django/Dockerfile – ohduran Mar 20 '19 at 07:54
  • 1
    @ohduran your link is dead – Hyperbole Mar 29 '19 at 15:32
  • 1
    You're right @Hyperbole, new link: https://github.com/ohduran/Promenade/blob/master/compose/local/django/Dockerfile – ohduran Mar 31 '19 at 10:44
  • If you are using alpine, it takes time to compile from scratch https://github.com/grpc/grpc/issues/22815#issuecomment-649690837 – Sachin G. Oct 30 '21 at 12:22

3 Answers3

39

I had the same issue and it was solved by upgrading pip:

$ pip3 install --upgrade pip

Here's a word from one of the maintainers of grpc project:

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
11

Had the same issue, fixed it by using a virtualenv and a multistage dockerfile :

FROM python:3.7-slim as base

# ---- compile image -----------------------------------------------
FROM base AS compile-image
RUN apt-get update \
  && apt-get install -y --no-install-recommends \
  build-essential \
  gcc

RUN python -m venv /app/env
ENV PATH="/app/env/bin:$PATH"

COPY requirements.txt .
RUN pip install --upgrade pip
# pip install is fast here (while slow without the venv) :
RUN pip install -r requirements.txt

# ---- build image -----------------------------------------------
FROM base AS build-image
COPY --from=compile-image /app/env /app/env

# Make sure we use the virtualenv:
ENV PATH="/app/env/bin:$PATH"
COPY . /app
WORKDIR /app

Here is my requirements.txt :

fastapi==0.27.*
grpcio-tools==1.21.*
uvicorn==0.7.*
PhE
  • 15,656
  • 4
  • 23
  • 21
1

Some docker images (looking at you, Alpine) can't pull prebuilt wheels. Use a docker image that can, like Debian.

Check out this nice write-up on why. I'll reproduce a quote from it, that's especially apt:

But for Python, as Alpine doesn't use the standard tooling used for building Python extensions, when installing packages, in many cases Python (pip) won't find a precompiled installable package (a "wheel") for Alpine. And after debugging lots of strange errors you will realize that you have to install a lot of extra tooling and build a lot of dependencies just to use some of these common Python packages. -- Sebastián Ramírez

Gleno
  • 16,621
  • 12
  • 64
  • 85