Dockerize Django-MongoDB Application with docker-compose
A Quick Start Example to run Django-MongoDB Application in Docker.
This module requires the following modules/libraries:
- Docker
- Any version of Linux or Unix OS (Recommended and Optional)
Install using the following the command,
docker-compose up -d --build
When a container is started for the first time, it will execute files with extensions .sh
, and .js
that are found in init_db
. .js
files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable in docker-compose.yml
, if it is present, or test
otherwise. You may also switch databases within the .js script.
Directory named data_db
will serve as a MongoDB data directory on the host system (outside the container). It is easy for tools and applications on the host system to access the MongoDB files from this directory. The data_db
directory from host system is mounted to /data/db
inside the container, where MongoDB by default will write its data files.
Using docker hierarchy in docker-compose.yml
, the services make-migration
and migration
runs after setting up the database but before the start of the main web
service. This docker configuration helps us to automatically run the django migration commands on the application once the database has been setup.
This example also creates a web-based user inteface for MongoDB called mongo-express through port 8081.
Known Issues:
- The default Docker setup on Windows uses a VirtualBox VM to host the Docker daemon. Unfortunately, the mechanism VirtualBox uses to share folders between the host system and the Docker container is not compatible with the memory mapped files used by MongoDB. This means that it is not possible to run a MongoDB container with the data directory mapped to the host on Windows OS.
- As a quick work around to run this example on windows machine please comment few lines of volume mapping in
docker-compose.yml
.
This example uses a popular open source project called Djongo - A Django and MongoDB database connector. Thanks to the creator for this wonderful contribution.
Reach out to me at one of the following places!
- Twitter at
@writetopradeep
- LinkedIn at
balanpradeepkumar