Running Django-Neomodel Within a Docker Container
Here’s your quickstart guide for posterity, using the example in Django-Neomodel.
A Neomodel community member asked about how to run Django Neomodel (a Neomodel plugin for Django) within a Docker container. So let’s show how to do it.
Haven’t tried out Django-Neomodel yet? Start here.
Local Setup — Example
Prerequisites:
- An updated version of Docker
- Django Neomodel
The Django-Neomodel repository comes with a Docker example. If you’re just getting started with Docker, this may be a good place to start.
From the root of the Django-Neomodel repo, go to the tests directory:
cd tests/
Run the Docker Command (make sure your Docker service is running and up to date).
docker-compose up
Verify the Django admin is running: http://localhost:8000/admin/
Verify Neo4j Browser is running: http://localhost:7474/browser/
You’ll be then able to log in with the test credentials admin
/ 1234
.
Under the Hood
Dockerfile
The Dockerfile collects the commands Docker will be using to create your environment.
FROM python:3
WORKDIR /usr/src/app
COPY ./tests/requirements.txt ./
RUN pip install — no-cache-dir -r requirements.txt
COPY .. .
RUN chmod +x ./tests/docker-entrypoint.sh
CMD [“./tests/docker-entrypoint.sh” ]
docker-compose.yml
docker-compose.yml
is where you’ll configure the ports you’ll be using to communicate with your Neo4j database.
version: ‘3’
services:
backend:
build:
context: ../
dockerfile: ./tests/Dockerfile
command: ‘/bin/bash -c “chmod +x /usr/src/app/tests/docker-entrypoint.sh && /usr/src/app/tests/docker-entrypoint.sh”’
volumes:
- ..:/usr/src/app
ports:
- “8000:8000”
expose:
- 8000
depends_on:
- neo4j_db
links:
- neo4j_db
environment:
- NEO4J_BOLT_URL=bolt://neo4j:foobar@neo4j_db:7687
- DJANGO_SETTINGS_MODULE=settingsneo4j_db:
image: neo4j:4.2-enterprise
ports:
- “7474:7474”
- “7687:7687”
expose:
- 7474
- 7687
volumes:
- db:/data/dbms
environment:
- NEO4J_AUTH=neo4j/foobar
- NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
- dbms.connector.bolt.listen_address=:7687
- dbms.connector.bolt.advertised_address=:7687volumes:
db:
docker-entrypoint.sh
docker-entrypoint.sh
is where you’ll configure the python/Django portion of your app. You’ll add your DJANGO_SUPERUSER_USERAME
and other Django-related environment variables here.
#!/bin/bash -xe
cd tests
python -c <<EOF |
from django.db import IntegrityError
try:
python manage.py install_labels
except IntegrityError:
print(“Already installed”)
EOFpython manage.py migrate # Apply database migrations
if [ “$DJANGO_SUPERUSER_USERNAME” ]
then
python manage.py createsuperuser \
— noinput \
— username $DJANGO_SUPERUSER_USERNAME \
— email $DJANGO_SUPERUSER_EMAIL
fi$@
python manage.py runserver 0.0.0.0:8000
Resources
About the Authors
Cristina and Alisson work at The SilverLogic, a software development company based in Boca Raton.
Alisson is a software engineer at The SilverLogic. Passionate about plants, he is the driving force behind Que Planta, a GraphQL-based social network for plants.