Setup a MongoDB + Python Docker Dev Stack
May 14, 2019MongoDB + Python
I only just recently jumped on the MongoDB bandwagon, but I really, really like it. To me, working with MongoDB feels so much more natural. It feels like working with code. MongoDB collections are arrays of objects, as opposed to a SQL DB where we kind of make it look like an object. Compared to SQL databases it is a much newer technology, but it is quickly gaining traction and becoming more and more robust.
With that said, it's nice to have a docker-compose template on hand to quickly spin up a MongoDB + Python Stack! That is exactly what we have here today. If you read my Setup a MySQL + Python Docker Dev Stack post, this will feel very similar.
MongoDB in Docker Compose
First of all, let's examine what just MongoDB looks like in docker compose. I'm going to go over only a very small portion of the potential configuration here. If you need more check out the official MongoDB docker image.
Environmental Variables
Here, I am setting the initial root username with MONGO_INITDB_ROOT_USERNAME and the root password with MONGO_INITDB_ROOT_PASSWORD. That's really it for this one. You need to add any data locally and import it with the mongoimport tool.
Now, I could spin this up as is putting the above docker-compose.yml file in a directory and running:
Once you start that you will be in a shell in the docker mongo container.
➜ connect-mongo docker-compose exec mongo bash
root@f65e7e65e52b:/# mongo
MongoDB shell version v4.0.8
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1f2b96f1-42bb-4599-af76-70e26f849ad8") }
MongoDB server version: 4.0.8
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
And that is how you know you are up and running!
Connect to the Database From a Python App
I am going to use my tried and true method here for building a python docker image. We can spin up a docker image for our python app by defining the packages we need in an environment.yml and building them. For more information on this process you can see my full tutorial at Develop a Python Flask App With Docker.
For this particular example we need a mysql connection library. I am using pymongo. I always add in ipython, so you'll see that there too.
Here is the Dockerfile for reference. I create a conda environment, called connect (see Line 1 in the environment.yml configuration) and source it.
FROM continuumio/miniconda3:4.5.11
RUN apt-get update -y; apt-get upgrade -y
RUN apt-get update -y; apt-get upgrade -y; apt-get install -y vim-tiny vim-athena ssh
COPY environment.yml environment.yml
RUN conda env create -f environment.yml
RUN echo "alias l='ls -lah'" >> ~/.bashrc
RUN echo "source activate connect" >> ~/.bashrc
ENV CONDA_EXE /opt/conda/bin/conda
ENV CONDA_PREFIX /opt/conda/envs/connect
ENV CONDA_PYTHON_EXE /opt/conda/bin/python
ENV CONDA_PROMPT_MODIFIER (connect)
ENV CONDA_DEFAULT_ENV connect
ENV PATH /opt/conda/envs/connect/bin:/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Add the Python App to the Docker Compose Stack
Simply add your python app, with the command 'tail -f /dev/null' to ensure it does not exit.
Restart your stack with your new configuration.
docker-compose restart
This time connect to your Python App with docker-compose exec python_app bash.
Wrap Up
There we go! You've seen how easy it is to spin up a database, and use python to connect to it and start querying.
Happy teching!