自定义 Cloud Shell 容器映像

本页面介绍如何创建自定义 Docker 映像并将其用作 Cloud Shell 环境。

如需创建自定义 Cloud Shell 体验,您需要创建 Docker 映像,该映像充当具有指定其他软件包和自定义配置的自定义 Cloud Shell 环境。您的 Docker 映像必须基于 Cloud Shell 基础映像,并托管在 Container Registry 中。

创建自定义 Docker 映像


如需在 Cloud Shell Editor 中直接遵循有关此任务的分步指导,请点击操作演示

操作演示


按照以下说明创建您自己的自定义 Docker 映像:

  1. 在 Cloud Shell 终端标签页中,运行以下命令,以在 Cloud Source Repositories 托管的代码库中创建样板自定义映像:

    cloudshell env create-custom-image NEW_REPO_NAME
    

    或者,如果您希望手动部署到 Container Registry 或使用自己的自动化流程,则可以创建新的空 Dockerfile 并跳到第 3 步。

  2. 按照输出到命令行的说明打开新的 Dockerfile:

    cd $HOME/NEW_REPO_NAME && cloudshell edit Dockerfile
    
  3. 在 Cloud Shell 体验的第一行中添加您想要使用的任何其他软件包。例如:

    FROM gcr.io/cloudshell-images/cloudshell:latest
    RUN apt-get -y install lsof
    

    Dockerfile 中的第一行 FROM gcr.io/cloudshell-images/cloudshell:latest 引用了 Cloud Shell 基础映像,不应移除。

  4. 通过运行以下命令在本地构建映像:

    cloudshell env build-local
    
  5. 通过运行以下命令在本地测试映像,并验证已安装的软件包是否存在:

    cloudshell env run
    

    如需退出测试,请运行以下命令:

    exit
    
  6. 在本地提交代码更改:

    git commit -a -m "Initial custom environment check-in."
    
  7. 将代码更改推送到 Cloud Source Repositories:

    git push origin master
    
  8. 最后,将您的自定义映像推送到 Container Registry:

    cloudshell env push
    

要通过在 Cloud Shell 中打开链接使用自定义映像,您需要先将其公开。如需了解将自定义映像设为公开的步骤,请按照 Container Registry 访问权限控制指南进行操作。

设置正确的访问权限设置后,您可以更新在 Cloud Shell 中打开链接,以指定 cloudshell_git_repocloudshell_image 参数。例如,如需添加在 Cloud Shell 中打开按钮,请修改以下示例中的网址,然后将类似于以下内容的链接添加到您的 Markdown 文件中:

[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://shell.cloud.google.com/cloudshell/editor?cloudshell_git_repo=REPO_URL&cloudshell_image=DOCKER_IMAGE_URL)

替换以下值:

  • REPO_URL:指向代码库位置的网址
  • DOCKER_IMAGE_URL:指向 Docker 映像的网址

如需详细了解这些参数及其他参数,请参阅在 Cloud Shell 中打开

使自定义容器映像保持最新状态

您的自定义容器必须基于最新的公开 Cloud Shell 映像才能在 Cloud Shell 上运行。我们建议您每周重新构建一次,以确保自定义映像处于最新状态。