Syntax issue with adding creating a mongodb and user with roles

I’ve started building my first docker container for a mongodb with auth.

I have a python script that fetches credentials from our password safe and makes them available for a bash script which then sets up the admin db and users.

  • How do I make a Docker container start automatically on system boot?
  • How to downloaded from remote location and execute script on container start Docker?
  • How to Increase the Speed of Docker Builds While Using Mounted Volumes
  • How can I set $PS1 with Dockerfile?
  • Docker with multiple exposed ports
  • Restarting Play application Docker container results in 'This application is already running' - RUNNING_PID is not deleted
  • I use a RUN command for the python script and that is fine.

    The bash script run.sh runs perfectly when I log into the container and run it.

    However, it should run as part of a CMD surely?

    Dockerfile extract:

    ADD set_mongo_passwords.py /
    ADD run.sh /
    
    RUN chmod +x /run.sh
    RUN chmod +x /set_mongo_passwords.py
    
    RUN python /set_mongo_passwords.py <user> <password>
    CMD ["/bin/bash","/run.sh"]
    

    Bash script

    #!/bin/sh
    
    mongod --auth
    
    source mongodb_credentials.txt
    
    # Wait for MongoDB to boot
    RET=1
    while [[ RET -ne 0 ]]; do
        echo "=> Waiting for confirmation of MongoDB service startup..."
        sleep 5
        mongo admin --eval "help" >/dev/null 2>&1
        RET=$?
    done
    
    echo "=> Creating admin user"
    mongo admin --eval "db.createUser({user: '$MONGO_ADMIN_USER', pwd: '$MONGO_ADMIN_PASSWORD', roles:[{role:'userAdminAnyDatabase',db:'admin'}]});"
    
    sleep 3
    
    echo "=> Creating IawApi user"
    mongo admin -u $MONGO_ADMIN_USER -p $MONGO_ADMIN_PASSWORD --eval "db.createUser({user: '$MONGO_IAWAPI_USER', pwd: '$MONGO_IAWAPI_PASSWORD', roles:[{role:'dbOwner',db:'IawApi'}]});"
    
    echo "=> Creating vpnDebugger user"
    mongo admin -u $MONGO_ADMIN_USER -p $MONGO_ADMIN_PASSWORD --eval "db.createUser({user: '$MONGO_VPNDEBUGGER_USER', pwd: '$MONGO_VPNDEBUGGER_PASSWORD', roles:[{role:'dbOwner',db:'vpnDebugger'}]});"
    
    echo "Done!"
    
    # delete anything that can be used to gain access to credentials
    
    rm -f mongodb_credentials.txt
    rm -f set_mongo_passwords.py
    

  • docker swarm 1.2.0 reschedule with port mapping
  • How to isolate memory leak of .NET application running on mono on docker in Mesos
  • docker cp the content of a folder
  • Rails controller caching when using docker container (dev)
  • Can't get classpath resource with spring ResourceUtils or DefaultResourceLoader in Docker env
  • Slapd with back-sql in docker only retrieves attribute mapping for one object class
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.