7

any idea how can i install the php Ldap extension in dockerfile

FROM php:7.2-fpm-alpine

i tried the following

RUN docker-php-ext-configure ldap --prefix=/usr/local/php --with-ldap=/usr/lib/i386-linux-gnu
RUN docker-php-ext-install ldap

but when i build docker , i get the error message

configure: error: Cannot find ldap.h

ERROR: Service 'php' failed to build: The command '/bin/sh -c docker-php-ext-install ldap' returned a non-zero code: 1

PS: it is alpine so 'apt-get' wont work here, instead 'apk add'

Kareem Essawy
  • 565
  • 1
  • 4
  • 14
  • Note that `--with-ldap=/usr/lib/i386-linux-gnu` assumes an architecture. If you are running this in an ARM CPU (or something else, like RISC-V) then `ldap.h` won't be installed at `/usr/lib/i3860linux-gnu`. In my case, just omitting this option and installing the packages fom @pujiant's reply did it. – Elton Carvalho Feb 13 '23 at 23:13

6 Answers6

15

If you are experiencing configure: error: Cannot find ldap.h try to add this line in your Alpine based Dockerfile

RUN apk add ldb-dev libldap openldap-dev
Pujianto
  • 161
  • 1
  • 6
  • FYI. Works with `php:7-4-fpm-alpine`. – TheFrost Mar 17 '21 at 13:28
  • FYI, not working with php:8-fpm-alpine. command works successfully but i can't find ldap extension is installed. – LoveCoding Mar 09 '22 at 22:02
  • You also need to run `docker-php-ext-install ldap` and `docker-php-ext-enable ldap` for this to work. – Tim Mar 03 '23 at 22:41
  • Can you please tell me how you know this? I'm a developer with next to nothing knowledge about server operational. This is frustrating because I don't know how to debug this, only google can help me – Trisno Raynaldy Jul 26 '23 at 08:19
8

You need to use openldap-dev to have the ldap.h file to install ldap with the docker-php-ext-install script.

Something like this works:

FROM php:8.0.2-fpm-alpine

RUN apk update \
    && apk add --no-cache --virtual .build-dependencies-in-virtual-world openldap-dev \
    && docker-php-ext-install ldap \
    && docker-php-ext-enable  ldap \
    && apk del .build-dependencies-in-virtual-world

For me, I used this my Laravel project: My Dockerfile for a Laravel app

I had to use line 5 because id get this if I don't:

Undefined constant "LdapRecord\Models\Attributes\LDAP_ESCAPE_FILTER"

angellugo
  • 81
  • 1
  • 2
5

This is working for me:

FROM php:7.4.9-fpm-alpine3.12
        
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/

# Install PHP extensions
RUN install-php-extensions ldap
Stefan Pavlov
  • 398
  • 2
  • 8
1

fixed with the following dockerfile :

FROM php:7.2-fpm-alpine

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# lumen packages
RUN apk add openldap-back-mdb
RUN apk add --update --virtual .build-deps autoconf g++ make zlib-dev curl-dev libidn2-dev libevent-dev icu-dev libidn-dev openldap libxml2-dev

RUN docker-php-ext-install intl soap
RUN docker-php-ext-install mbstring tokenizer mysqli pdo_mysql json hash iconv
RUN apk --update --no-cache add php7-ldap libldap php-ldap  openldap-clients openldap openldap-back-mdb

RUN apk add --no-cache ldb-dev
RUN ln -s /usr/lib/x86_64-linux-gnu/libldap.so /usr/lib/libldap.so \
    && ln -s /usr/lib/x86_64-linux-gnu/liblber.so /usr/lib/liblber.so
#RUN docker-php-ext-configure ldap --prefix=/usr/local/php --with-ldap=/usr/lib/libldap.so
#RUN docker-php-ext-install ldap
ARG DOCKER_PHP_ENABLE_LDAP

RUN echo -n "With ldap support:          " ; if [[ "${DOCKER_PHP_ENABLE_LDAP}" = "on" ]] ;      then echo "Yes"; else echo "No" ; fi && \
    if [ -z ${DOCKER_USER_UID+x} ]; then echo "DOCKER_USER_UID is unset"; DOCKER_USER_UID=1000; else echo "DOCKER_USER_UID is set to '$DOCKER_USER_UID'"; fi && \
    if [ -z ${DOCKER_USER_GID+x} ]; then echo "DOCKER_USER_GID is unset"; DOCKER_USER_GID=1000; else echo "DOCKER_USER_GID is set to '$DOCKER_USER_GID'"; fi

# Enable LDAP
RUN if [ "${DOCKER_PHP_ENABLE_LDAP}" != "off" ]; then \
        # Dependancy for ldap \
        apk add --update --no-cache \
            libldap && \
        # Build dependancy for ldap \
        apk add --update --no-cache --virtual .docker-php-ldap-dependancies \
            openldap-dev && \
        docker-php-ext-configure ldap && \
        docker-php-ext-install ldap && \
        apk del .docker-php-ldap-dependancies && \
        php -m; \
    else \
        echo "Skip ldap support"; \
    fi

RUN pecl install raphf propro
RUN docker-php-ext-enable raphf propro
RUN pecl install pecl_http
RUN echo -e "extension=raphf.so\nextension=propro.so\nextension=iconv.so\nextension=http.so" > /usr/local/etc/php/conf.d/docker-php-ext-http.ini
RUN rm -rf /usr/local/etc/php/conf.d/docker-php-ext-raphf.ini
RUN rm -rf /usr/local/etc/php/conf.d/docker-php-ext-propro.ini
RUN rm -rf /tmp/*

COPY ./app /var/www/html/

RUN chown -R www-data:www-data /var/www/html/
RUN chmod -R 755 /var/www/html/

WORKDIR /var/www/html/
RUN composer install
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Kareem Essawy
  • 565
  • 1
  • 4
  • 14
0

You can try the ldb-dev alpine package.`

FROM php:7.2-fpm-alpine
RUN apk add --no-cache ldb-dev

PHP configure not finding LDAP header libraries

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • i tried, still says "configure: error: Cannot find ldap.h" – Kareem Essawy Jul 16 '20 at 12:16
  • did you checked that tag answer? it also suggested to add `ln -s /usr/lib/x86_64-linux-gnu/libldap.so /usr/lib/libldap.so \ && ln -s /usr/lib/x86_64-linux-gnu/liblber.so /usr/lib/liblber.so` – Adiii Jul 16 '20 at 12:37
-2

mac m2:

ldap --with-libdir=lib/aarch64-linux-gnu/
Andromeda
  • 1,205
  • 1
  • 14
  • 21