JupyterNotebook-Docker版

简介: 制作jupyter notebook的docker镜像,兼容numpy, pandas, matplotlib, pyspark, opencv-python, requests, flask等组件。

制作前提:电脑安装并启动Docker Desktop
详细步骤如下:
(1)样式文件 custom.css

.CodeMirror pre {font-family: Consolas; font-size: 12pt;}
* {font-family: Consolas;}
div.output_area pre {font-family: Consolas; font-size: 12pt;}
div.input_prompt {font-family: Consolas; font-size: 12pt;}
div.out_prompt_overlay {font-family: Consolas; font-size: 12pt;}
div.prompt {font-family: Consolas; font-size: 12pt;}
span.cm-comment {font-family:  Consolas !important; font-style:normal !important; color:#BB3D00 !important;}
AI 代码解读

(2)镜像生成文件Dockerfile

FROM python:3.9.16

RUN python -m pip install --upgrade pip
RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
RUN pip install numpy
RUN pip install scipy
RUN pip install pillow
RUN pip install matplotlib
RUN pip install pandas
RUN pip install pyspark
RUN pip install psutil
RUN pip install virtualenv
RUN pip install virtualenvwrapper
RUN pip install opencv-python
RUN pip install requests
RUN pip install flask_wtf
RUN pip install flask-sqlalchemy
RUN pip install flask-mysqldb
RUN pip install scikit-image
RUN pip install notebook

# 创建用户
RUN useradd -ms /bin/bash yanghaitao

# 设置默认目录
RUN jupyter notebook --generate-config
RUN echo "c.NotebookApp.notebook_dir='/home/yanghaitao'" >> /root/.jupyter/jupyter_notebook_config.py

# 设置默认的字体
COPY custom.css /root/.jupyter/custom/
COPY custom.css /root/.jupyter/

EXPOSE 8888
CMD jupyter notebook --ip 0.0.0.0 --port 8888 --allow-root --no-browser
AI 代码解读

(3)构建命令
将两个文件放在同一目录下,运行构建命令

docker build -t my-jupyter-notebook:latest .
AI 代码解读

需要等待较长时间
(4)查看镜像

docker images
AI 代码解读

(5)window端测试运行命令

docker run -p 8888:8888 my-jupyter-notebook:latest
AI 代码解读

(6)镜像下载为本地文件

# 将镜像打包为本地文件
# 指令:docker save 镜像id > 文件名.tar   缺点:load之后没有tag
# docker save -o my-jupyter-notebook.tar my-jupyter-notebook:latest
docker save ee2a262aa724 > my-jupyter-notebook.tar
AI 代码解读

大约2.7G,上传到目标服务器。

(7)将镜像文件加载到docker中

# 将本地文件加载为镜像
# 指令:docker load < 文件名.tar
docker load < my-jupyter-notebook.tar
AI 代码解读

如果根据镜像ID导出为文件,加载的镜像没有tag信息,可以重命名

# 镜像重命名
# docker tag  旧镜像名  新镜像名
# docker tag  镜像id  仓库:标签
docker tag ee2a262aa724 my-jupyter-notebook:latest
AI 代码解读

(8)挂载数据卷
为了能够保存jupyter notebook的文档,必须将docker中的文档保存到linux服务器上,不然一旦docker容器被删掉,文档就不在了。启动命令如下:

# 挂载数据卷
# -v 宿主机的绝对路径:容器内路径
docker run -p 8888:8888 
    -v /opt/jupyter/notebook:/home/yanghaitao
       -d my-jupyter-notebook:latest
AI 代码解读

(9)浏览器访问Notebook
http://ip:8888/
(10)获取token

# 进入容器
docker exec -it 92585929f745 /bin/bash
# 获取token
jupyter notebook list
AI 代码解读
目录
打赏
0
0
0
0
52
分享
相关文章
|
9月前
|
Docker介绍
Docker介绍
227 0
一文让你了解Docker的前世今生
一文让你了解Docker的前世今生
160 0
为什么会是Docker?
最近Docker容器化部署特别火热,那为什么说Docker是下一代的容器工具?回答这个问题之前,我们就服务器的历史演变来简单说明一下。
为什么会是Docker?
5 分钟带你看懂 Docker
什么是Docker? 打开翻译君输入Docker 结果显示码头工人,没错!码头工人搬运的是集装箱,那么今天要讲的Docker其操作的也是集装箱,这个集装箱就静态而言就是一个应用镜像文件,就动态而言,就是一个容器。
4311 0
认识docker
一、Docker工作原理 二、Docker容器和虚拟机对比 三、镜像容器管理 1、Docker关键组件 2、Docker架构 3、Docker内部组件 镜像(Image)——一个特殊的文件系统Docker 镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等) 容器(Container)——镜像运行时的实体容器是镜像运行时的实体。
1575 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等