1
背景介紹? ? ? ?
在之前的文章 -?交互式 .Net?中已經介紹了什么是?交互式 .Net,文中是通過 Visual Studio Code 插件的方式實現?交互式 .Net 的。現在,我們將使用容器的方式實現?交互式 .Net。
2
鏡像構建? ? ? ?
1. Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:6.0-focalARG 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 sed -i 's|https\?://[^/]\+/|http://mirrors.aliyun.com/|' /etc/apt/sources.list
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-genRUN 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/dotnet6/nuget/v3/index.json" -n "dotnet6"
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=falseEXPOSE 8888
EXPOSE ${HTTP_PORT_RANGE}RUN groupadd -g 1337 jupyter
#RUN groupdel jupyterRUN mkdir notebooksWORKDIR notebooksENV TOKEN ''ENTRYPOINT jupyter lab --ip=0.0.0.0 --allow-root --notebook-dir=/notebooks/ --ServerApp.token=${TOKEN}
2. 構建鏡像
docker build -t dotnet-interactive:1.0.0 .
3
啟動容器? ? ? ?
執行以下指令啟動容器:
docker run --name dotnet-interactive -d -e TOKEN=123456 -v /root/notebooks:/notebooks -p 80:8888 dotnet-interactive:1.0.0
指令解析:
docker run | 啟動 docker 容器 |
-- name dotnet-interactive | 設置容器名稱為 nginx |
-d | 后臺運行 |
-e TOKEN=123456 | 設置訪問密碼為 123456,可根據需要進行調整 |
-v?/root/notebooks:/notebooks | 把容器目錄掛載到宿主機 |
-p 80:8888 | 把宿主機的 80 端口映射到容器的 8888 端口 |
dotnet-interactive:1.0.0 | 使用鏡像 |
4
使用介紹? ? ? ?
1.?打開網頁并登錄
2.?新建 Notebook
3.?解析 Markdown
輸入一段 markdown 內容,并選擇 Markdown,使用熱鍵 Alt+Enter 查看結果
4. 執行 C# 代碼
輸入一段 C# 代碼,并選擇 Code,使用熱鍵 Alt+Enter 查看結果
5.?獲取操作系統信息
6.?獲取 .Net 版本
7.?獲取?ipynb 文件
可以在宿主機的?/root/notebooks 路徑下獲取剛才操作的 ipynb 文件:
5
參考總結? ? ? ?
以上就是本文希望分享的內容,其中?interactive?的?Github?地址為:https://github.com/dotnet/interactive
如果大家有什么問題,歡迎在公眾號 - 跬步之巔留言交流。