I had a web dashboard that I needed to authenticate using ldap. For that I wanted to go with python-ldap package.
I faced few challenges in deploying to my Docker image since it required source code build on deployment, and there are no official binaries. That meant that if I want to deploy python-ldap on a production server / container; I have to install development packages as described in the Build Prerequisites section of their documentation.
For development purposes, this would have been fine, but I preferred not to have those unnecessary packages in my deployed production container. The other option was to deploy prebuilt packages. I also preferred not to do that.
The solution was simple though. I simply built the wheel inside one stage of my docker image, then just copied this wheel file to the next stage, to install it directly using pip.
Below is my docker file.
FROM python:3.9.2 AS ldap-build RUN apt-get update -y && \ apt-get install -y libsasl2-dev python-dev libldap2-dev libssl-dev && \ python -m pip wheel --wheel-dir=/tmp python-ldap==3.3.1 FROM python:3.9.2 COPY --from=ldap-build /tmp/*.whl /tmp RUN python -m pip install /tmp/*.whl CMD tail -f /dev/null
While being a simple solution, I was a bit surprised I didn't find a ready made solution like this. I thought it's worth sharing for python-ldap deployments in production, and in any similar case, where you have to build python packages yourself.
I hope it helps somebody out there.