How to install lxml in docker

I want to deploy my Python project in Docker, I wrote lxml>=3.5.0 in Requirments.txt, because the project requires lxml. Here is my dock file:

 FROM gliderlabs/alpine:3.3 RUN set -x \ && buildDeps='\ python-dev \ py-pip \ build-base \ ' \ && apk --update add python py-lxml $buildDeps \ && rm -rf /var/cache/apk/* \ && mkdir -p /app ENV INSTALL_PATH /app WORKDIR $INSTALL_PATH COPY requirements-docker.txt ./ RUN pip install -r requirements.txt COPY . . RUN apk del --purge $buildDeps ENTRYPOINT ["celery", "-A", "tasks", "worker", "-l", "info", "-B"] 

I got this when deployed in docker:

 ********************************************************************************* Could not find function xmlCheckVersion in library libxml2. Is libxml2 installed? ********************************************************************************* error: command 'gcc' failed with exit status 1 ---------------------------------------- Rolling back uninstall of lxml 

I thought this was due to 'python-dev' and 'python-lxml', then I edited the dock file like this:

 WORKDIR $INSTALL_PATH COPY requirements-docker.txt ./ RUN apt-get build-dev python-lxml RUN pip install -r requirements.txt 

This did not work, and I got another error:

 ---> Running in 73201a0dcd59 /bin/sh: apt-get: not found 

How to install lxml in docker?

+24
source share
4 answers

I added RUN apk add --update --no-cache g++ gcc libxslt-dev to RUN pip install -r requirements.txt and it worked.

+51
source

The accepted answer is not accurate and installs redundant packets. The best solution to reduce image size would be:

 RUN apk add --no-cache --virtual .build-deps gcc libc-dev libxslt-dev && \ apk add --no-cache libxslt && \ pip install --no-cache-dir lxml>=3.5.0 && \ apk del .build-deps 

The resulting image size will be & lt; 163MB

+2
source

Do as in

https://hub.docker.com/r/ryanfox1985/docker-couchpotato/builds/boinrrs9dbhnutwjxjw2l8m/

Download apk and install it

RUN wget http://nl.alpinelinux.org/alpine/edge/main/x86_64/py-lxml-3.4.0-r0.apk -O /var/cache/apk/py-lxml.apk RUN apk add --allow-untrusted /var/cache/apk/py-lxml.apk

-4
source

In fact, it's just

 RUN apt-get install -y libxslt1-dev 
-5
source

All Articles