How to expose multiple ports on Azure Container Instance?
Asked Answered
M

4

16

Is it possible to expose/open more than one port on an Azure Container Instance? I've only been able to open one port per container.

I'd like to run the equivalent of: docker run -p 80:80 -p 443:443 ...

I've unsuccessfully tried:

  • Maps only the last port: az container create ... --port 80 --port 443
  • Syntax error: az container create ... --port 80 443

But the resource JSON seems to indicate that an array is possible:

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"
      }
    ]
  },
  ...
}
Mckenney answered 9/8, 2017 at 16:53 Comment(0)
H
7

Since ports ( indicated by [] ) property is an array you can add more elements to it:

{
  "$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

Deploy template:
https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-create-first-template#deploy-template

Hamish answered 9/8, 2017 at 18:3 Comment(4)
well, i was thinking you are talking about the arm template. basically uoi can use the following snippet in the arm template to define the ports used by the containerHamish
Oh, can you improve your answer to provide a more complete example?Mckenney
I probably shouldn't since the ridiculous down vote, but that's stack overflow for youHamish
Thanks, how do you "send" that template to Azure?Mckenney
A
25

This can now be done via Azure CLI. Example is below:

az container create -g MyResourceGroup --name myalpine --image alpine:latest --ip-address public --ports 80 443

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

**update: you can set the protocol now as well. TCP or UDP in both the cli and the portal.

i.e.

[--ports]
[--protocol {TCP, UDP}]
Avelin answered 8/2, 2018 at 22:35 Comment(5)
This is the simplest and best solution now that it is a CLI option to list them as space separated values.Deport
Only works if ports are of the same protocol. If you want to open port 50 for both UDP and TCP connections, you are out of luck.Jasisa
@MaximV.Pavlov that is not accurate at this point in time. Perhaps in 2018 that was the case. Just for FYI you can set TCP or UDP in both the CLI and the portal. [--ports] [--protocol {TCP, UDP}]Antiperspirant
Ahh, i see what you mean if you want to use 2 of the same port different protocolAntiperspirant
can this only be done at create time? Can one not edit it afterwards?H
H
7

Since ports ( indicated by [] ) property is an array you can add more elements to it:

{
  "$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

Deploy template:
https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-create-first-template#deploy-template

Hamish answered 9/8, 2017 at 18:3 Comment(4)
well, i was thinking you are talking about the arm template. basically uoi can use the following snippet in the arm template to define the ports used by the containerHamish
Oh, can you improve your answer to provide a more complete example?Mckenney
I probably shouldn't since the ridiculous down vote, but that's stack overflow for youHamish
Thanks, how do you "send" that template to Azure?Mckenney
D
5

You can, but currently you can only do it with an Azure Resource Manager template. The CLI and the portal are both oriented towards the simple case: one container in the container group, and one exposed port in that container.

Here's an example resources section from an Azure Resource Manager template (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"
                    }
                ]
             }
        }
    }
]

You can deploy the template using az group deployment create (full documentation):

az group deployment create -n myDeployment --template-file azuredeploy.json --parameters @azuredeploy.parameters.json -g myResourceGroup
Damian answered 16/8, 2017 at 19:56 Comment(3)
Thanks, had you posted that before, you would have been the accepted answer. But after working out the misunderstanding with @4c74356b41, his also got the job done...Mckenney
For anyone else, you can do this with the --ports parameter now: learn.microsoft.com/en-us/cli/azure/…Linneman
@SeanMckenna, running this today I see 2 ports in the properties of the container however only one port is open, port 80.I can't telnet to 443. Can you comment/clarify whether this used to work and now doesn't please?Clinkstone
R
4

Now Azure Portal provides way to add two extra ports. All you need is to say "Yes" to Open additional ports in Configuration while creating ACI. See Image below.

enter image description here

Rolfe answered 24/9, 2018 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.