Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically pass environment variables to Azure Container Instance

I have a simple bash script which creates a container and runs a task in it:

api=${1}

az container create \
--resource-group "my_resource_group" \
--file container.yaml \
--environment-variables "APIKEY"="${api}"

But the variable APIKEY never gets the value from this call, but always from the "default" value in container.yaml.

How do I create a container instance with the whole definition in .yaml and pass environment variables in the command az container create?

like image 908
user2776801 Avatar asked Aug 01 '26 18:08

user2776801


1 Answers

When input is given as file, environment variables are not considered.

Looking at cli sources, when File is used, it will simply return creating container based on file. https://github.com/Azure/azure-cli/blob/713061e3e4a0fc969b0797ec06bd0e3db49ad4a8/src/azure-cli/azure/cli/command_modules/container/custom.py#L112

What you could do is, use sed in bash script to change APIKey value in your container.yaml file and then deploy it as you are.

sed -i 's/PLACEHOLDER_FOR_APIKEY_HERE/$api/g' container.yaml

PLACEHOLDER_FOR_APIKEY_HERE will be replaced with commandline argument and container.yaml file will be updated.

So the script will look similar to below one.

api=${1}
sed -i 's/PLACEHOLDER_FOR_APIKEY_HERE/$api/g' container.yaml
az container create \
--resource-group "my_resource_group" \
--file container.yaml
like image 73
Iqan Shaikh Avatar answered Aug 04 '26 14:08

Iqan Shaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!