Docker OpenLDAP Container
Are you looking to set up a OpenLDAP container using Docker on your MacOS machine? Look no further! In this article, we'll walk you through the process of creating and configuring a Docker OpenLDAP container, including setting environment variables, starting the container, and testing the LDAP server.
Step 1: Create an Environment File
The first step is to create an environment file that will store our admin password.
.env
DB_PASSWORD=Password1234
This file will be used to set the LDAP_ADMIN_PASSWORD
environment variable in our Docker container.
Step 2: Create a Docker Compose File
Next, we'll create a Docker compose file that defines our OpenLDAP container.
openldap-docker-compose.yml
version: '3.9'
services:
openldap:
# Apple M1 Chip
# platform: linux/amd64
image: bitnami/openldap:2.6
container_name: openldap
restart: always
env_file:
- .env
environment:
LDAP_ROOT: dc=appdev24,dc=com
LDAP_ADMIN_USERNAME: admin
LDAP_ADMIN_PASSWORD: $DB_PASSWORD
ports:
- 389:1389
- 636:1636
volumes:
- openldap_datadir:/bitnami/openldap/
networks:
- openldap-network
networks:
openldap-network:
driver: bridge
volumes:
openldap_datadir:
This file defines a single service, openldap, which uses the bitnami/openldap 2.6 image and maps port 389 on your local machine to port 1389 in the container.
Step 3: Start the Container
Now that we have our environment file and Docker compose file set up, it's time to start the container! Run the following command:
docker-compose -f openldap-docker-compose.yml up -d
This will start the container in detached mode, meaning it will run in the background.
Step4: Test OpenLDAP Container
Now we will try to connect to OpenLDAP Server.
Note: you should have OpenLDAP command line tools ldapwhoami, ldapadd, ldapsearch, ldapdelete installed locally.
The ldapwhoami command is used to authenticate users.
db_password=Password1234
ldapwhoami -vvv -H ldap://localhost -D "cn=admin,dc=appdev24,dc=com" -x -w ${db_password}
Now we will create a user. For that let's create a LDAP data interchange format file.
developer.ldif
dn: cn=developer,dc=appdev24,dc=com
changetype: add
objectclass: inetOrgPerson
cn: developer
givenname: developer
sn: Developer
displayname: Developer User
mail: developer@appdev24.com
userpassword: Password1234
OpenLDAP provides ldapadd command to add records to the OpenLDAP directory.
ldapadd -x -H ldap://localhost -D "cn=admin,dc=appdev24,dc=com" -w ${db_password} -f developer.ldif
Now we will search the user. ldapsearch command is used to search the records in the OpenLDAP directory service.
ldapsearch -x -H ldap://localhost -D "cn=admin,dc=appdev24,dc=com" -w ${db_password} -b "cn=developer,dc=appdev24,dc=com"
Now we will delete the user. The ldapdelete command is used to delete an existing LDAP records.
ldapdelete -x -H ldap://localhost -D "cn=admin,dc=appdev24,dc=com" -w ${db_password} "cn=developer,dc=appdev24,dc=com"
ldapsearch -x -H ldap://localhost -D "cn=admin,dc=appdev24,dc=com" -w ${db_password} -b "cn=developer,dc=appdev24,dc=com"
Step 5: Stop the Container
Finally, we can stop the container by running the following command:
docker-compose -f openldap-docker-compose.yml down
This will stop the container and remove it from memory.
That's it! We've successfully created and configured a Docker OpenLDAP container.