IntroductionThis tutorial walks through
- Usage of docker-compose (v2 syntax) with the data volume persisted on the host (e.g. your laptop).
- A python project created using cookiecutter
- and the Python/Mongo driver specified in the requirements.txt file for controlled and repeatable installation
- Inserting and Querying data to and from Mongo
- also terminating and re-starting the docker instance to demonstrate persistence beyond the container lifecycle
EnvironmentAt the time of this article, I'm using
- OS X 10.11.6
- Docker version 1.12.2-rc1-beta27 (build: 12496)
- Python 2.7.12 |Anaconda 4.1.1 (x86_64)| (default, Jul 2 2016, 17:43:17)
Mongo in DockerCopy this into docker-compose.yml
Note that the use of docker-compose and a standard mongo image. We do not have to create/edit an existing Dockerfile. It's best to use trusted images. Also note the path for /volumes.
Copy this into run.sh
We will use this script to gracefully launch docker-compose.
The Python ProjectI prefer to use cookiecutter to create new Python projects.
For developers with a JEE background, this is similar to using the Maven Archetype Generator (shown here for reference):
After stepping through the cookiecutter questionnaire, I replace the contents of "requirements.txt" with
I use this command to install the python project:
The Python CodeThe python code looks like this:
The docker instance can be terminated. When launched again, the data inserted in the first session will still be present.