简体   繁体   中英

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?

我在RUN pip install -r requirements.txt之前添加了RUN apk add --update --no-cache g++ gcc libxslt-dev并且它起作用了。

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

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

Since only this answer worked for me and I wanted something light

And I liked this answer , but which didn't work for me at first

I've edited it for myself and got this at the end :

RUN apk add --update --no-cache --virtual .build-deps g++ gcc libxml2-dev libxslt-dev python-dev && \
    apk add --no-cache libxslt && \
    pip install --no-cache-dir lxml>=3.5.0 && \
    apk del .build-deps

The final image is around 110MB, and didn't have anymore any libxml and libslt errors

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

其实,这只是

RUN apt-get install -y libxslt1-dev

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM