<从0开始> 构建jupyter+tensorflow+keras的docker镜像

5,740 阅读2分钟

趁热记录下,给未来的自己

前言

最近在学习机器学习,为了方便在不同地方可以访问到相同的学习资源和保证学习进度的连续性以及无需重复搭建环境,从而降低学习成本,就需要搭建一套在线的学习平台,保证在任何地方,都可以快速进入学习状态。借助于jupyter可以完美满足我的需求。

另外,考虑到移植的方便性,将采用docker的方式构建和部署平台。

必要的文件

Dockerfile

首先编写Dockerfile文件,用于构建docker镜像

注意:Dockerfile需要和sources.list在同一个目录下

    FROM rackspacedot/python37
    MAINTAINER arkMon

    RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak
    # 替换apt源为国内源,加快构建速度
    COPY ./sources.list /etc/apt/ 
    RUN apt-get update & apt-get upgrade -y
    # 更新pip源
    RUN pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ 
    RUN /usr/local/bin/python -m pip install --upgrade pip
    # 安装python画图插件
    RUN pip3 install matplotlib 
    # 安装jupyter
    RUN pip3 install jupyter 
    RUN ipython3 kernel install
    # 安装Jupyter交互界面扩展组件
    RUN pip3 install ipywidgets 
    # 安装Jupyter交互界面扩展组件
    RUN jupyter nbextension enable --py widgetsnbextension
    # 配置Jupyter远程访问
    RUN echo "c.NotebookApp.open_browser = False" >> /root/.jupyter/jupyter_notebook_config.py 
    RUN echo "c.NotebookApp.notebook_dir = '/root/jupyter'" >> /root/.jupyter/jupyter_notebook_config.py
    RUN echo "c.NotebookApp.ip = '*'" >> /root/.jupyter/jupyter_notebook_config.py
    RUN echo "c.NotebookApp.base_url = '/jupyter'" >> /root/.jupyter/jupyter_notebook_config.py 
    RUN echo "c.NotebookApp.allow_origin = '*'" >> /root/.jupyter/jupyter_notebook_config.py 
    # 安装tensorflow
    RUN pip3 install --upgrade tensorflow
    RUN pip3 install tensorflow
    # 安装keras
    RUN pip3 install keras
    # 启动容器时命令,因为容器默认在root用户下,故这里需要加上--allow-root
    CMD jupyter notebook --allow-root

sources.list

使用apt的国内阿里云镜像,加快apt-get的速度

$ cat sources.list

    deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
    deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

构建镜像

$ docker build .

注意:该命令的运行目录和Dockerfile必须在同一层目录下

启动镜像

$ docker run -dti --name jupyter_keras -p 40000:8888 -v /data/workspace/docker-install/keras_jupyter/jupyter:/root/jupyter ${image_id}

把容器内的/root/jupyter目录挂载到宿主机/data/workspace/docker-install/keras_jupyter/jupyter目录下,

  • 一方面,可以把在jupyter里编辑的文件,持久化到宿主机上,保证了数据的安全性
  • 另一方面,后续可以做负载均衡的横向扩展:利用该镜像启动多个容器副本,所有容器都挂载到宿主机的同一个目录,利用nginx的upstream功能呢,实现负载均衡能力

外部访问

地址:http://HostIP:40000