如何在Azure容器实例上公开多个端口?

时间:2017-08-09 16:53:42

标签: azure azure-container-instances

是否可以在Azure Container Instance上公开/打开多个端口?我只能为每个容器打开一个端口。

我希望运行相当于:docker run -p 80:80 -p 443:443 ...

我尝试失败了:

  • 仅映射最后一个端口:az container create ... --port 80 --port 443
  • 语法错误:az container create ... --port 80 443

但是资源JSON似乎表明数组是可能的:

az container show -name <container-name> --resource-group <resource-group-name>

Response: 
{
  "containers": [
    {
      ...
      "name": "...",
      "ports": [
        {
          "port": 80
        }
      ...
    }
  ],
   ...
  "ipAddress": {
    "ip": "xxx.xxx.xxx.xxx",
    "ports": [
      {
        "port": 80,
        "protocol": "TCP"
      }
    ]
  },
  ...
}

4 个答案:

答案 0 :(得分:15)

现在可以通过Azure CLI完成此操作。示例如下:

  

az容器创建-g MyResourceGroup --name myalpine --image   alpine:最新--ip-address public --ports 80 443

https://docs.microsoft.com/en-us/cli/azure/container?view=azure-cli-latest#az_container_create

答案 1 :(得分:4)

由于ports(由[]表示)属性是一个数组,您可以添加更多元素:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {    
    "name": {
        "type": "string",
        "defaultValue": "acilinuxpublicipcontainergroup"
    },    
    "image": {        
        "type": "string",
        "defaultValue": "microsoft/aci-helloworld"
    },
    "port": {
        "type": "string",
        "defaultValue": "80"
    },    
    "cpuCores": {
        "type": "string",
        "defaultValue": "1.0"
    },
    "memoryInGb": {
        "type": "string",
        "defaultValue": "1.5"
    }
  },
  "resources": [
    {
            "name": "[parameters('name')]",
            "type": "Microsoft.ContainerInstance/containerGroups",
            "apiVersion": "2017-08-01-preview",
            "location": "[resourceGroup().location]",
            "properties": {
                "containers": [
                    {
                        "name": "[parameters('name')]",
                        "properties": {
                            "image": "[parameters('image')]",
                            "ports": [
                                {
                                    "port": "[parameters('port')]" 
                                }
                            ],
                            "resources": {
                                "requests": {
                                    "cpu": "[parameters('cpuCores')]",
                                    "memoryInGb": "[parameters('memoryInGb')]"
                                }
                            }
                        }
                    }
                ],
                "osType": "Linux",
                "ipAddress": {
                    "type": "Public",
                    "ports": [
                        {
                            "protocol": "tcp",
                            "port": "[parameters('port')]"
                        },
                        {
                            "protocol": "tcp",
                            "port": "[parameters('port2')]"
                        }
                    ]
                 }
            }
        }
    ]
}

https://github.com/Azure/azure-quickstart-templates/tree/master/101-aci-linuxcontainer-public-ip

部署模板:
https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-create-first-template#deploy-template

答案 2 :(得分:4)

您可以,但目前只能使用Azure Resource Manager模板执行此操作。 CLI和门户都面向简单的情况:container group中有一个容器,容器中有一个暴露端口。

以下是Azure资源管理器模板(see full template)中的示例资源部分:

"resources": [
{
        "name": "myContainerGroup",
        "type": "Microsoft.ContainerInstance/containerGroups",
        "apiVersion": "2017-08-01-preview",
        "location": "[resourceGroup().location]",
        "properties": {
            "containers": [
                {
                    "name": "myContainer",
                    "properties": {
                        "image": "seanmckenna/aci-helloworld-multiport",
                        "ports": [
                            {
                                "port": "80" 
                            },
                            {
                                "port": "443"
                            }
                        ],
                        "resources": {
                            "requests": {
                                "cpu": "1.0",
                                "memoryInGb": "1.5"
                            }
                        }
                    }
                }
            ],
            "osType": "Linux",
            "ipAddress": {
                "type": "Public",
                "ports": [
                    {
                        "protocol": "tcp",
                        "port": "80"
                    },
                    {
                        "protocol": "tcp",
                        "port": "443"
                    }
                ]
             }
        }
    }
]

您可以使用az group deployment createfull documentation)部署模板:

az group deployment create -n myDeployment --template-file azuredeploy.json --parameters @azuredeploy.parameters.json -g myResourceGroup

答案 3 :(得分:1)

现在,Azure Portal提供了添加两个额外端口的方法。创建ACI时,只需在配置中说“ ”以打开其他端口。请参阅下面的图像。

enter image description here