49

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
thiiiiiking
  • 1,233
  • 3
  • 12
  • 16
  • 3
    You use alpine, so it may be `apk add` but not `apt-get` – user2915097 Mar 11 '16 at 05:06
  • @user2915097 Thanks! I changed it to `apk add python-dev py-lxml`, it returned the error: `error: command 'gcc' failed with exit status 1`. I don't know how to install lxml – thiiiiiking Mar 11 '16 at 09:20
  • the package name is correct https://pkgs.alpinelinux.org/package/main/x86/py-lxml – user2915097 Mar 11 '16 at 09:24
  • 1
    seems to install lxml fine in this Dockerfile https://pkgs.alpinelinux.org/package/main/x86/py-lxml or https://hub.docker.com/r/ryanfox1985/docker-couchpotato/builds/boinrrs9dbhnutwjxjw2l8m/ or https://hub.docker.com/r/trcook/docker-scrapy/builds/bfrzq6d6nzquxl5vw3qt4k5/ – user2915097 Mar 11 '16 at 09:26

6 Answers6

81

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

Derek Adair
  • 21,846
  • 31
  • 97
  • 134
Cintia Sestelo
  • 919
  • 6
  • 3
21

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

Bohdan Sukhov
  • 212
  • 2
  • 5
  • 1
    thanks @bohdan-sukhov In my testing I've found you will also need to add the package `py3-lxml` in order for it to work – Gilad Sharaby Jan 09 '20 at 13:36
  • 3
    rather than bothering to delete .build-deps, you can use [multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/), with /// # install dependencies /// COPY ./requirements.txt . /// RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt /// in the first build, and /// COPY --from=builder /usr/src/app/wheels /wheels /// COPY --from=builder /usr/src/app/requirements.txt . /// RUN pip install --upgrade --no-cache pip /// RUN pip install --no-cache /wheels/* /// in the final build. – Kim SJ Oct 21 '20 at 19:37
  • From 274MB to 63MB :D Thank you! – Marcello DeSales Feb 03 '21 at 09:20
5

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
Ryan Augustine
  • 1,455
  • 17
  • 14
4

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

Arrcival
  • 169
  • 9
-3

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

user2915097
  • 30,758
  • 6
  • 57
  • 59
-6

Actually, it's just

RUN apt-get install -y libxslt1-dev
Pablo Morales
  • 687
  • 1
  • 5
  • 15