How to install hdf5 on Docker image with Linux alpine 3.13 - python-3.x

I am building a Docker image with python 3.7.10 (Linux Alpine v3.13) but when building the image with docker build . the package hdf5 will fail during the installation. This is my Dockerfile:
FROM python:3.7.10-alpine3.13
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /requirements.txt
ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers
RUN apk add --no-cache jpeg-dev zlib-dev mariadb-dev libffi-dev openblas-dev libgfortran lapack-dev build-base openssl-dev
RUN apk add --no-cache hdf5 hdf5-dev
RUN pip install -r /requirements.txt
RUN apk --no-cache del build-base
ENV PYTHONUNBUFFERED 1
COPY . /app/
CMD ["uwsgi", "my_app"]
requirements.txt file:
h5py==2.10.0
numpy==1.19.2
I have tried with and without the --no-binary flag but still no luck. Was somebody able to install that library on this Alpine version?
Error logs:
17858 | __pyx_t_1 = H5Ovisit_by_name(__pyx_v_loc_id, __pyx_v_obj_name, __pyx_v_idx_type, __pyx_v_order, __pyx_v_op, __pyx_v_op_data, __pyx_v_lapl_id); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 1641, __pyx_L1_error)
| ^~~~~~~~~~~~~~~~
In file included from /usr/include/H5Apublic.h:22,
from /usr/include/hdf5.h:23,
from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/api_compat.h:27,
from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:654:
/usr/include/H5Opublic.h:213:15: note: declared here
213 | H5_DLL herr_t H5Ovisit_by_name3(hid_t loc_id, const char *obj_name,
| ^~~~~~~~~~~~~~~~~
/tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c: In function '__pyx_f_4h5py_4defs_H5Pget_driver_info':
/tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:21768:13: warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
21768 | __pyx_t_1 = H5Pget_driver_info(__pyx_v_plist_id); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 2016, __pyx_L1_error)
| ^
In file included from /usr/include/H5public.h:32,
from /usr/include/hdf5.h:22,
from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/api_compat.h:27,
from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:654:
/tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c: In function '__pyx_f_4h5py_4defs_H5Sencode':
/tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:34528:15: error: too few arguments to function 'H5Sencode2'
34528 | __pyx_t_1 = H5Sencode(__pyx_v_obj_id, __pyx_v_buf, __pyx_v_nalloc); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 3303, __pyx_L1_error)
| ^~~~~~~~~
In file included from /usr/include/hdf5.h:38,
from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/api_compat.h:27,
from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:654:
/usr/include/H5Spublic.h:129:15: note: declared here
129 | H5_DLL herr_t H5Sencode2(hid_t obj_id, void *buf, size_t *nalloc, hid_t fapl);
| ^~~~~~~~~~
In file included from /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/ndarrayobject.h:21,
from /usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/arrayobject.h:4,
from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/api_compat.h:26,
from /tmp/pip-install-fkianwj6/h5py_dfa9366c1fdb47e98e9e2e8de85ac21e/h5py/defs.c:654:
At top level:
/usr/local/lib/python3.7/site-packages/numpy/core/include/numpy/__multiarray_api.h:1464:1: warning: '_import_array' defined but not used [-Wunused-function]
1464 | _import_array(void)
| ^~~~~~~~~~~~~
error: command 'gcc' failed with exit status 1
----------------------------------------

Seems like I have used a wrong and old h5py version (h5py==2.10.0). The following setup worked just fine when updating to h5py==3.2.1 within the requirements.txt.
FROM python:3.7.10-alpine3.13
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /requirements.txt
ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
RUN apk add --update --no-cache --virtual .tmp gcc libc-dev linux-headers
RUN apk add --no-cache jpeg-dev zlib-dev mariadb-dev libffi-dev openblas-dev libgfortran lapack-dev build-base openssl-dev
RUN apk add --no-cache hdf5-dev
RUN pip install -r /requirements.txt
RUN apk --no-cache del build-base
ENV PYTHONUNBUFFERED 1
COPY . /app/
CMD ["uwsgi", "my_app"]
requirements.txt file:
h5py==3.2.1
numpy==1.20.1
Thanks to #jordanvrtanoski and #valiano for the feedback.

Related

Dockerfile ot working in Linux Sever but working in Ubuntu VM

