How to load LDAP docker container data on startup

I want to have a LDAP server on a docker container, I already used dinkel/openldap, osixia/openldap and muzili/ldap docker images and so far connection and first configuration is ok.

My problem is: although I mounted container’s /var/lib/ldap & /etc/ldap, there is always a backup needed before stoping, killing or removing(safe removing, docker rm without -v) the ldap container or there would be data loss. And it doesn’t seem to go through all config files because it also need a data import at start.

  • Build Docker image only if Code Repository has been updated (Jenkins)
  • nohup a process in background in Docker at K8S ,will led to that process run multi time,why's that?
  • Docker HAProxy SSL termination with Letsencrypt
  • How to Dockerize a tomcat app
  • How to use docker python SDK in swarm context?
  • Can't resolve hostnames between docker containers
  • I think I may going through some problem with docker container and their volume mounting but every else container mounting is a success and it only seem to have a problem with ldap containers.

    Is there any solution to make it work?(config at startup by reading from mounted folders and not losing data?)

  • Can I use docker for installing ubuntu on a Mac?
  • cannot access Docker remote registry
  • Restart docker with different environment variable
  • How to cache node_modules on Docker build?
  • Running Docker and Jekyll together in a same ec2 instance
  • Settings up remote interpretor in pycharm for docker
  • 2 Solutions collect form web for “How to load LDAP docker container data on startup”

    you can start another container to backup the folders first.

    Suppose the ldap container named ldap, it has two volumes: /var/lib/ldap & /etc/ldap, with below command, you can backup them easily.

    docker run --volumes-from ldap -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar  /var/lib/ldap  /etc/ldap

    If you need input some config when run a new container, think to update Dockerfile with ONBUILD command, such as:

    ONBUILD cp . /etc/ldap
    ONBUILD COMMAND you'd like to run

    With above code, your image can be stay as same (no re-build required), but when you start up the container, it will input the setting to /etc/ldap when start the container, and get some commands run after that.


    Managing data in containers

    Dockerfile reference


    To clear the solution:
    Like answered by BMW ldap container has 2 volumes, which was removed when mounted to empty folder in mounting section of ansible role docker module.
    So I first run a non-mounted ldap container and backup it’s volumes as suggested by BMW. Then kill and remove it and run a whole new container on backed up data. Then for user data configuration I run another ldap container(from same image) which only has to register users’ data from a config file.

    Final Ansible role code:

    - name: run temporary ldap container
        image: muzili/ldap
        name: temporary-ldap
        hostname: temporary-ldap
        state: restarted
        ports: 389:389
          SLAPD_PASSWORD: ******
    - name: ldap data copy container
        image: ubuntu
        name: backup_agent
        state: started
        - /backup
        - temporary-ldap
        command: tar cvf /backup/backup.tar  /var/lib/ldap  /etc/ldap
    - name: copy compressed data from backup_agent
      command: /usr/bin/docker cp backup_agent:/backup/backup.tar "{{base_dir}}/ldap/import"
    - name: extract ldap configuration data
        copy: "no"
        src: "{{base_dir}}/ldap/import/backup.tar"
        dest: "{{base_dir}}/ldap"
    - name: kill temporary ldap container
        image: muzili/ldap
        name: temporary-ldap
        state: absent
    - name: run main ldap container
        image: muzili/ldap
        name: ldap-server
        hostname: ldap-server
        state: running
        ports: 389:389
          SLAPD_PASSWORD: ******
        - "{{base_dir}}/ldap/etc/ldap:/etc/ldap"
        - "{{base_dir}}/ldap/var/lib/ldap:/var/lib/ldap"
    - name: wait for container to start
        port: 389
        delay: 5
    - name: copy ldap data configuration file
        src: conf/
        dest: "{{base_dir}}/ldap/import/conf"
    - name: run ldap-importer container
        image: muzili/ldap
        name: ldap-importer
        hostname: ldap-importer
        state: started
        - "{{base_dir}}/ldap/import/conf:/etc/ldap/conf"
        command: "ldapadd -h ldap-server -c -x -D \"cn=admin,dc=dev,dc=domain,dc=com\" -w ****** -f /etc/ldap/conf/data.ldif"
        - "ldap-server"
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.