Blog Entry

试一下dotnet-interactive

试一下dotnet-interactive

Created
2022/04/21
Updated
2022/04/21

in VSCode

  1. 安装.Net6 SDK
  2. 安装插件

in Docker

文件结构

.
├─ Dockerfile
├─ docker-compose.yml
└─ conf
└─ jupyter_lab_config.py 配置文件

Dockfile

根据这个Dockfile修改

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal
ARG HTTP_PORT_RANGE=1100-1200
# Opt out of telemetry until after we install jupyter when building the image, this prevents caching of machine id
ENV DOTNET_INTERACTIVE_CLI_TELEMETRY_OPTOUT=true
# Install all OS dependencies for notebook server that starts but lacks all
# features (e.g., download as all possible file formats)
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update \
&& apt-get install -yq --no-install-recommends \
wget \
bzip2 \
ca-certificates \
sudo \
locales \
fonts-liberation \
run-one \
python3.8 \
python3-pip \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
locale-gen
RUN python3 -m pip install setuptools
RUN python3 -m pip install jupyter
RUN python3 -m pip install jupyterlab
# Add package sources
RUN dotnet nuget add source "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" -n "dotnet-tools"
RUN dotnet nuget add source "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" -n "dotnet5"
RUN dotnet nuget add source "https://pkgs.dev.azure.com/dnceng/public/_packaging/MachineLearning/nuget/v3/index.json" -n "MachineLearning"
# Install lastest build from master branch of Microsoft.DotNet.Interactive
RUN dotnet tool install --tool-path /usr/share/dotnet-interactive Microsoft.dotnet-interactive --add-source "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json"
RUN ln -s /usr/share/dotnet-interactive/dotnet-interactive /usr/bin/dotnet-interactive
RUN dotnet interactive jupyter install --http-port-range ${HTTP_PORT_RANGE}
# Enable telemetry once we install jupyter for the image
ENV DOTNET_INTERACTIVE_CLI_TELEMETRY_OPTOUT=false
EXPOSE 8888
EXPOSE ${HTTP_PORT_RANGE}
RUN mkdir notebooks
WORKDIR notebooks
ENTRYPOINT jupyter lab --ip=0.0.0.0 --allow-root --notebook-dir=/notebooks/

docker-compose

docker-compose
version: '3'
services:
dotnet-interactive:
# image: xkyii/dotnet-interactive
build: .
volumes:
- ~/data/notebooks:/notebooks
- ./conf/:/conf
ports:
- 8888:8888
entrypoint:
- jupyter-lab
command:
--config=/conf/jupyter_lab_config.py
# 写到配置文件里面了
# --no-browser
# --ip=0.0.0.0
# --allow-root
# --notebook-dir=/notebooks/

配置

conf/jupyter_lab_config.py
# 启动时不打开浏览器
c.LabApp.open_browser = False
# 允许域名(nginx反代需要)
c.ServerApp.allow_origin = '*'
# 允许远程访问(nginx反代需要)
c.ServerApp.allow_remote_access = True
# 以root运行(Docker传统技能)
c.ServerApp.allow_root = True
# 监听ip
c.ServerApp.ip = '0.0.0.0'
# 登录密码
c.ServerApp.password = '...'

关于密码:

Terminal window
jupyter-lab password

Enter password: Verify password: [JupyterPasswordApp] Wrote hashed password to /root/.jupyter/jupyter_server_config.json

Terminal window
cat /root/.jupyter/jupyter_server_config.json
{
"ServerApp": {
"password": "argon2:$argon2id$v=19$m=10240,t=10,p=8$5gG0gJSC4x2lu0Z7jPJU/A$F9zX+C+jfVFc8ixxrTAonS/ioGjGG6oHJgXP/iRwkX4"
}
}

ServerApp.password的内容复制填充到jupyter_lab_config.pyc.ServerApp.password字段就可以了

启动

Terminal window
docker-compose up -d
# 如果启动有问题,可以用这个看日志
# 建议首次启动时用这个,毕竟构建docker镜像过程还挺不稳定的
docker-compose up

访问

启动成功的话应该就可以访问了: http://your-ip:8888

nginx代理

notebook.xkyii.cn
server {
# 监听端口
listen 80;
# 域名
server_name notebook.xkyii.cn;
# Proxy
location / {
proxy_pass http://127.0.0.1:8888/;
proxy_http_version 1.1;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}
}

后面就可以用域名访问了