Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rbeucher/a995d8e224193d2404001ca0b90c9fd2 to your computer and use it in GitHub Desktop.
Save rbeucher/a995d8e224193d2404001ca0b90c9fd2 to your computer and use it in GitHub Desktop.
Guide to setup Django-shell Jupyter notebook running in Docker container

Setting Up Django-Shell Jupyter Notebook in Docker Container

This guide assumes that your django application already running in the container.

1. Install required python packages

You will have to make sure that django-extensions and jupyter packages are installed in the container's python virtual environment.

If you need to shell into the container as root user to do this, you can do so from the host PC:

docker exec -it --user root <container_name> /bin/bash

Then the actual package installation:

pip install django-extensions jupyter

2. Edit django settings file

We need to supply the address 0.0.0.0 as jupyter notebook ip parameter. But since the server will be run via django-extensions plugin, this needs to be done in django settings. To find out which settings module is used by the container you can look up the DJANGO_SETTINGS_MODULE environment variable:

echo $DJANGO_SETTINGS_MODULE

And add the following setting to the module it refers to (you can adjust the port parameter value as needed):

NOTEBOOK_ARGUMENTS = [
    '--ip', '0.0.0.0',
    '--port', '8888'
]

3. Restart the container and find its IP address

Exit from the container and restart it from the host PC:

docker restart <container_name>

Then find out its IP address:

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name>

4. Start jupyter notebook server via django management command

First, shell into the container:

docker exec -it <container_name> /bin/bash

Then navigate into the directory where manage.py module is located. And run jupyter notebook from django management command:

./manage.py shell_plus --notebook

During launch jupyter notebook server will show the address and port and token that you will need to use as URL to access it.

5. Access the server from host

You need to replace the IP adress of the URL with the IP address of the container. Open your browser and point to the URL:

http://<ip_address>:8888/?token=<token_value>

You can create new django-shell notebook from the New drop down button on the top right corner of the jupyter notebook server homepage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment