Securing Container Instances and Container Apps
This lesson explains the identity and network settings of the serverless container platforms.
Learning outcomes
After completing this lesson, you will be able to:
- Identify the requirements of ACI in a virtual network.
- Describe managed identities on ACI.
- Choose Container Apps exposure controls.
- Describe Easy Auth for Container Apps.
Azure Container Instances
Azure Container Instances (ACI) runs container groups on demand. It suits short tasks, build jobs and simple applications. Each container group has hypervisor-level isolation. By default, a group can have a public IP address and DNS label, and security work mainly concerns taking it off the internet and giving it an identity.
ACI networking
Deploying into a virtual network places a container group in a delegated subnet, which can hold only container groups and must be in the same subscription. Several requirements follow from this, and they are easily tested.
- Outbound traffic must use a NAT gateway, which is the only supported configuration.
- Public IP addresses and DNS labels are not supported in a virtual network.
- Global peering is not supported, and images must come from ACR rather than other private registries.
- A subnet of at least /24 is recommended, as a smaller one can run out of space.
- Outbound ports 25 and 19390 are not supported, and 19390 is needed for connections from the Azure portal.
Network profiles were retired as of API version 2021-07-01, so templates should not use them.
ACI in a virtual network
ACI identity and secrets
A container group can use a managed identity, system-assigned or user-assigned, set with --assign-identity. Changing identities restarts the group. In Linux containers, code reaches the metadata endpoint at 169.254.169.254. Windows containers have no such endpoint, and use the IDENTITY_ENDPOINT and IDENTITY_HEADER variables instead, and Windows containers in a virtual network cannot reach the identity endpoint at all.
Secure environment variables keep values out of the container's properties, and a secret volume mounts sensitive values as files. Both are better than plain environment variables, but a managed identity with Key Vault is better still, since no secret need be passed. Confidential containers on ACI add hardware-based protection.
Passing secrets to containers
Azure Container Apps
Azure Container Apps runs microservices on a managed platform. Apps live in an environment, which is the network and logging boundary. Networking is decided when the environment is created, and it cannot be changed later. The workload profiles environment is the default, and it supports user-defined routes, NAT gateway outbound traffic and private endpoints, with a minimum subnet of /27. The older consumption-only environment needs a /23 and lacks those features.
The following table shows how to limit exposure.
| Control | What it does | Note |
|---|---|---|
| Internal environment | Gives the environment no public endpoint | Set public network access to Disabled |
| Ingress setting | Limits an app to its environment, or exposes it externally | Set per app |
| IP restrictions | Allow or deny by IPv4 CIDR | Allow and deny rules cannot be combined |
| Easy Auth | Adds sign-in in front of the app | HTTPS only; allowInsecure should be off |
| Private endpoint | Gives private inbound access | Workload profiles only |
If there are no IP rules, all inbound traffic is allowed. To switch from deny rules to allow rules, delete the deny rules first. Blocked clients see RBAC: Access Denied.
Securing Container Apps
Container Apps identity and authentication
An app can have a system-assigned identity and several user-assigned identities, and adding or removing one does not restart the app. Managed identities can pull images from a private ACR with no stored credentials, and that should be the normal design. In a consumption-only environment, init containers cannot use managed identities. An identitySettings lifecycle value of Init, Main, All or None can limit when an identity is available.
Easy Auth runs as a sidecar in front of the app, with providers such as Microsoft Entra ID, GitHub, Google and custom OpenID Connect. It passes identity details in headers such as X-MS-CLIENT-PRINCIPAL-NAME. Set restricted access to Require authentication. Application code should trust those headers only if the app can be reached solely through the platform's ingress.
az containerapp ingress access-restriction set --resource-group RG --name APP \
--rule-name office --ip-address 203.0.113.0/24 --action Allow --description "Office range"A private batch container
A nightly job runs in Azure Container Instances, and must reach a private database in a virtual network. It must not have a public address, and it needs a fixed outbound address for a partner's allow list. It runs in a Linux container and needs a secret from Key Vault. What should you configure?
Deploy the group into a delegated subnet, which removes the public address, and use a NAT gateway, which gives a fixed outbound address and is the only supported outbound option. Assign a managed identity with Key Vault Secrets User, and pull images from ACR with it.
A public IP address with an NSG is not available to a group in a virtual network. Passing the secret as a plain environment variable would expose it in the group's properties. A Windows container would have trouble reaching the identity endpoint in a virtual network, but this job is Linux.
A container group in a virtual network needs a public IP address. Is this possible?
Show answer
No. Public IP addresses and DNS labels are not supported for groups in a virtual network.
Exam Tips
Remember for the exam
- ACI in a virtual network needs a NAT gateway for outbound traffic.
- Container Apps networking cannot be changed after creation.
- Allow and deny IP rules cannot be combined in Container Apps.
- Windows containers in a virtual network cannot reach the ACI identity endpoint.