如何在docker中安装lxml

时间:2016-03-11 03:19:03

标签: python docker lxml dockerfile

我想在docker中部署我的python项目,我在requirments.txt中写了lxml>=3.5.0,因为项目需要lxml。这是我的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"]

当我将它部署到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

我虽然是因为'python-dev'和'python-lxml',然后我编辑了这样的dockfile:

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

它不起作用,我又出了一个错误:

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

如何在docker中正确安装lxml?感谢

6 个答案:

答案 0 :(得分:44)

我在RUN apk add --update --no-cache g++ gcc libxslt-dev之前添加了RUN pip install -r requirements.txt,但它确实有效。

答案 1 :(得分:4)

接受的答案不是整洁的,并安装了冗余软件包。减小图像尺寸的更好解决方案是:

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

结果图像大小将为< 163MB

答案 2 :(得分:2)

由于我使用的是更加裸露的图像,因此我需要更多的libs / apps。

这对我有用:

.then(results => {
   this.setState({ AccountDetails: results.recordset }, ()=>{
       console.log(this.state.AccountDetails); // <----check the log here.
       // You can move your conditions here
   });
});

答案 3 :(得分:1)

Since only this answer worked 对我来说,我想要一些轻便的东西

And I liked this answer,但一开始对我不起作用

我自己编辑过,最后得到了这个:

super(props)

最终图像大约为 110MB,并且不再有任何 libxml 和 libslt 错误

答案 4 :(得分:-3)

实际上,只是

RUN apt-get install -y libxslt1-dev

答案 5 :(得分:-4)

按照

进行操作

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

下载apk并安装

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