使用用于 IntelliJ 的 Azure 工具包将 Web 应用发布为 Docker 容器Publish a web app as a Docker container by using the Azure Toolkit for IntelliJ

Docker 容器广泛用于部署 Web 应用程序。Docker containers are a widely used method for deploying web applications. 开发人员可在其中将其所有项目文件和依赖项整合成单个包,以便部署到服务器。By using Docker containers, developers can consolidate all their project files and dependencies into a single package for deployment to a server. 用于 IntelliJ 的 Azure 工具包可以添加用于部署到 Microsoft Azure 的“发布为 Docker 容器”功能,为 Java 开发人员简化了部署过程。 The Azure Toolkit for IntelliJ simplifies this process for Java developers by adding Publish as Docker Container features for deployment to Microsoft Azure. 本文逐步引导你完成将应用程序作为 Docker 容器发布到 Azure 的过程。This article walks you through the steps required to publish your applications to Azure as Docker containers.

备注

Docker 网站上提供了有关 Docker 的详细信息。More information about Docker is available on the Docker website.

先决条件Prerequisites

若要完成文章中的步骤,需要安装用于 IntelliJ 的 Azure 工具包,该工具包需要以下软件组件:To complete the steps in his article, you will need to install the Azure Toolkit for IntelliJ, which requires the following software components:

备注

JetBrains 插件存储库中的 Azure Toolkit for IntelliJ 页列出了与该工具包兼容的内部版本。The Azure Toolkit for IntelliJ page at the JetBrains Plugin Repository lists the builds that are compatible with the toolkit.

使用 Docker 容器将 Web 应用发布到 AzurePublish your web app to Azure by using a Docker container

