Friday, March 5, 2021

.NET Interactive in GCP AI Platform Notebooks via a Container

In my last post, I showed how to setup F#, C#, and PowerShell within Google Cloud AI Platform Notebooks using a manual approach. Another option is to create a notebooks instance from a container that includes .NET Interactive pre-installed.

To accomplish this, do the following:

1. Enable applicable GCP APIs (Cloud Build, Artifact Registry, AI Platform Notebooks, etc.), if they are not already enabled.

2. Open the GCP Cloud Shell Terminal.

3. In Cloud Shell, create a file named Dockerfile (e.g. nano Dockerfile), copy/paste the following into the file, and save.

# Specifies base image and tag
FROM gcr.io/deeplearning-platform-release/base-cpu:latest
ENV \
# Enable detection of running in a container
DOTNET_RUNNING_IN_CONTAINER=true \
# Enable correct mode for dotnet watch (only mode supported in a container)
DOTNET_USE_POLLING_FILE_WATCHER=true \
# Skip extraction of XML docs - generally not useful within an image/container - helps performance
NUGET_XMLDOC_MODE=skip \
# Opt out of telemetry until after we install jupyter when building the image, this prevents caching of machine id
DOTNET_INTERACTIVE_CLI_TELEMETRY_OPTOUT=true
# Installs .NET Interactive
RUN wget https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
RUN dpkg -i packages-microsoft-prod.deb
RUN apt-get update
RUN apt-get install -y dotnet-sdk-3.1
RUN dotnet tool install -g --add-source "https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" Microsoft.dotnet-interactive --version 1.0.155302
ENV PATH="$PATH:~/.dotnet/tools"
RUN echo $PATH
RUN dotnet interactive jupyter install
# Enable telemetry once we install jupyter for the image
ENV DOTNET_INTERACTIVE_CLI_TELEMETRY_OPTOUT=false
EXPOSE 8080
view raw Dockerfile hosted with ❤ by GitHub

4. Build the container with a command such as the following (Note: Replace the “REPLACE_WITH_PROJECT_ID” placeholder with your GCP Project ID):

gcloud builds submit --tag gcr.io/REPLACE_WITH_PROJECT_ID/interactive/dotnet-interactive:latest
view raw gistfile1.txt hosted with ❤ by GitHub

5. Create a new Notebooks instance via gcloud or the AI Platform Notebooks console. Note: Replace the “REPLACE_WITH_PROJECT_ID” placeholder with your GCP Project ID.

gcloud beta notebooks instances create dotnet-interactive-docker \
--container-repository=gcr.io/REPLACE_WITH_PROJECT_ID/interactive/dotnet-interactive \
--container-tag=latest \
--machine-type=n1-standard-4 \
--location=us-central1-b
view raw gistfile1.txt hosted with ❤ by GitHub

6. Once the notebook instance is created, open JupyterLab and F#, C#, & PowerShell options will be available.