I have a Dockerfile and it's working fine in Ubuntu VM. However, the same Dockerfile does not build in Linux Server.
Dockerfile:
FROM python:3.9.7-slim as builder-image
ARG DEBIAN_FRONTEND=noninteractive
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONFAULTHANDLER 1
RUN apt-get update && apt-get install -y --no-install-recommends python3-dev gcc libc-dev musl-dev libffi-dev g++ cargo && \
apt-get clean && rm -rf /var/lib/apt/lists/*
RUN python3.9 -m venv /home/myuser/venv
ENV PATH="/home/myuser/venv/bin:$PATH"
RUN /home/myuser/venv/bin/pip install --upgrade pip
WORKDIR /home/myuser/venv
COPY /data/requirements.txt requirements.txt
RUN pip3 install --no-cache-dir wheel
RUN pip3 install --no-cache-dir -r requirements.txt
FROM python:3.9.7-slim
RUN useradd --create-home myuser
COPY --from=builder-image /home/myuser/venv /home/myuser/venv
USER myuser
RUN mkdir /home/myuser/code
WORKDIR /home/myuser/code
ENV PYTHONUNBUFFERED=1
ENV VIRTUAL_ENV=/home/myuser/venv
ENV PATH="/home/myuser/venv/bin:$PATH"
ENTRYPOINT ["/bin/bash"]
docker build -t python-docker_14122021 .
Error:
Sending build context to Docker daemon 49.66 kB
Step 1/23 : FROM python:3.9-slim-buster as builder-image
Error parsing reference: "python:3.9-slim-buster as builder-image" is not a valid repository/tag: invalid reference format
You have a very old docker on the server. You need to have at least version 17.06 of docker to support multi-staging builds.

Docker Alpine: unable to select packages: python (no such package) while building image for ARM

I have Node.js app that uses SQLite database. To use it on ARM architecture, I need to build sqlite3 binaries so I need some packages while building Docker image.
Here is my Dockerfile:
FROM node:14-alpine as builder
WORKDIR /app
COPY package*.json ./
RUN apk update \
&& apk --no-cache --virtual build-dependencies add python make g++ \
&& npm install --production
COPY . .
RUN mkdir -p ./public ./data \
&& cd ./client \
&& npm install --production \
&& npm run build \
&& cd .. \
&& mv ./client/build/* ./public \
&& rm -rf ./client \
&& apk del build-dependencies
FROM node:14-alpine
COPY --from=builder /app /app
WORKDIR /app
EXPOSE 5005
ENV NODE_ENV=production
CMD ["node", "server.js"]
I've been using it for 6 months and it was working fine but now it throws this error:
> [linux/amd64 builder 4/6] RUN apk update && apk --no-cache --virtual build-dependencies add python make g++ && npm install --production:
#10 0.166 fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
#10 0.503 fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
#10 1.141 v3.14.2-123-g010734651f [https://dl-cdn.alpinelinux.org/alpine/v3.14/main]
#10 1.141 v3.14.2-120-g90167408c8 [https://dl-cdn.alpinelinux.org/alpine/v3.14/community]
#10 1.141 OK: 14943 distinct packages available
#10 1.216 fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
#10 1.476 fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
#10 1.936 ERROR: unable to select packages:
#10 1.989 python (no such package):
#10 1.989 required by: build-dependencies-20211108.132318[python]
------
Dockerfile.multiarch:7
--------------------
6 |
7 | >>> RUN apk update \
8 | >>> && apk --no-cache --virtual build-dependencies add python make g++ \
9 | >>> && npm install --production
10 |
--------------------
error: failed to solve: process "/bin/sh -c apk update && apk --no-cache --virtual build-dependencies
add python make g++ && npm install --production"
did not complete successfully: exit code: 2
I tried to specific python version like so: ... add python3 make .... It passes this step but I'm getting this error while building sqlite3 binaries:
#20 392.8 make: Entering directory '/app/node_modules/sqlite3/build'
#20 392.8 CC(target) Release/obj.target/nothing/../node-addon-api/nothing.o
#20 393.6 AR(target) Release/obj.target/../node-addon-api/nothing.a
#20 393.8 COPY Release/nothing.a
#20 394.0 ACTION deps_sqlite3_gyp_action_before_build_target_unpack_sqlite_dep Release/obj/gen/sqlite-autoconf-3340000/sqlite3.c
#20 394.1 /bin/sh: python: not found
#20 394.1 make: *** [deps/action_before_build.target.mk:13: Release/obj/gen/sqlite-autoconf-3340000/sqlite3.c] Error 127
#20 394.1 make: Leaving directory '/app/node_modules/sqlite3/build'
RUN apk add --no-cache --virtual .gyp python3 make g++
We hit the same error and in our case explicitly targeting python3 seems to fix it. I asked on IRC, did not find out yet why python is not working anymore, but was told that python2 still provides /usr/bin/python.
So maybe you actually need python2?

How to get rid of cryptography build error?

I am trying to build a dockerfile but the problem is when it trying to build specifically cryptography is not building.
MY Dockerfile
FROM python:3.7-alpine
ENV PYTHONUNBUFFERED 1
RUN apk update \
# psycopg2 dependencies
&& apk add --virtual build-deps gcc python3-dev musl-dev\
&& apk add postgresql-dev \
&& apk add build-base \
# Pillow dependencies
&& apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \
# CFFI dependencies
&& apk add libffi-dev py-cffi \
# Translations dependencies
&& apk add gettext \
# https://docs.djangoproject.com/en/dev/ref/django-admin/#dbshell
&& apk add postgresql-client \
# cairo
&& apk add cairo cairo-dev pango-dev gdk-pixbuf-dev poppler-utils
# fonts for weasyprint
RUN mkdir ~/.fonts
COPY ./fonts/* /root/.fonts/
# secret key (should be in docker-secrets, or we need to run minikube locally
RUN mkdir /etc/secrets
COPY secret.readme proxy_rsa_key* /etc/secrets/
# Requirements are installed here to ensure they will be cached.
COPY ./requirements /requirements
RUN pip install -r /requirements/local.txt
COPY ./compose/local/django/entrypoint /entrypoint
RUN sed -i 's/\r//' /entrypoint
RUN chmod +x /entrypoint
COPY ./compose/local/django/start /start
RUN sed -i 's/\r//' /start
RUN chmod +x /start
COPY ./compose/local/django/celery/worker/start /start-celeryworker
RUN sed -i 's/\r//' /start-celeryworker
RUN chmod +x /start-celeryworker
COPY ./compose/local/django/celery/beat/start /start-celerybeat
RUN sed -i 's/\r//' /start-celerybeat
RUN chmod +x /start-celerybeat
COPY ./compose/local/django/celery/flower/start /start-flower
RUN sed -i 's/\r//' /start-flower
RUN chmod +x /start-flower
WORKDIR /app
ENTRYPOINT ["/entrypoint"]
when I try to build my dockerfile it shows:
Building wheel for cryptography (PEP 517): finished with status 'error'
ERROR: Command errored out with exit status 1:
error: Can not find Rust compiler
----------------------------------------
ERROR: Failed building wheel for cryptography
I tried to solve but i couldn't. I am newbie in docker.Please help how to get rid of this problem.
cryptography < 3.5:
You can skip the rust installation and other related dependencies by adding the line below before apk add commands:
ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
cryptography >= 3.5: Thanks #riptusk331
After this version rust will be required. Either install a specific version <3.5 or follow cryptography installation instructions. It is stated in the attached link that they are very aggressively explained in the docs.
Since the error is...
error: Can not find Rust compiler
...the solution is to install the rust compiler. You'll also need
cargo, the Rust package manager, and it looks like your Dockerfile
is missing openssl-dev.
The following builds successfully for me:
FROM python:3.7-alpine
ENV PYTHONUNBUFFERED 1
RUN apk add --update \
build-base \
cairo \
cairo-dev \
cargo \
freetype-dev \
gcc \
gdk-pixbuf-dev \
gettext \
jpeg-dev \
lcms2-dev \
libffi-dev \
musl-dev \
openjpeg-dev \
openssl-dev \
pango-dev \
poppler-utils \
postgresql-client \
postgresql-dev \
py-cffi \
python3-dev \
rust \
tcl-dev \
tiff-dev \
tk-dev \
zlib-dev
RUN pip install cryptography
Note that the above apk add ... command line is largely the same as
what you've got; I've just simplified the multiple apk add ...
statements into a single apk add execution.
pip install -U pip is what all I had to do
Some people might come here (Like I did) looking for a fix just for Python, not necessarily Alpine.
Several options are available, mentioned in the github issue. (only pick one of these)
You can install rust, as another answer mentioned
You can downgrade your cryptography version to 3.4.x
You can upgrade to pip version 19.1.1 or higher, which installs precompiled packages
I faced the same issue and in order to resolve it I tried out the following:
Answer provided by #larsks. RUN apk add cargo openssl-dev helped. But this resulted in a huge image size for me (>1GB). Best practice is to always target the bare-minimum with a small image size. This way, we don't expose ourselves to any external vulnerabilities.
Answer provided by Sabri Özgür, ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1. This worked great!
Based on the comment added by #riptusk331 for the earlier answer, simply ignoring the Rust build may only work for now, as Cryptography 3.5+ will start requiring Rust.
My solution just to be safe was;
...
ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
...
RUN pip install cryptography==3.4.6
...
This way, I managed to keep the image size at a considerably lower value while getting the build to pass.
Editing pyvenv.cfg by adding in my .env:
CRYPTOGRAPHY_DONT_BUILD_RUST=1
then doing pip install cryptography, solved my issue.

Docker python3.8 alpine python-ldab installation fails missing lber.h

I am trying to pip install python-ldap in Docker but am missing lber.h. I need python-ldap because I am trying to implement an OS agnostic SSO for my application, following this for now and hoping it doesn't break the rest of my app https://code.tutsplus.com/tutorials/flask-authentication-with-ldap--cms-23101
I have tried
RUN apk add libsasl2-dev libldap2-dev libssl-dev
and
RUN apk --no-cache add libsasl2-dev libldap2-dev libssl-dev
but I get the following error:
ERROR: unsatisfiable constraints:
libldap2-dev (missing):
required by: world[libldap2-dev]
libsasl2-dev (missing):
required by: world[libsasl2-dev]
libssl-dev (missing):
required by: world[libssl-dev]
Having gone to https://pkgs.alpinelinux.org/packages?name=libldap2-dev&branch=edge I feel that it is possible that these packages do indeed not exist within the alpine python image. Therefore, I tried substituting the installations with packages that exist in the above link, namely: openssl libsasl libldap
This didn't work either and had the same effect as not installing any of the packages i.e. the header missing error appeared:
Modules/constants.h:7:10: fatal error: lber.h: No such file or directory
#include "lber.h"
^~~~~~~~
compilation terminated.
error: command 'gcc' failed with exit status 1
as a result of /tmp/pip-install-doifa8k9/python-ldap/
My Dockerfile:
FROM python:3.8.0-alpine
WORKDIR /home/flask
ADD . /home/flask
RUN apk --no-cache add --update --virtual .build-editdistance gcc g++ && \
apk --no-cache add libsasl libldap openssl && \
apk --no-cache add --update --virtual .libs libstdc++ && \
apk add linux-headers && \
apk add unixodbc-dev;
RUN rm -rf /var/cache/apk/* && \
pip install --upgrade pip && \
pip install --upgrade setuptools && \
pip install --no-cache-dir -Ur requirements.txt && \
apk del .build-editdistance;
EXPOSE 5000
For alpine python in docker:
apk add openldap-dev
instead of
apk add libsasl libldap openssl
This is answered in I can't install python-ldap but the answer is buried sufficiently deep and obfuscated by the accepted answer ( which is where libsasl libldap openssl came from) that it would be a good idea to keep this docker specific question

Dockerfiles build performance issues

The following Dockerfiles takes more than 30 minutes to build.
FROM python:3.7-alpine
COPY . /app
WORKDIR /app
RUN apk add --no-cache python3-dev libstdc++ && \
apk add --no-cache g++ && \
ln -s /usr/include/locale.h /usr/include/xlocale.h && \
pip install --upgrade pip && \
pip3 install --upgrade pip && \
pip3 install allure-behave && \
pip3 install -r requirements.txt
entrypoint ["sh", "testsuite.sh"]
Requirement file:
behave==1.2.6
boto3==1.8.2
botocore==1.11.9
pandas==0.25.0
Is that normal?

Resources