This is a simple example of Python Flask and MongoDB usage. The Database Management System is MongoDB. The python-MongoDB connector is PyMongo. Frontend app is Nginx used as a reverse proxy.
- Machine with docker and docker-compose installed (Linux is preferred)
Clone this repo From the repo root directory run
$ source .env
$ docker-compose up -d
It will pull all the relevant images, configure the needed networks, volumes and bring up all the services.
To change the images version edit the .env file and set it to the wanted version.
The docker-compose does not create the user that will be used by the flask app. To create a new user, first start an interactive shell on the mongodb container:
$ docker exec -it mongodb bash
Once inside the container, log in to the MongoDB root administrative account:
# mongo -u mongodbuser -p
You will be prompted for the password that you entered as the value for the MONGO_INITDB_ROOT_PASSWORD variable in the docker-compose.yml file. The password can be changed by setting a new value for the MONGO_INITDB_ROOT_PASSWORD in the mongodb service, in which case you will have to re-run the docker-compose up -d command.
Execute the use command to switch to the flaskdb database:
mongodb> use flaskdb
Next, create a new user that will be allowed to access this database:
mongodb> db.createUser({user: 'flaskuser', pwd: 'your password', roles: [{role: 'readWrite', db: 'flaskdb'}]})
mongodb> exit
This command creates a user named flaskuser with readWrite access to the flaskdb database.
Log in to the authenticated database with the following command:
# mongo -u flaskuser -p your password --authenticationDatabase flaskdb
Now that you have added the user, log out of the database.
mongodb> exit
And finally, exit the container:
# exit
Now that your services are configured and running, you can test your application by navigating to http://your_server_ip in a browser.
- Refine the search output
- Create init script for MongoDB to create users as part of
docker-compose upprocess.