首页 > 编程语言 > docker容器下配置jupyter notebook的操作
2021
03-24

docker容器下配置jupyter notebook的操作

docker容器下配置jupyter notebook,主要是为了编写python代码,更具体点是做深度学习的开发。

jupyter web形式最高效的使用方式就是部署在云上,不管是cpu云服务器还是gpu的云服务器,都能快速启动使用。

而docker的出现又方便了很多在部署使用上。

- 安装 docker

docker分为docker CE和docker EE,一般使用docker CE(社区版本)。

docker可以在Linux(ubuntu、centos)、MacOS、Windows或者树莓派上安装。一般主要在linux下使用,我个人喜欢ubuntu系统。所以介绍在ubutnu下安装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 镜像源,如果需要测试或每日构建版本的 Docker CE 请将 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了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自学编程网。如有错误或未考虑完全的地方,望不吝赐教。

编程技巧