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.

Photo by frank mckenna on Unsplash

Haven’t tried out Django-Neomodel yet? Start here.

Neo4j for Django Developers

Local Setup — Example

Prerequisites:

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: https://localhost:8000/admin/

Verify Neo4j Browser is running: https://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.

docker-compose.yml :

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=settings
neo4j_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=:7687
volumes:
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.

docker-entrypoint.sh :

#!/bin/bash -xe
cd tests
python -c <<EOF |
from django.db import IntegrityError
try:
python manage.py install_labels
except IntegrityError:
print(“Already installed”)
EOF
python 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



Neo4j Online Developer Expo and Summit is back for 2021.

Register for NODES 2021 today and enjoy the talks from experienced graph developers.


Save My Spot

Running Django-Neomodel within a Docker Container was originally published in Neo4j Developer Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.