1. 程式人生 > >伺服器+docker+jupyter notebook

伺服器+docker+jupyter notebook

閒來無事,買了一個阿里雲的伺服器,1核2g跑機器學習模型慢了一點,不過喜歡折騰未必不是一件好事。為了方便資料探勘,資料視覺化,以及kaggle相關的一系列比賽,在伺服器上搭建了一個jupyter notebook,目前只是用root許可權登入的。

1、 Centos 7 docker的安裝

//yum安裝docker
[[email protected] ~]$ sudo yum install docker -y

//docker 啟動
[[email protected] ~]$ sudo systemctl start docker.service

//設定開機啟動docker
[
[email protected] ~]$ sudo systemctl enable docker.service
//docker常用的命令:
[[email protected] ~]$ docker info
[[email protected] ~]$ docker images
[[email protected] ~]$ docker ps
[[email protected] ~]$ docker --help

2、docker進行配置

//這裡拉取ubuntu到本地
[[email protected] ~]$ sudo docker pull ubuntu

//檢視本地的映象
[[email protected] ~]$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/ubuntu latest cd6d8154f1e1 3 weeks ago 84.1 MB //執行映象,建立一個容器 : // -p 將本地埠的9900對映到容器埠的8888 // -v 將本地目錄/root/JupyterNotebook掛載到容器的/JupyterNotebook [[email protected]
~]# sudo docker run -it -p 9900:8888 -v /root/JupyterN otebook:/JupyterNotebook cd6d8154f1e1 /bin/bash //這時已經進入容器裡面了 [email protected]:/# //更新源 [email protected]:/# apt-get update //安裝wget來下載anaconda [email protected]:/# wget https://repo.anaconda.com/archive/Anaconda3-5.3.0-Linux-x86_64.sh [email protected]:/# ls Anaconda3-5.3.0-Linux-x86_64.sh boot home media proc sbin tmp JupyterNotebook dev lib mnt root srv usr bin etc lib64 opt run sys var //執行指令碼,一路Enter按照提示就可以了 [email protected]:/# bash Anaconda3-5.3.0-Linux-x86_64.sh //source 一下bash [email protected]:/# source /root/.bashrc [email protected]:/# rm -rf Anaconda3-5.3.0-Linux-x86_64.sh

Jupyter Notebook 配置

//生成配置檔案
[email protected]:/# jupyter notebook --generate-config
Writing default config to: /root/.jupyter/jupyter_notebook_config.py

//生成密匙
[email protected]:/# ipython
Python 3.7.0 (default, Jun 28 2018, 13:15:42)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from notebook.auth import passwd
In [2]: passwd()
Enter password:
Verify password:
Out[2]: 'sha1:4e379475fe85:e9aee4f0b42202fee4f14be37ee8b29ae7dad126'
In [3]: exit()
//'sha1:4e379475fe85:e9aee4f0b42202fee4f14be37ee8b29ae7dad126'這個東西會寫到配置檔案裡複製下來

//編輯配置檔案
[email protected]:/# apt-get install vim -y
[email protected]:/# vim /root/.jupyter/jupyter_notebook_config.py

//在檔案末尾寫入(如果後面有需要,看文件就好了)
c.NotebookApp.ip='*'                       # 設定所有ip可以訪問
c.NotebookApp.password = u'sha:ce...       # 剛才複製的那個密文'
c.NotebookApp.open_browser = False         # 禁止自動開啟瀏覽器
c.NotebookApp.port =8888                   #指定開啟的埠

//這裡寫一個指令碼用來啟動jupyter notebook
[email protected]:/# vim notebook.sh
//寫入如下內容
#!/bin/bash
#!/root/.bashrc
/root/anaconda3/bin/jupyter notebook --allow-root
[email protected]:/# chmod +x notebook.sh

//使用Jupyter Notebook
[email protected]:/# jupyter notebook --allow-root
[I 08:47:49.861 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret[W 08:47:50.071 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 08:47:50.113 NotebookApp] JupyterLab extension loaded from /root/anaconda3/lib/python3.7/site-packages/jupyterlab[I 08:47:50.113 NotebookApp] JupyterLab application directory is /root/anaconda3/share/jupyter/lab[I 08:47:50.118 NotebookApp] Serving notebooks from local directory: /[I 08:47:50.118 NotebookApp] The Jupyter Notebook is running at:
[I 08:47:50.118 NotebookApp] http://(eb0788fc375d or 127.0.0.1):8888/[I 08:47:50.118 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

//如果訪問不了的話,就是主機的埠還沒有開啟(可以使用telnet來測試你的埠是否允許訪問)
~~//***如果出錯這裡問題最大***~~ 
[[email protected] ~] iptables -I INPUT -p tcp --dport 9900 -m state --state NEW -j ACCEPT
[[email protected] ~] iptables-save > /etc/sysconfig/iptables
[[email protected] ~] reboot

//檢視容器執行情況
[[email protected] ~] [[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS                    [email protected]:/# 
eb0788fc375d        cd6d8154f1e1        "/bin/bash"         46 minutes ago      Up 42 seconds                  0.0.0.0:9900->8888/tcp   blissful_blackwell

//進入容器
[[email protected] ~]# docker exec -it blissful_blackwell /bin/bash

//啟動容器並且執行指令碼
[[email protected] ~]# docker start blissful_blackwell /bin/bash
[[email protected] ~]# docker exec -d blissful_blackwell /notebook.sh

Jupyter Notebook的拓展


//進入容器使用conda安裝相應的包
[email protected]:/#  docker exec -it 容器名稱 /bin/bash
[email protected]:/#  conda install tensorflow
[email protected]:/#  conda install caffe