Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I install lxml in docker

I want to deploy my python project in docker, I wrote lxml>=3.5.0 in the requirments.txt as the project needs lxml. Here is my dockfile:

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 I deploy it to 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 though it was because 'python-dev' and 'python-lxml', then I edited the dockfile like this:

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

It did not work, and I got another error:

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

How can I install lxml correctly in docker?

like image 321
thiiiiiking Avatar asked Mar 11 '16 03:03

thiiiiiking


People also ask

How do I get lxml version?

In case you want to use the current in-development version of lxml, you can get it from the github repository at https://github.com/lxml/lxml . Note that this requires Cython to build the sources, see the build instructions on the project home page.

What is lxml package?

lxml is a Python library which allows for easy handling of XML and HTML files, and can also be used for web scraping. There are a lot of off-the-shelf XML parsers out there, but for better results, developers sometimes prefer to write their own XML and HTML parsers.


2 Answers

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

like image 69
Cintia Sestelo Avatar answered Sep 25 '22 05:09

Cintia Sestelo


Accepted answer is not neat and installs redundant packages. Better solution for reducing image size will 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  

Result image size will be < 163MB

like image 22
Bohdan Sukhov Avatar answered Sep 22 '22 05:09

Bohdan Sukhov