2017-08-09 110 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 –

相關問題