备注

  • 若要发布 Web 应用,必须创建一个随时可用于部署的项目。To publish your web app, you must create a deployment-ready artifact. 有关详细信息,请参阅有关创建项目的其他信息部分。To learn more, see the Additional information about creating artifacts section.

  • 在完成部署向导至少一次后,再次运行向导时,在本演练中指定的大部分设置将用作默认值。After you have completed the deployment wizard at least once, most of your settings are used as defaults when you run the wizard again.

  1. 在 IntelliJ 中打开你的 Web 应用项目。Open your web app project in IntelliJ.

  2. 若要启动“发布为 Docker 容器”向导,请执行以下操作之一: To start the Publish as Docker Container wizard, do either of the following:

    • 在“项目”工具窗口中右键单击你的项目,然后依次单击“Azure”、“发布为 Docker 容器”: In the Project tool window, right-click your project, click Azure, and then click Publish as Docker Container:

      “发布为 Docker 容器”命令

    • 在 IntelliJ 工具栏中单击“发布组”按钮,然后单击“发布为 Docker 容器”: On the IntelliJ toolbar, click the Publish Group button, and then click Publish as Docker Container:

      “发布为 Docker 容器”命令The Publish as Docker Container command
      此时将打开“在 Azure 中部署 Docker 容器”向导。 The Deploy Docker Container on Azure wizard opens.

    “在 Azure 中部署 Docker 容器”向导

  3. 在“键入映像名称,选择项目的路径,并检查要使用的 Docker 主机”窗口中执行以下操作: In the Type an image name, select the artifact's path and check a Docker host to be used window, do the following:

    a.a. 在“Docker 映像名称”框中输入 Docker 主机的唯一名称。 In the Docker image name box, enter a unique name for your Docker host. (向导会自动创建名称,但你可以修改该名称。)(The wizard automatically creates a name, but you can modify it.)

    b.b. “主机”区域将显示已创建的所有 Docker 主机。 The Hosts area displays any Docker hosts that you have already created. 执行下列操作之一:Do either of the following:

    • 如果有现有的 Docker 主机,可以在其中部署 Web 应用。If you have an existing Docker host, you can deploy your web app to it.

    • 若要创建 Docker 主机,请单击绿色加号 ( + )。To create a Docker host, click the green plus sign (+).
      此时将打开“创建 Docker 主机”对话框。 The Create Docker Host dialog box opens.

      “在 Azure 中部署 Docker 容器”向导

  4. 在“配置新虚拟机”窗口中提供有关 Docker 主机的以下信息。 In the Configure the new virtual machine window, provide the following information about your Docker host. (向导会自动生成大多数信息,但你可以修改其中的任何信息。)(The wizard automatically generates most of the information for you, but you can modify any of them.)

    a.a. 在“名称”框中输入 Docker 主机的唯一名称。 In the Name box, enter a unique name for the Docker host. (这与前面指定的 Docker 映像名称不同。)(It is not the same as the Docker image name that you specified earlier.)

    b.b. 在“订阅”框中,输入主机要使用的 Azure 订阅。 In the Subscription box, enter the Azure subscription that you use for your host.

    c.c. 在“区域”框中,输入主机所在的地理区域。 In the Region box, enter the geographical region where your host is located.

    d.d. 在“OS 和大小”选项卡上执行以下操作: On the OS and Size tab, do the following:

    • 主机 OS:输入包含主机的虚拟机的操作系统。Host OS: Enter the operating system for the virtual machine that contains your host.
    • 大小:输入主机的虚拟机大小。Size: Enter the virtual-machine size for your host.

    e.e. 在“资源组”选项卡上选择以下选项之一: On the Resource Group tab, select either of the following:

    • 新建资源组:为主机创建资源组。New resource group: Create a resource group for your host.
    • 现有资源组:指定 Azure 帐户中的现有资源组。Existing resource group: Specify an existing resource group from your Azure account.

    f.f. 在“网络”选项卡上选择以下选项之一: On the Network tab, select either of the following:

    • 新建虚拟网络:为主机创建虚拟网络。New virtual network: Create a virtual network for your host.
    • 现有虚拟网络:指定 Azure 帐户中的现有虚拟网络。Existing virtual network: Specify an existing virtual network from your Azure account.

    g.g. 在“存储”选项卡上选择以下选项之一: On the Storage tab, select either of the following:

    • 新建存储帐户:为主机创建存储帐户。New storage account: Create a storage account for your host.
    • 现有存储帐户:指定 Azure 帐户中的现有存储帐户。Existing storage account: Specify an existing storage account from your Azure account.
  5. 单击“下一步”。 Click Next.
    此时将打开“配置登录凭据和端口设置”窗口。 The Configure log in credentials and port settings window opens.

    “配置登录凭据和端口设置”窗口

  6. 选择以下选项之一:Select one of the following options:

    • 从 Azure Key Vault 导入凭据:指定 Azure 订阅中存储的一组以前保存的凭据。Import credentials from Azure Key Vault: Specify a previously saved set of credentials that are stored in your Azure subscription.

      备注

      共享订阅的另一帐户或服务主体不会自动访问使用特定帐户或服务主体创建的 Azure Key Vault。An Azure key vault that's created with a specific account or service principal is not automatically accessible by another account or service principal that shares the subscription. 若要允许另一帐户或服务主体使用 Key Vault,必须使用 Azure 门户添加该帐户或服务主体。To allow another account or service principal to use the key vault, you must use the Azure portal to add the account or service principal.

    • 新建登录凭据:创建一组新的登录凭据。New log in credentials: Create a new set of login credentials. 如果选择此选项,请执行以下操作:If you select this option, do the following:

      a.a. 在“VM 凭据”选项卡上,为 Docker 主机的虚拟机登录凭据提供以下信息: On the VM Credentials tab, provide the following information for the virtual-machine login credentials of your Docker host:

      • 用户名:输入虚拟机登录凭据的用户名。Username: Enter the username for your virtual-machine login credentials.

      • 密码确认:输入虚拟机登录凭据的密码。Password and Confirm: Enter the password for your virtual-machine login credentials.

      • SSH:输入 Docker 主机的安全外壳 (SSH) 设置。SSH: Enter the Secure Shell (SSH) settings for your Docker host. 可选择以下选项之一:You can select one of the following options:

      • :指定虚拟机不允许 SSH 连接。None: Specifies that your virtual machine does not allow SSH connections.

      • 自动生成:自动创建用于通过 SSH 建立连接的必需设置。Auto-generate: Automatically creates the requisite settings for connecting via SSH.

      • 从目录导入:用于指定包含以前已保存的一组 SSH 设置的目录。Import from directory: Allows you to specify a directory that contains a set of previously saved SSH settings. 该目录必须包含以下两个文件:The directory must contain the following two files:

        • id_rsa:包含用户的 RSA 标识。id_rsa: Contains the RSA identification for a user.

        • id_rsa.pub:包含用于身份验证的 RSA 公钥。id_rsa.pub: Contains the RSA public key that is used for authentication.

      b.b. 在“Docker 守护程序访问”选项卡上提供以下信息: On the Docker Daemon Access tab, provide the following information:

      创建 Docker 主机

      • Docker 守护程序端口:输入 Docker 主机的唯一 TCP 端口。Docker Daemon port: Enter the unique TCP port for your Docker host.

      • TLS 安全性:输入 Docker 主机的传输层安全性设置。TLS Security: Enter the Transport Layer Security settings for your Docker host. 可从以下选项中选择:You can choose from the following options:

      • :指定虚拟机不允许 TLS 连接。None: Specifies that your virtual machine does not allow TLS connections.

      • 自动生成:自动创建用于通过 TLS 建立连接的必需设置。Auto-generate: Automatically creates the requisite settings for connecting via TLS.

      • 从目录导入:指定包含以前已保存的一组 TLS 设置的目录。Import from directory: Specifies a directory that contains a set of previously saved TLS settings. 该目录必须包含以下六个文件:The directory must contain the following six files:

        • ca.pemca-key.pem:包含 TLS 证书颁发机构的证书和公钥。ca.pem and ca-key.pem: Contain the certificate and public key for the TLS Certificate Authority.

        • cert.pemkey.pem:包含用于 TLS 身份验证的客户端证书和公钥。cert.pem and key.pem: Contain client certificate and public key which will be used for TLS authentication.

        • server.pemserver-key.pem:包含用于 TLS 身份验证的客户端证书和公钥。server.pem and server-key.pem: Contain the client certificate and public key that is used for TLS authentication.

  7. 输入所需的信息后,单击“完成”。 After you have entered the required information, click Finish.
    此时将再次显示“在 Azure 中部署 Docker 容器”向导。 The Deploy Docker Container on Azure wizard reappears.

    “在 Azure 中部署 Docker 容器”向导

  8. 单击“下一步”。 Click Next.
    此时将打开“配置要创建的 Docker 容器”窗口。 The Configure the Docker container to be created window opens.

    “配置要创建的 Docker 容器”窗口

  9. 在“配置要创建的 Docker 容器”窗口中提供以下信息: In the Configure the Docker container to be created window, provide the following information:

    a.a. 在“Docker 容器名称”框中,输入 Docker 容器的唯一名称。 In the Docker container name box, enter a unique name for your Docker container.

    b.b. 选择以下 Docker 映像之一:Choose one of the following Docker images:

    • 预定义的 Docker 映像:指定 Azure 中预先存在的 Docker 映像。Predefined Docker image: Specify a pre-existing Docker image from Azure.

      备注

      此框中的 Docker 映像列表包括 Azure 工具包已配置为要修补的多个映像,以便能够自动部署项目。The list of Docker images in this box consists of several images that the Azure Toolkit has been configured to patch so that your artifact is deployed automatically.

    • 自定义 Dockerfile:指定本地计算机中以前保存的 Dockerfile。Custom Dockerfile: Specify a previously saved Dockerfile from your local computer.

      备注

      这是一项比较高级的功能,面向想要部署自己的 Dockerfile 的开发人员。This is a more advanced feature for developers who want to deploy their own Dockerfile. 但是,使用此选项的开发人员需负责确保正确生成其 Dockerfile。However, it is up to developers who use this option to ensure that their Dockerfile is built correctly. 由于 Azure 工具包不会验证自定义 Dockerfile 中的内容,因此,如果 Dockerfile 有问题,部署可能会失败。Because the Azure Toolkit does not validate the content in a custom Dockerfile, the deployment might fail if the Dockerfile has issues. 此外,由于 Azure 工具包预期自定义 Dockerfile 中包含 Web 应用项目,因此会尝试打开 HTTP 连接。In addition, because the Azure Toolkit expects the custom Dockerfile to contain a web app artifact, it attempts to open an HTTP connection. 如果开发人员发布不同类型的项目,在部署后可能会收到无实质影响的错误。If developers publish a different type of artifact, they might receive innocuous errors after deployment.

    c.c. 在“端口设置”框中,输入 Docker 容器的唯一 TCP 端口绑定。 In the Port settings box, enter the unique TCP port binding for your Docker container.

  10. 完成前面的步骤后,单击“完成”。 After you have completed the preceding steps, click Finish.

