NGC容器中快速搭建Jupyter环境

本文将介绍如下内容:

  • 一、搭建 Docker Container 环境
  • 二、配置 Jupyter
  • 三、访问 Jupyter 页面并后台运行服务

一、搭建 Docker Container 环境

1、拉取 Docker Image

NVIDAI NGC CONTAINER

python 复制代码
# 1. 进入 NVIDAI NGC CONTAINER,检索。Eg: Pytorch Tag
# NVIDAI NGC CONTAINER: https://catalog.ngc.nvidia.com/containers

# 2. 拉取 Docker Container
docker pull nvcr.io/nvidia/pytorch:24.10-py3
2、创建 Docker Container

vim create_docker_container.sh

bash 复制代码
#!/bin/bash

# script parameters
docker_run_name=$1
docker_image_name=$2
docker_image_version=$3
ssh_port=$4
jupyter_port=$5
tensorboard=$6

# docker run --gpus all -itd \
docker run --gpus all --cpus 24 --shm-size 16G --memory 50gb -itd \
  -p ${ssh_port}:22 \
  -p ${jupyter_port}:8888 \
  -p ${tensorboard}:6006 \
  -v /etc/localtime:/etc/localtime \
  -v /home/ai:/home/ai \
  --name ${docker_run_name} \
  -e JUPYTER_PASSWORD="123456" \
  -e JUPYTER_PORT=8888 \
  -e ROOT_PASS="123456" \
  ${docker_image_name}:${docker_image_version}

# bash create_generate_train_container.sh generate_pytorch_2410  nvcr.io/nvidia/pytorch  24.10-py3 20010 20011 20012

bash create_generate_train_container.sh generate_pytorch_2410 nvcr.io/nvidia/pytorch 24.10-py3 20010 20011 20012

二、配置 Jupyter

1、方法一
bash 复制代码
# 1. 安装jupyter
pip install jupyter

# 2. 输入以下命令即可设置密码
jupyter notebook password
2、方法二
bash 复制代码
#安装jupyter
pip install jupyter

#生成jupyter配置文件,这个会生成配置文件.jupyter/jupyter_notebook_config.py
jupyter notebook --generate-config

#使用python交互窗口生成密码
In [1]: from notebook.auth import passwd # 或替换为: from jupyter_server.auth import passwd
In [2]: passwd()
Enter password: 
Verify password: 
Out[2]: 'sha1:******'

#去配置文件.jupyter/jupyter_notebook_config.py中修改以下参数
c.NotebookApp.ip='*'                          #绑定所有地址
c.NotebookApp.password = u'刚才生成的密码'
c.NotebookApp.open_browser = False            #启动后是否在浏览器中自动打开
c.NotebookApp.port =8888                      #指定一个访问端口,默认8888,注意和映射的docker端口对应

三、后台运行服务并访问 Jupyter 页面

1、后台运行服务
bash 复制代码
# 启动 jupyter 服务
jupyter notebook --allow-root --notebook-dir=/home/ai
# 注意: --notebook-dir 为 jupyter 启动目录

# 后台 nohup 运行
nohup jupyter notebook --allow-root --notebook-dir=/home/ai >/dev/null 2>&1 & 
2、访问 Jupyter 页面
bash 复制代码
# 在浏览器中访问 http://127.0.0.1:20011/tree?

或

# 在浏览器中访问 http://127.0.0.1:20011/lab?

注意: 20011 为创建 Docker Container 时 jupyter 服务 8888 端口对应的外部端口。
相关推荐
Ray Liang1 分钟前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮17 分钟前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling20 分钟前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮3 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽4 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健19 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞21 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
曲幽1 天前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程1 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python