In your code, when you create an Azure container with a managed identity that is being created at the ACI creating time to authenticate to ACR. I am afraid that you can not do that because there are limitations
You can’t use a managed identity to pull an image from Azure Container Registry when creating a container group. The identity is only available within a running container.
CLICK HERE to find out more related problems solutions.