Azure 工具包随即开始在 Docker 容器中将你的 Web 应用部署到 Azure。The Azure Toolkit begins deploying your web app to Azure in a Docker container. 除非已将 IntelliJ 配置为在后台部署,否则会出现“正在部署到 Azure”进度条。 Unless you have configured IntelliJ to be deployed in the background, a Deploying to Azure progress bar appears.

部署进度条

有关创建项目的其他信息Additional information about creating artifacts

若要创建随时可用于部署的项目,请执行以下操作:To create a deployment-ready artifact, do the following:

  1. 在 IntelliJ 中打开你的 Web 应用项目。Open your web app project in IntelliJ.

  2. 依次单击“File”(文件) 、“Project Structure”(项目结构) 。Click File, and then click Project Structure.

    “项目结构”命令

  3. 若要添加项目,请单击绿色加号 ( + ),然后单击“Web 应用程序: 存档”。To add an artifact, click the green plus sign (+), and then click Web Application: Archive.

    “Web 应用程序:存档”命令

  4. 在“名称”框中输入项目的名称(请不要添加 .war 扩展名),然后单击“确定”。 In the Name box, enter a name for your artifact (do not include the .war extension), and then click OK.

    项目名称框

有关在 IntelliJ 中创建项目的详细信息,请参阅 JetBrains 网站上的 Configuring artifacts(配置项目)。For more information about creating artifacts in IntelliJ, see Configuring artifacts on the JetBrains website.

后续步骤Next steps

有关 Docker 的其他资源,请参阅官方 Docker 网站For additional resources for Docker, see the official Docker website.

若要报告 bug 或请求新功能,请在我们的 GitHub 存储库上创建问题。To report bugs or request new features, create issues on our GitHub repository. 或者,在 Stack Overflow 上使用标记 azure-java-tools 提出问题。Or, ask questions on Stack Overflow with tag azure-java-tools.

若要详细了解如何将 Java 与 Azure 配合使用,请参阅以下链接:For more information about using Java with Azure, see the following links: