docker + TensorFlow2.0 + jupyter lab

服务器为 Ubuntu

首先当然需要有Docker

安装docker tensorflow jupyter

参考tensorflow安装教程

输入一下命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
$ sudo docker run -it -p 8888:8888 tensorflow/tensorflow:nightly-py3-jupyter
Unable to find image 'tensorflow/tensorflow:nightly-py3-jupyter' locally
nightly-py3-jupyter: Pulling from tensorflow/tensorflow
5bed26d33875: Pull complete
f11b29a9c730: Pull complete
930bda195c84: Pull complete
78bf9a5ad49e: Pull complete
84227541b6bb: Pull complete
4f31c9672ae8: Pull complete
3ab00ff69975: Pull complete
ba1f9a4960a4: Pull complete
1e2e9ebd327e: Extracting [==================================================>] 590.2MB/590.2MB
1e2e9ebd327e: Pull complete
5dbeace46811: Pull complete
236b4193378a: Pull complete
94c8012aaf41: Pull complete
8978cb54431f: Pull complete
a32047d10082: Pull complete
4bfab90cc021: Pull complete
db51178e67ae: Pull complete
c582e0693d6e: Pull complete
d9275e9db168: Pull complete
48835526d3e2: Pull complete
5287adc8a8f2: Pull complete
525c81ec54db: Pull complete
0161c3804581: Pull complete
ebfa1948cb3e: Pull complete
933aa5af1920: Pull complete
a600cede3739: Pull complete
1d8aeacd0c4f: Pull complete
683a7567eeea: Pull complete
Digest: sha256:0b59d7826a07049f013c171bb96c26c0d4a6222f856e6520a6cd70d7be5ecdec
Status: Downloaded newer image for tensorflow/tensorflow:nightly-py3-jupyter

[I 07:24:55.849 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
jupyter_http_over_ws extension initialized. Listening on /http_over_websocket
[I 07:24:56.134 NotebookApp] Serving notebooks from local directory: /tf
[I 07:24:56.134 NotebookApp] The Jupyter Notebook is running at:
[I 07:24:56.134 NotebookApp] http://1d6d334faf94:8888/?token=ec98c98ec99956afa3a078ce9ae5e34f11a88b6bf92f0ac8
[I 07:24:56.134 NotebookApp] or http://127.0.0.1:8888/?token=ec98c98ec99956afa3a078ce9ae5e34f11a88b6bf92f0ac8
[I 07:24:56.134 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 07:24:56.138 NotebookApp]

To access the notebook, open this file in a browser:
file:///root/.local/share/jupyter/runtime/nbserver-1-open.html
Or copy and paste one of these URLs:
http://1d6d334faf94:8888/?token=ec98c98ec99956afa3a078ce9ae5e34f11a88b6bf92f0ac8
or http://127.0.0.1:8888/?token=ec98c98ec99956afa3a078ce9ae5e34f11a88b6bf92f0ac8

等待一会

安装并成功启动了jupyter notebook,在浏览器中打开提示的网址:http://127.0.0.1:8888/?token=...

按住 ctrl + c 退出

接下来是重点

安装jupyter lab

查看docker镜像

1
2
3
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tensorflow/tensorflow nightly-py3-jupyter 37b1af999efc 4 months ago 2.42GB

先删除刚启动的容器

1
2
sudo docker stop 37b1af999efc  #关闭
sudo docker container rm 37b1af999efc #删除

在本机上新建一个文件夹,和一个json文件

1
2
3
$ mkdir jupyter_config
$ cd jupyter_config/
~/jupyter_config$ vim config.json

这个文件内容是jupyter-lab的配置信息

1
2
3
4
5
6
7
8
9
10
{
"NotebookApp":{
"ip":"*",
"port":8888,
"password":"",
"open_browser":false,
"token":"",
"allow_root":true
}
}

在docker中创建挂载jupyter_config文件夹的容器

1
2
3
4
5
6
$ sudo docker run -itd -p 8888:8888 -v ~/【主机的目录】:/home/user【容器的目录】 tensorflow/tenso
rflow:nightly-py3-jupyter bash
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
e72acb760f91 tensorflow/tensorflow:nightly-py3-jupyter "bash" About a minute ago Up About a

进入该容器

1
2
3
4
5
6
7
$ sudo docker exec -it e72acb760f91 bash

________ _______________
___ __/__________________________________ ____/__ /________ __
__ / _ _ \_ __ \_ ___/ __ \_ ___/_ /_ __ /_ __ \_ | /| / /
_ / / __/ / / /(__ )/ /_/ / / _ __/ _ / / /_/ /_ |/ |/ /
/_/ \___//_/ /_//____/ \____//_/ /_/ /_/ \____/____/|__/

在容器中安装jupyter lab

1
root@e72acb760f91:/tf# pip3 install jupyterlab

切换到我们挂载的jupyter_config文件夹去

1
2
3
root@e72acb760f91:/tf# cd /home/user/
root@e72acb760f91:/home/user# ls
jupyter_config

启动jupyter lab

1
2
3
4
5
6
7
8
9
10
11
# jupyter-lab --config ./jupyter_config/config.json
[I 08:44:59.650 LabApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[W 08:44:59.906 LabApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[W 08:44:59.906 LabApp] WARNING: The notebook server is listening on all IP addresses and not using authentication. This is highly insecure and not recommended.
jupyter_http_over_ws extension initialized. Listening on /http_over_websocket
[I 08:44:59.917 LabApp] JupyterLab extension loaded from /usr/local/lib/python3.6/dist-packages/jupyterlab
[I 08:44:59.917 LabApp] JupyterLab application directory is /usr/local/share/jupyter/lab
[I 08:44:59.920 LabApp] Serving notebooks from local directory: /home/xujie
[I 08:44:59.920 LabApp] The Jupyter Notebook is running at:
[I 08:44:59.920 LabApp] http://e72acb760f91:8888/
[I 08:44:59.920 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

用浏览器打开

1
http:// docker IP :8888/

ip查看方式:

1
2
3
4
5
6
7
8
9
10
$ ifconfig

docker0 Link encap:Ethernet HWaddr 02:42:52:46:56:2f
inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0
inet6 addr: fe80::42:52ff:fe46:562f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13461 errors:0 dropped:0 overruns:0 frame:0
TX packets:17994 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:20897178 (20.8 MB) TX bytes:114945170 (114.9 MB)

完结,开始工作。

0%