How can I install lxml in docker

与世无争的帅哥 提交于 2020-02-26 08:50:06

问题


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?


回答1:


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




回答2:


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




回答3:


Since I was using a much more bare-bone image I needed some more libs/apps.

This worked for me:

RUN apk add --update --no-cache g++ gcc libxml2-dev libxslt-dev python-dev libffi-dev openssl-dev make

RUN pip install -r requirements.txt



回答4:


Do as in

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

Download the 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




回答5:


Actually, it's just

RUN apt-get install -y libxslt1-dev


来源:https://stackoverflow.com/questions/35931579/how-can-i-install-lxml-in-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!