Django shell_plus:如何在 Docker 容器中访问 Jupyter notebook
Django shell_plus: How to access Jupyter notebook in Docker Container
我正在尝试从 Docker 容器中的 django-extensions 访问使用 shell_plus
命令创建的 Jupyter Notebook。
docker-compose -f local.yml run --rm django python manage.py shell_plus --notebook
我的配置基于@RobM 和@Mark Chackerian 对此 的回答。 IE。我安装并配置了一个自定义内核,我的 Django 应用程序配置文件将常量 NOTEBOOK_ARGUMENTS
设置为:
NOTEBOOK_ARGUMENTS = [
'--ip', '0.0.0.0',
'--port', '8888',
'--allow-root',
'--no-browser',
]
我可以在日志中看到容器成功启动:
[I 12:58:54.877 NotebookApp] The Jupyter Notebook is running at:
[I 12:58:54.877 NotebookApp] http://10d56bab37fc:8888/?token=b2678617ff4dcac7245d236b6302e57ba83a71cb6ea558c6
[I 12:58:54.877 NotebookApp] or http://127.0.0.1:8888/?token=b2678617ff4dcac7245d236b6302e57ba83a71cb6ea558c6
但是我打不开 url。我在 docker-compose
中转发了端口 8888,尝试使用 localhost
而不是 127.0.0.1
并且还尝试使用容器 IP w/o 成功。
感觉我在这里遗漏了明显的东西……感谢任何帮助。
它可以正常工作,但我不明白为什么会这样。在 docker-compose run
命令中公开端口就成功了。
docker-compose -f local.yml run --rm -p 8888:8888 django python manage.py shell_plus --notebook
我的印象是,在 local.yml
中公开端口也会在 run
启动的容器中打开它们。
为了截至 2020 年的记录,我设法在 docker-compose
中使用 Postgresql
进行了工作 django
设置:
development.py (settings.py)
INSTALLED_APPS += [
"django_extensions",
]
SHELL_PLUS = "ipython"
SHELL_PLUS_PRINT_SQL = True
NOTEBOOK_ARGUMENTS = [
"--ip",
"0.0.0.0",
"--port",
"8888",
"--allow-root",
"--no-browser",
]
IPYTHON_ARGUMENTS = [
"--ext",
"django_extensions.management.notebook_extension",
"--debug",
]
IPYTHON_KERNEL_DISPLAY_NAME = "Django Shell-Plus"
SHELL_PLUS_POST_IMPORTS = [ # extra things to import in notebook
("module1.submodule", ("func1", "func2", "class1", "etc")),
("module2.submodule", ("func1", "func2", "class1", "etc"))
]
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" # only use in development
requirements.txt
django-extensions
jupyter
notebook
Werkzeug # needed for runserver_plus
...
docker-compose.yml
version: "3"
services:
db:
image: postgres:13
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
restart: always
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data/
web:
build: .
environment:
- DJANGO_SETTINGS_MODULE=settings.development
command:
- scripts/startup.sh
volumes:
- ...
ports:
- "8000:8000" # webserver
- "8888:8888" # ipython notebook
depends_on:
- db
volumes:
postgres_data:
从您的主机终端 运行 此命令:
docker-compose exec web python manage.py shell_plus --notebook
最后在主机的网络浏览器中导航到 http://localhost:8888/?token=<xxxx>
。
如果你想像分离服务一样使用jupyter notebook:
jupyter_notebook:
build:
context: .
dockerfile: docker/dev/web/Dockerfile
command: python manage.py shell_plus --notebook
depends_on:
- web
ports:
- 8888:8888 # ipython notebook
env_file:
- .env
之后:
docker-compose logs -f 'jupyter_notebook'
您将在日志中获得访问令牌
compose 运行 命令默认不会公开定义的服务端口。来自 https://docs.docker.com/compose/reference/run/
的文档
The [...] difference is that the docker-compose run command does not
create any of the ports specified in the service configuration. This
prevents port collisions with already-open ports. If you do want the
service’s ports to be created and mapped to the host, specify the
--service-ports flag:
docker-compose run --service-ports web python manage.py shell
因此您需要运行
docker-compose -f local.yml run --rm --service-ports django python manage.py shell_plus --notebook
也可能是默认的 8888 端口已被本地 jupyter 服务器使用(例如,由 VS Code 的 jupyter notebook 实现启动。因此我通常映射到 settings.py NOTEBOOK_ARGUMENTS list. (此时compose文件中的端口映射也需要调整,当然,后台不能有另一个容器运行ning和这个服务定义相同也可能占用端口。)
我正在尝试从 Docker 容器中的 django-extensions 访问使用 shell_plus
命令创建的 Jupyter Notebook。
docker-compose -f local.yml run --rm django python manage.py shell_plus --notebook
我的配置基于@RobM 和@Mark Chackerian 对此NOTEBOOK_ARGUMENTS
设置为:
NOTEBOOK_ARGUMENTS = [
'--ip', '0.0.0.0',
'--port', '8888',
'--allow-root',
'--no-browser',
]
我可以在日志中看到容器成功启动:
[I 12:58:54.877 NotebookApp] The Jupyter Notebook is running at:
[I 12:58:54.877 NotebookApp] http://10d56bab37fc:8888/?token=b2678617ff4dcac7245d236b6302e57ba83a71cb6ea558c6
[I 12:58:54.877 NotebookApp] or http://127.0.0.1:8888/?token=b2678617ff4dcac7245d236b6302e57ba83a71cb6ea558c6
但是我打不开 url。我在 docker-compose
中转发了端口 8888,尝试使用 localhost
而不是 127.0.0.1
并且还尝试使用容器 IP w/o 成功。
感觉我在这里遗漏了明显的东西……感谢任何帮助。
它可以正常工作,但我不明白为什么会这样。在 docker-compose run
命令中公开端口就成功了。
docker-compose -f local.yml run --rm -p 8888:8888 django python manage.py shell_plus --notebook
我的印象是,在 local.yml
中公开端口也会在 run
启动的容器中打开它们。
为了截至 2020 年的记录,我设法在 docker-compose
中使用 Postgresql
进行了工作 django
设置:
development.py (settings.py)
INSTALLED_APPS += [
"django_extensions",
]
SHELL_PLUS = "ipython"
SHELL_PLUS_PRINT_SQL = True
NOTEBOOK_ARGUMENTS = [
"--ip",
"0.0.0.0",
"--port",
"8888",
"--allow-root",
"--no-browser",
]
IPYTHON_ARGUMENTS = [
"--ext",
"django_extensions.management.notebook_extension",
"--debug",
]
IPYTHON_KERNEL_DISPLAY_NAME = "Django Shell-Plus"
SHELL_PLUS_POST_IMPORTS = [ # extra things to import in notebook
("module1.submodule", ("func1", "func2", "class1", "etc")),
("module2.submodule", ("func1", "func2", "class1", "etc"))
]
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" # only use in development
requirements.txt
django-extensions
jupyter
notebook
Werkzeug # needed for runserver_plus
...
docker-compose.yml
version: "3"
services:
db:
image: postgres:13
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
restart: always
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data/
web:
build: .
environment:
- DJANGO_SETTINGS_MODULE=settings.development
command:
- scripts/startup.sh
volumes:
- ...
ports:
- "8000:8000" # webserver
- "8888:8888" # ipython notebook
depends_on:
- db
volumes:
postgres_data:
从您的主机终端 运行 此命令:
docker-compose exec web python manage.py shell_plus --notebook
最后在主机的网络浏览器中导航到 http://localhost:8888/?token=<xxxx>
。
如果你想像分离服务一样使用jupyter notebook:
jupyter_notebook:
build:
context: .
dockerfile: docker/dev/web/Dockerfile
command: python manage.py shell_plus --notebook
depends_on:
- web
ports:
- 8888:8888 # ipython notebook
env_file:
- .env
之后:
docker-compose logs -f 'jupyter_notebook'
您将在日志中获得访问令牌
compose 运行 命令默认不会公开定义的服务端口。来自 https://docs.docker.com/compose/reference/run/
的文档The [...] difference is that the docker-compose run command does not create any of the ports specified in the service configuration. This prevents port collisions with already-open ports. If you do want the service’s ports to be created and mapped to the host, specify the --service-ports flag:
docker-compose run --service-ports web python manage.py shell
因此您需要运行
docker-compose -f local.yml run --rm --service-ports django python manage.py shell_plus --notebook
也可能是默认的 8888 端口已被本地 jupyter 服务器使用(例如,由 VS Code 的 jupyter notebook 实现启动。因此我通常映射到 settings.py NOTEBOOK_ARGUMENTS list. (此时compose文件中的端口映射也需要调整,当然,后台不能有另一个容器运行ning和这个服务定义相同也可能占用端口。)