2017-08-09 71 views
7

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

我想运行相当于: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" 
     } 
    ] 
    }, 
    ... 
} 

回答

1

由于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

+0

这JSON是*响应*从'AZ容器show'命令 – dstj

+0

好了,我在想你所谈论的手臂模板。基本上可以在arm模板中使用以下代码片段来定义容器使用的端口 – 4c74356b41

+0

哦,您能否改进您的答案以提供更完整的示例? – dstj

3

可以,但目前您只能使用Azure资源管理器模板来执行此操作。 CLI和门户都面向简单的案例:container group中的一个容器,以及该容器中的一个暴露端口。

下面是一个蓝色的资源管理器模板的示例资源部(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 
+0

谢谢,如果你之前发布过,你会被接受的答案。但是在解决了@ 4c74356b41的误解之后,他也完成了这项工作... – dstj

+2

对于其他人,您可以使用--ports参数现在执行此操作:https://docs.microsoft.com/en-us/ CLI /天蓝色/容器?鉴于=蔚CLI-最新#az_container_create –

相关问题