在Docker容器中配置Jupyter Notebook,主要目的是编写Python代码,特别是针对深度学习的开发场景。
Jupyter以Web形式提供最高效的使用体验,将其部署在云端最为适宜,无论是CPU还是GPU云服务器,均可快速启动并投入使用。
Docker技术的出现极大地简化了部署和使用流程。
- 安装 Docker
Docker分为企业版(EE)和社区版(CE),通常推荐使用Docker CE。
Docker支持Linux(如Ubuntu、CentOS)、MacOS、Windows以及树莓派等平台。鉴于主要在Linux环境下使用,且个人偏好Ubuntu系统,以下将介绍在Ubuntu下的安装步骤。
首先,需移除本机可能存在的旧版本Docker:
$sudo apt-get remove docker docker-engine docker.io
安装可选内核模块
为减少内核软件包的安装体积,自Ubuntu 14.04起,部分内核模块被移至可选内核模块包(linux-image-extra-*)。正常安装的系统通常包含此包,但裁剪后的系统可能会精简掉它。AUFS内核驱动作为推荐的Docker存储层驱动,属于可选内核模块的一部分,建议安装该包以使用AUFS。
$sudo apt-get update
$ sudo apt-get install linux-image-extra-$(uname -r) linux-image-extra-virtual
对于Ubuntu 16.04及以上版本的系统,Docker CE默认使用overlay2存储层驱动,无需手动配置。
证书及密钥准备
由于APT源使用HTTPS以确保软件下载过程中不被篡改,因此首先需要添加支持HTTPS传输的软件包以及CA证书。
$ sudo apt-get update
$ sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
考虑到国内网络环境,强烈建议使用国内源,官方源的配置方法已在注释中说明。
$ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# 官方源
# $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
最后添加Docker软件源:
$ sudo add-apt-repository \
"deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \
$(lsb_release -cs) \
stable"
# 官方源
# $ sudo add-apt-repository \
# "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
# $(lsb_release -cs) \
# stable"
上述命令会添加稳定版本的Docker CE APT镜像源。如需测试或每日构建版本,请将stable替换为test或nightly。
安装 Docker CE
$ sudo apt-get update
$ sudo apt-get install docker-ce
启动 Docker CE
$ sudo systemctl enable docker
$ sudo systemctl start docker
若使用Ubuntu 14.04,请使用以下命令启动:
$ sudo service docker start
建立 docker 用户组
出于安全考虑,Linux系统通常不直接使用root用户。更好的做法是将需要使用Docker的用户加入docker用户组。
$ sudo groupadd docker
$ sudo usermod -aG docker $USER
测试 Docker 是否安装正确
$ docker run hello-world
拉取hello-world镜像进行测试,
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
若能正常输出以上信息,则说明安装成功。
镜像加速器
在国内从Docker Hub拉取镜像有时会遇到困难,此时可配置镜像加速器。Docker官方及阿里云、七牛云等国内云服务商均提供了加速器服务。
针对ubuntu 14.04系统:
编辑 /etc/default/docker 文件,在DOCKER_OPTS中配置加速器地址:
DOCKER_OPTS="--registry-mirror=https://registry.docker-cn.com"
然后重新启动服务:
$ sudo service docker restart
针对ubuntu 16.04+系统:
请在 /etc/docker/daemon.json 中写入如下内容(若文件不存在请新建):
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
之后重新启动服务:
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker
检查加速器是否生效
命令行执行 docker info,若结果中看到相关配置内容,说明配置成功。
Registry Mirrors:
https://registry.docker-cn.com/
- 常见docker命令
在使用Docker命令前,需区分镜像和容器的概念,建议参考相关连接。
同一镜像可启动多个Docker容器,这些容器启动后均为活动状态且相互隔离。若在某个容器中进行操作后希望保留现有系统环境,需进行提交保存。
启动命令:docker run
例如,下面的命令输出一个 “Hello World”,随后终止容器。
$ docker run ubuntu:18.04 /bin/echo 'Hello world'
Hello world
这跟在本地直接执行 /bin/echo ‘hello world' 几乎感觉不出任何区别。
下面的命令则启动一个 bash 终端,允许用户进行交互。
$ docker run -it ubuntu:16.04 /bin/bash
root@af8bae53bdd3:/#
其中-i 让容器的标准输入保持打开,-t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入。
容器的状态主要有:
created:已经被创建 (使用 docker ps -a 命令可以列出)但是还没有被启动 (使用 docker ps 命令还无法列出)
running:运行中
paused:容器的进程被暂停了
restarting:容器的进程正在重启过程中
exited:上图中的 stopped 状态,表示容器之前运行过但是现在处于停止状态(要区别于 created 状态,它是指一个新创出的尚未运行过的容器)。可以通过 start 命令使其重新进入 running 状态
destroyed:容器被删除了,再也不存在了
查看当前系统拉取的镜像:
$docker images
查看当前系统下所有启动(Up状态)的容器
$docker container ls
或者
$docker ps
查看当前系统下所有容器
$ docker container ls -a
或者
$docker ps -a
终止某个容器:
$ docker container stop (id or name)
或者通过exit命令或 Ctrl+d 来退出终端,来停止容器。
进入容器:
$docker attach (id or name)
或者
$docker exec (id or name)
$docker attach从这个 stdin 中 exit,会导致容器的停止.
$docker exec从这个 stdin 中 exit,不会导致容器的停止.
推荐使用$docker exec
删除容器:$docker container rm (id or name)
清理所有处于终止状态的容器$ docker container prune
- 安装jupyter
首先进入容器:
$docker run -i -t ubuntu:16.04 /bin/bash
这个过程基本和在ubuntu系统上安装jupyter的过程是一样的,但容器中的ubuntu是个最简环境,没有安装python-dev包。
#更新apt-get环境
apt-get update
#安装python dev包
apt-get install python-dev
#安装jupyter
pip install jupyter
jupyter 默认只能通过本地地址访问,要放开配置,允许jupyter远程访问。在放开远程访问时,需要设置密码,jupyter的配置文件只支持加密后的密文密码。
#生成jupyter配置文件,这个会生成配置文件.jupyter/jupyter_notebook_config.py
jupyter notebook --generate-config
#使用ipython生成密码
In [1]: from notebook.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 notebook命令把jupyter启动起来了,如果在容器中直接使用的root用户,启动jupyter的命令为jupyter notebook --allow-root。
最终的命令为:
docker run -it --name jupytertest -p 8888:8888 -v ~/mnt:/mnt jupyter-ubuntu:v1 su root -c 'jupyter notebook --allow-root'
-p为端口映射,-v为路径挂载映射。
启动成功后使用http://ubuntu-ip:8888访问。

补充:在docker上创建远程jupyter
在远程服务器上的docker容器内使用命令
jupyter notebook --port=8888 --allow-root
即可运行jupyter。
需要注意的一点是,如果当前docker容器的端口是8888的话,即可省略--port=8888,若当前docker容器的端口不是8888的话,需在运行jupyter的时候指定端口和当前docker容器的端口一致。
如果忘了当前docker容器的端口,可在docker外使用命令
docker ps
查看。
运行了jupyter之后,界面上会输出一堆类似log的东西。如下:

保留当前界面,并且记录其中的token(用红线框出部分)。
运行之后在本地的浏览器输入远程服务器的ip:运行jupyter的docker容器的端口号。例如:192.168.0.101:8888。
登录界面如下:

如果不想设置密码的话,直接使用token登录即可。
如果要设置密码的话,使用token设置密码也行。下次进入就可以直接使用密码而不用token了。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持本站。如有错误或未考虑完全的地方,望不吝赐教。