使用官方python docker镜像作为基础,运行python-ldap

16

我正在使用官方的 Python Docker 镜像来运行 Python 2.7 版本。我的应用程序需要使用 pyhon-ldap。

我的 Dockerfile 如下所示:

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

requirements.txt 中包含 python-ldap

自然会遇到这个问题:

In file included from Modules/LDAPObject.c:9:0:
Modules/errors.h:8:18: fatal error: lber.h: No such file or directory
 #include "lber.h"
                  ^
compilation terminated.
error: command 'gcc' failed with exit status 

我知道这是因为没有安装libldap2-dev和其他一些包。于是我进行了一些研究,发现官方的Python镜像是基于Debian Jessie构建的。虽然我更喜欢Redhat,但我知道如何使用apt-get,所以我修改了我的Docker文件如下:

FROM python:2.7
RUN apt-get install -y libldap2-dev
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

然后就会出现以下错误

Step 1 : RUN apt-get install -y libldap2-dev
 ---> Running in 2ca6155b606e
Reading package lists...
Building dependency tree...
Reading state information...
Package libldap2-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'libldap2-dev' has no installation candidate

好的,我发现存储库不见了,我进行了更多的研究,并在Python基础镜像中旋转容器并进行了一些调试。我可以证明已安装存储库(尽管我实际上不知道这些是否有效)。

cat /etc/apt/sources.list
deb http://httpredir.debian.org/debian jessie main
deb http://httpredir.debian.org/debian jessie-updates main
deb http://security.debian.org jessie/updates main

我似乎无法完全确定发生了什么,但看起来apt实际上没有访问存储库的外部访问权限,而apt-cache搜索仅返回已安装的软件包。我该如何让apt在容器内安装软件和/或重新配置以使其实际起作用?

有没有更好的方法使用官方Python映像来获取我需要编译Python-ldap所需的软件包?

2个回答

20

在安装之前运行apt-get update即可完成任务:

RUN apt-get update && apt-get install -y libldap2-dev


16

这对我来说在 Python 3.4 和 Python 3.5 两个版本上都起作用:

RUN apt-get update && apt-get install -y python-dev libldap2-dev libsasl2-dev libssl-dev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接