使用自定义 docker 图像启动 Google Cloud AI Platform Notebooks 的要求

Requirements for launching Google Cloud AI Platform Notebooks with custom docker image

在 AI Platform Notebooks 上,the UI 允许您 select 启动自定义图像。如果这样做,您会看到一个信息框,上面写着容器 "must follow certain technical requirements":

我认为这意味着他们有一个必需的入口点、公开的端口、jupyterlab 启动命令或某事,但我找不到任何关于实际要求的文档

我一直在尝试对其进行逆向工程,但运气不佳。我 nmap 编辑了一个标准实例并看到它打开了端口 8080,但是将我的图像 CMD 设置为 运行 0.0.0.0:8080 上的 Jupyter Lab 并没有成功。当我在 UI 中单击 "Open JupyterLab" 时,我得到一个 504。

有没有人link了解相关文档,或者过去有过这样做的经验?

您可以通过两种方式创建自定义容器:

构建衍生容器

如果您只需要安装额外的包,您应该创建一个从标准图像之一派生的 Dockerfile(例如,FROM gcr.io/deeplearning-platform-release/tf-gpu.1-13:latest),然后添加 运行 命令使用 conda/pip/jupyter 安装包。

conda 基础环境已经添加到路径中,所以不需要conda init/conda activate 除非你需要设置另一个环境。在启动环境之前需要 运行 的其他 scripts/dynamic 环境变量可以添加到 /env.sh,它作为入口点的一部分。

例如,假设您有一个自定义的 TensorFlow wheel,您想用它来代替内置的 TensorFlow 二进制文件。如果您不需要额外的依赖项,您的 Dockerfile 将类似于:

Dockerfile.example

FROM gcr.io/deeplearning-platform-release/tf-gpu:latest
RUN pip uninstall -y tensorflow-gpu && \
    pip install -y /path/to/local/tensorflow.whl

然后您需要构建它并将其推送到您的 GCE 服务帐户可以访问的地方。

PROJECT="my-gcp-project"
docker build . -f Dockerfile.example -t "gcr.io/${PROJECT}/tf-custom:latest"
gcloud auth configure-docker
docker push "gcr.io/${PROJECT}/tf-custom:latest"

从头开始构建容器

主要要求是容器必须在端口 8080 上公开服务。

在 VM 上执行的 sidecar 代理将仅将请求传送到此端口。

如果使用 Jupyter,您还应该确保您的 jupyter_notebook_config.py 配置如下:

c.NotebookApp.token = ''
c.NotebookApp.password = ''
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8080
c.NotebookApp.allow_origin_pat = (
'(^https://8080-dot-[0-9]+-dot-devshell\.appspot\.com$)|'
'(^https://colab\.research\.google\.com$)|'
'((https?://)?[0-9a-z]+-dot-datalab-vm[\-0-9a-z]*.googleusercontent.com)')
c.NotebookApp.allow_remote_access = True
c.NotebookApp.disable_check_xsrf = False
c.NotebookApp.notebook_dir = '/home'

这会禁用基于笔记本令牌的身份验证(身份验证通过代理上的 oauth 登录来处理),并允许来自三个来源的跨源请求:云 Shell 网络预览、colab(请参阅此博客 post) 和 Cloud Notebooks 服务代理。笔记本服务只需要三分之一;前两个支持备用访问模式。

为了完成 Zain 的回答,您可以在下面找到一个使用官方 Jupyter 图像的最小示例,灵感来自此 repo https://github.com/doitintl/AI-Platform-Notebook-Using-Custom-Container

Dockerfile

FROM jupyter/base-notebook:python-3.9.5

EXPOSE 8080

ENTRYPOINT ["jupyter", "lab", "--ip", "0.0.0.0", "--allow-root", "--config", "/etc/jupyter/jupyter_notebook_config.py"]

COPY jupyter_notebook_config.py /etc/jupyter/

jupyter_notebook_config.py

(与 Zain 的几乎相同,但有一个额外的模式可以与内核进行通信;没有它通信将无法进行)

c.NotebookApp.ip = '*'
c.NotebookApp.token = ''
c.NotebookApp.password = ''
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8080
c.NotebookApp.allow_origin_pat = '(^https://8080-dot-[0-9]+-dot-devshell\.appspot\.com$)|(^https://colab\.research\.google\.com$)|((https?://)?[0-9a-z]+-dot-datalab-vm[\-0-9a-z]*.googleusercontent.com)|((https?://)?[0-9a-z]+-dot-[\-0-9a-z]*.notebooks.googleusercontent.com)|((https?://)?[0-9a-z\-]+\.[0-9a-z\-]+\.cloudshell\.dev)|((https?://)ssh\.cloud\.google\.com/devshell)'
c.NotebookApp.allow_remote_access = True
c.NotebookApp.disable_check_xsrf = False
c.NotebookApp.notebook_dir = '/home'
c.Session.debug = True

最后,在进行故障排除时考虑此页面:https://cloud.google.com/notebooks/docs/troubleshooting