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 端口对应的外部端口。
相关推荐
金銀銅鐵2 小时前
[Python] 借助 turtle 逐字展示唐诗《金缕衣》
python
Python大数据分析@4 小时前
使用大模型MCP采集数据,爬虫已经无门槛
python·网络爬虫
quantdash_cc4 小时前
告别自建 Requests/BS4 网页爬虫:基于 QuantDash 搭建零维保的高性能量化行情流水线
开发语言·爬虫·python·pandas·量化·quantdash
65岁退休Coder5 小时前
LangChain v1.3.4 笔记 - 07 补充:链式调用 LCEL
后端·python·langchain
卷无止境5 小时前
FastAPI 部署在 Nginx 后面到底该怎么配
后端·python
半亩码田5 小时前
C#转Python第3.1篇:Python 的 class 没有访问修饰符?面向对象的另一条路
开发语言·python·c#
Wzx1980126 小时前
python沙箱和docker沙箱你选对了吗?
开发语言·python·docker
Python私教6 小时前
签名 URL 刷新导致审批失效?别急着删掉所有 query
python·安全
牧羊人.3336 小时前
Python 办公自动化从入门到入土|09 数据容器之字典
开发语言·python
Zane19946 小时前
类变量与实例变量:一个共享列表引发的线上事故
后端·python