djangodockerjupyter-notebookdjango-extensions

Docker + django_extensions + jupyter: OSError: [Errno 99] Cannot assign requested address


I am trying to set up jupyter inside a container that contains a Django app. I am using django-extensions in order to take advantage of the shell_plus command. However, when I run it:

docker-compose exec app python manage.py shell_plus --notebook

I am getting the following error:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.8/site-packages/django_extensions/management/commands/shell_plus.py", line 125, in run_from_argv
    return super(Command, self).run_from_argv(argv)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.8/site-packages/django_extensions/management/utils.py", line 62, in inner
    ret = func(self, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/django_extensions/management/commands/shell_plus.py", line 517, in handle
    shell()
  File "/usr/local/lib/python3.8/site-packages/django_extensions/management/commands/shell_plus.py", line 253, in run_notebook
    app.initialize(notebook_arguments)
  File "<decorator-gen-117>", line 2, in initialize
  File "/usr/local/lib/python3.8/site-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/notebook/notebookapp.py", line 1769, in initialize
    self.init_webapp()
  File "/usr/local/lib/python3.8/site-packages/notebook/notebookapp.py", line 1490, in init_webapp
    self.http_server.listen(port, self.ip)
  File "/usr/local/lib/python3.8/site-packages/tornado/tcpserver.py", line 151, in listen
    sockets = bind_sockets(port, address=address)
  File "/usr/local/lib/python3.8/site-packages/tornado/netutil.py", line 174, in bind_sockets
    sock.bind(sockaddr)
OSError: [Errno 99] Cannot assign requested address

Solution

  • I was able to figure it out. It is needed to pass some additional notebook arguments for django-extensions using NOTEBOOK_KERNEL_SPEC_NAMES:

    NOTEBOOK_ARGUMENTS = [
        "--ip", "0.0.0.0",
        "--port", "8888",
        "--allow-root"
    ]
    

    You also need to add DJANGO_ALLOW_ASYNC_UNSAFE to your environment variables with value True. Otherwise, you will get an error while trying to perform queries using Django ORM.

    In addition, make sure to expose the port in the container. In my case, I am using a docker-compose.yml, so it will be:

    ports:
      - 8000:8000
      - 8888:8888 # this is the port at which notebook runs
    

    After setting that, running the following command will work like a charm:

    docker-compose exec app python manage.py shell_plus --notebook