Linux Centos7 使用Docker容器安装 jupyter notebook

首先进入容器

docker run -it centos:7 /bin/bash

容器内安装jupyter

这个过程基本和在 centos 系统上安装 jupyter 的过程是一样的,但容器中的 centos 是个最简环境,没有安装 pip 包。

准备工作

# 1、更新yum 
yum update -y

#2、安装 epel 源。如果不安装epel 源,后面安装pip的时候可能会报错:
#    No package python-pip available. 
#    Error: Nothing to do
yum install epel-release -y

#3、centos镜像自带python2.7,但是没有安装 pip ,还需要安装pip
yum install python-pip

安装jupyter

pip install jupyter

# 我这里出现了以下报错(2021年3月8日):
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-Upoocz/nbconvert/setup.py", line 81
        print("Failed, try again after installing PycURL with `pip install pycurl` 
to avoid outdated SSL.", file=sys.stderr)
                                                                                                                ^
    SyntaxError: invalid syntax
  ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-Upoocz/nbconvert/
You are using pip version 8.1.2, however version 21.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

如果使用把 pip升级到最新版本,再使用 pip 安装包时可能会报错:sys.stderr.write(f“ERROR: {exc}“)

问题原因:参考 https://blog.csdn.net/xxchaveablog/article/details/114279373

Python 2.7已于2020年1月1日到期,请停止使用。请升级您的Python,因为不再维护Python 2.7。pip 21.0将于2021年1月停止对Python 2.7的支持。pip 21.0将删除对此功能的支持。

#使用下面方法安装以前的pip版本解决:

# yum remove python-pip
yum install -y wget
wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
python get-pip.py 

# 然后就可以安装jupyter了
# 再次运行 pip install jupyter

 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:******'

# 记得把 sha1 后面的密码保存起来,后面要用到


#去配置文件.jupyter/jupyter_notebook_config.py中修改以下参数
c.NotebookApp.allow_remote_access=True
c.NotebookApp.ip='*'                      #绑定所有地址
c.NotebookApp.open_browser = False        #启动后是否在浏览器中自动打开
c.NotebookApp.password = u'刚才生成的密码'
c.NotebookApp.port =8888                  #指定一个访问端口,默认8888,注意和映射的docker端口对应

这个时候又出现了问题:网页登录的时候出现 Invalid credentials,解决方法:https://blog.csdn.net/weixin_46248466/article/details/105081120

安装过程中出现了各种各样的问题,但是最后都解决了。

为了方便以后使用,把容器 commit 成镜像,方便下次使用

docker commit 容器id jupyter-centos:1.0

# 最终命令
docker run -it -p 8888:8888 jupyter-centos:1.0 su root -c 'jupyter notebook --allow-root --NotebookApp.password=sha1:xxxx /mnt'

# xxxx是你的sha1密码

启动成功后使用 `http://ubuntu-ip:8888` 访问。

参考资料:https://blog.csdn.net/leng_yan/article/details/87208363

猜你喜欢

转载自blog.csdn.net/weixin_42467709/article/details/114535935