Jupyter notebook镜像服务docker

2019-07-17  本文已影响0人  ketchup

本文将制作一个对外提供Jupyter notebook服务的dockerfile
并且去掉logo,去掉terminal, 去掉quit功能

dockerfile编写使⽤用说明:

# 在 Dockerfile 文件中 # 是注释
# FROM 用于指定构建镜像使用的基础镜像
FROM ubuntu:18.04


# RUN 用于在构建镜像的时候在镜像中执行命令,&&\ 是换行
# 这里我们安装 python3 和 flask web 框架
RUN apt update &&\
    apt -y install python3 python3-pip &&\
    pip3 install flask


# COPY 相当于命令的 docker cp
# 把本机当前目录下的 app.py 文件拷贝到镜像的 /code/app.py
# 和 docker cp 不同的是,COPY 会自动创建镜像中不存在的目录,比如 /code
COPY app.py /code/app.py


# WORKDIR 用于指定从镜像启动的容器内的工作目录
WORKDIR /code


# CMD 用于指定容器运行后要执行的命令和参数列表
# 这样从本镜像启动容器后会自动执行 python3 app.py 这个命令
#
# 由于我们已经用 WORKDIR 指定了容器的工作目录
# 所以下面的命令都是在 /code 下执行的
CMD ["python3", "app.py"]

# 你可能会看到有资料介绍一个 ENTRYPOINT 参数用于指定容器运行后的入口程序
# 但是这个参数在现在的意义已经很小了,请忽略之

下面是做一个jupyter notebook的dockerfile示例:

#标准情况下不能使用lastest版本号,要用具体版本
FROM centos:latest

#把镜像中要用到的文件,从本地当前目录拷贝到镜像内
COPY . /root


RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm && \
    yum install -y python36u python36u-libs python36u-devel python36u-pip && \
    pip3.6 install jupyter -i https://pypi.douban.com/simple && \
    jupyter notebook --generate-config && \
    #替换改文件是为了 更改端口,密码等设置
    cp /root/jupyter_notebook_config.py /root/.jupyter/ &&\
    #安装numpy 等科学计算库
    python3.6 -m pip install numpy matplotlib pandas scipy -i https://pypi.douban.com/simple &&\
    #替换该文件为了去掉jupyter  logo
    cp /root/custom.css /usr/lib/python3.6/site-packages/notebook/static/custom/

EXPOSE 8888
CMD ["/bin/bash", "-x", "/root/start.sh

start.sh文件内容

#!/bin/sh
jupyter notebook --ip=0.0.0.0 --no-browser --allow-root & >>/data/log.txt
tail -f /dev/null

custom.css文件内容

/*
Placeholder for custom user CSS

mainly to be overridden in profile/static/custom/custom.css

This will always be an empty file in IPython
*/

/*for the error , connecting & renaming window*/

[dir="rtl"] .modal-footer {
    text-align : left !important;
}

[dir="rtl"] .close {
    float : left;
}

[dir="rtl"] .fa-step-forward::before {
    content: "\f048";
}
#ipython_notebook img{
    display:block;
    background: url(logo.png) no-repeat;
    background-size: contain;
    width: 233px;
    height: 33px;
    padding-left: 233px;
    -moz-box-sizing: border-box;
    box-sizing: border-b
}

jupyter_notebook_config.py 文件内容

#登陆不要密码
c.NotebookApp.token = ''
#jupyter 对外服务端口
c.NotebookApp.port = 8888

c.NotebookApp.open_browser = False
#容器内挂载目录
c.NotebookApp.notebook_dir = '/data'
#前端嵌入frame的时候会有跨域访问错误, 在这里加入ip白名单, 实现同源, 即可将jupyter嵌入到实训了
c.NotebookApp.tornado_settings = { 'headers': { 'Content-Security-Policy':"frame-ancestors 'self' http://106.14.14.147:83 http://106.14.14.147:8082 http://192.168.1.124:8083 http://101.132.135.5:8888 http://114.91.67.118:8888 http://106.14.14.147:8083 http://114.91.67.118 http://localhost:8083" } }
#去掉退出按钮
c.NotebookApp.quit_button = False
#去掉terminal按钮
c.NotebookApp.terminals_enabled = False

c.NotebookApp.ip = '

目录结构如下:

image.png
然后在当前目录下执行 docker build
docker build -t jupyter:1.0 .

如果需要上传镜像

修改 docker tag

docker tag jupyter:1.0 172.10.0.0:8000/library/jupyter:v1.0

docker push 传到仓库

docker push 172.10.0.0:8000/library/jupyter:v1.0
上一篇 下一篇

猜你喜欢

热点阅读