3

I am trying to run script which sends message to telegram using python-telegram-bot module. The script runs in docker on alpine.

The script cannot import Telegram, I get an error ModuleNotFoundError: No module named 'cryptography'

My docker file

FROM nickgryg/alpine-pandas:3.7.7 as base
FROM base as builder

RUN pip install --upgrade pip

RUN mkdir /install
RUN apk update && apk add postgresql-dev gcc musl-dev python3-dev libffi-dev openssl-dev 
WORKDIR /install
COPY requirements.txt /requirements.txt
RUN pip install --install-option="--prefix=/install" -r /requirements.txt
FROM base
COPY --from=builder /install /usr/local
COPY src /app
RUN apk --no-cache add libpq 
WORKDIR /app

requirements.txt is below

certifi==2020.4.5.1
chardet==3.0.4
Django==3.0.3
future==0.18.2
idna==2.9
pandas==1.0.3
pycountry==19.8.18
python-dateutil==2.8.1
pytz==2019.3
requests==2.23.0
six==1.14.0
sqlparse==0.3.1
urllib3==1.25.8
vertica-python==0.10.3
currencyconverter==0.14.1
python-telegram-bot==12.6.1
psycopg2==2.8.5

I also tried to add line add apk py3-cryptography to dockerfile but that didn't help.

I found many questions related to issues with cryptography but no solutions helped.

5
  • Show us your python source code that you copy into container in COPY src /app line Apr 25, 2020 at 15:20
  • Run python -m pip install <your-module>
    – xilpex
    Apr 25, 2020 at 17:21
  • Could you post the full output of your docker build command?
    – vijay v
    Apr 25, 2020 at 17:29
  • try to add this library libssl-dev
    – Beny Gj
    Apr 26, 2020 at 13:05
  • libressl-dev should be used for alpine, I tried it and nothing worked
    – Porada Kev
    Apr 27, 2020 at 9:00

2 Answers 2

4

Managed to resolve it myself. As the image is built in two stages I don't install cryptography during the first stage now, it is packaged into a wheel file. Which will then be copied and installed in the new image.

FROM nickgryg/alpine-pandas as base
FROM base as builder

RUN pip install --upgrade pip

RUN mkdir /install
RUN apk update && apk add gcc musl-dev python3-dev libffi-dev openssl-dev libc-dev postgresql-dev 
WORKDIR /install
COPY requirements.txt /requirements.txt
RUN pip install --install-option="--prefix=/install" -r /requirements.txt

RUN mkdir /wheels
WORKDIR /wheels
RUN pip wheel cryptography

FROM base
RUN apk add libressl
COPY --from=builder /install /usr/local
COPY --from=builder /wheels /wheels
RUN pip install /wheels/*.whl
COPY src /app
RUN apk --no-cache add libpq 
WORKDIR /app
0
+25

The image you're using is alpine. If you're having a dependency on cryptography module, then follow the below steps to get it fixed.

Building cryptography on Linux

Cryptography ships manylinux wheels (as of 2.0) so all dependencies are included. For users on pip 8.1 or above running on a manylinux1 or manylinux2010 compatible distribution (almost everything except Alpine) all you should need to do is:

$ pip install cryptography

If you are on Alpine or just want to compile it yourself then cryptography requires a compiler, headers for Python (if you're not using pypy), and headers for the OpenSSL and libffi libraries available on your system.

Alpine

Replace python3-dev with python-dev if you're using Python 2.

$ sudo apk add gcc musl-dev python3-dev libffi-dev openssl-dev

If you get an error with openssl-dev you may have to use libressl-dev.

So making the changes on the above apk given packages in your Dockerfile should work.

Just in case, if you want more details for other OS distributions, it can be found in section Building cryptography on Linux on the Cryptography Official Site and Cryptography GitHub.

1
  • Thank you for the answer. But these are guidelines from cryptography's official installation guide. I've already tried it, didn't help. All the requested libs are in my dockerfile and this can be seen in my question
    – Porada Kev
    Apr 27, 2020 at 9:00

Your Answer

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

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