制作前提:电脑安装并启动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;}
(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
(3)构建命令
将两个文件放在同一目录下,运行构建命令
docker build -t my-jupyter-notebook:latest .
需要等待较长时间
(4)查看镜像
docker images
(5)window端测试运行命令
docker run -p 8888:8888 my-jupyter-notebook:latest
(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
大约2.7G,上传到目标服务器。
(7)将镜像文件加载到docker中
# 将本地文件加载为镜像
# 指令:docker load < 文件名.tar
docker load < my-jupyter-notebook.tar
如果根据镜像ID导出为文件,加载的镜像没有tag信息,可以重命名
# 镜像重命名
# docker tag 旧镜像名 新镜像名
# docker tag 镜像id 仓库:标签
docker tag ee2a262aa724 my-jupyter-notebook:latest
(8)挂载数据卷
为了能够保存jupyter notebook的文档,必须将docker中的文档保存到linux服务器上,不然一旦docker容器被删掉,文档就不在了。启动命令如下:
# 挂载数据卷
# -v 宿主机的绝对路径:容器内路径
docker run -p 8888:8888
-v /opt/jupyter/notebook:/home/yanghaitao
-d my-jupyter-notebook:latest
(9)浏览器访问Notebook
http://ip:8888/
(10)获取token
# 进入容器
docker exec -it 92585929f745 /bin/bash
# 获取token
jupyter notebook list