In general, I would start the docker instance on my local machine like docker run -t -i -e 'a=b' ...
Now, I would like to deploy and run my custom docker image which I uploaded to the Docker Container Registry before and start it like the command above - with environment variables.
Checking the Azure CLI for WebApps you can see that setting environment variables in general should be possible. But for me it seems this "environment variables" are not the environment variables which are passed to the docker command. Why? Checking the container protocol I can see how the docker container is started. There are no environment variables set.
With Azure Container, it would work like this az container create ... --environment-variables a=b. These environment variables are passed down to the container/docker. And this is exactly what I am searching for WebApps.
Does anyone have some experience in deploying Azure Webapps with customer Docker instances started with environment variables?
I guess I found the solution for the problem:
App Settings are injected into your app as environment variables at runtime.
If you need to set an environment variable for your application, simply add an App Setting in the Azure portal. When your app runs, we will inject the app setting into the process as an environment variable automatically.
How it works via CLI:
az webapp config appsettings set --name <mycontainername> --resource-group <myresourcegroupname> --settings a='b'
Setting all environment variables via CLI like the command above worked for me. The same is possible via the portal UI in app settings. If you check how Azure starts the Docker instance, you will see that none of the set environment variables are set during startup (like.docker run -d -p 3287:3000 --name <mycontainername -e a=b) but if you login to the Docker container and run an echo command for the environment variable, you will see that the environment variable has been injected.
Note: Maybe you have to restart the Docker instance in order to have the new environment variables injected.
Related
I'm using an Azure App Service to run my docker image. Running my docker container requires using a couple of environment variables. Locally, when I run my container I just do something like:
docker run -e CREDENTIAL -e USERNAME myapp
However, in the Azure App Service, after defining CREDENTIAL and USERNAME as Application Settings, I'm unsure how to pass these to the container. I see from the logs that on startup Azure passes some of its own environment variables, but if I add a startup command with my environment variables, it tacks it on at the end of the one generated by Azure creating an invalid command. How can I pass mine to the container?
As I understand you want to set environment variables in that docker container with -e option.
You don't need to use startup command for that. Pass these variables as application settings:
Application Settings are exposed as environment variables for access by your application at runtime.
Documentation
I've created a Node 14 Web app on Azure. For Publish, I chose Code (not Container). Then I've changed the default container image with az webapp config container set. Question, how can I revert that to the default image (which was NODE|14-lts)?
Running az webapp config container set --docker-custom-image-name 14-lts resets it to DOCKER|14-lts, which is not the same.
Running az webapp config container set --docker-custom-image-name "NODE|14-lts" produces an error.
We have tested in our local environment(created a webapp with Linux as runtime ) below findings are based on the analysis.
If you are using docker container as publish mode then LinuxFxVersion value will be "DOCKER|node:14-lts" you cannot change the value of LinuxFxVersion to "NODE|14-LTS".
If you want "NODE|14-LTS" value in LinuxFxVersion in the site properties
you need change the publish mode from docker container to code mode by using the below cmdlet.
az webapp config set --name <webappName> --resource-group <resourceGroupName> --linux-fx-version 'Node|14-LTS'
I am running an R Shiny app inside a container in Azure Container Instance. Via a DevOps pipeline whenever I change the source code of my App, I recreate the container in the Build pipeline and update the Container Instance via an Azure Cli command in the Release pipline via az container create and az container restart.
After spinning it up I need to run a bash command - namely, automatically adjusting a file within the created container.
In local Docker this would be
docker exec {containerName} /bin/bash -c "echo `"var1 = \`"val1`"`" >> /home/shiny/.Renviron"
This means: run a bash command in the container to push some text into the .Renviron file within the container.
Now the Azure Container Instance says you cannot pass command arguments for az container exec: https://learn.microsoft.com/en-us/azure/container-instances/container-instances-exec
How would you then in an automated build/release process in Azure go for building, releasing and configuring a container?
I do not want to set those values within the build pipeline as I want to use the same image for different staging areas setting those values accordingly.
Many thanks in advance for your help.
I am very new to azure container instances, so I may not understand your aims but it seems another way of achieving this:
I do not want to set those values within the build pipeline as I want
to use the same image for different staging areas setting those values
accordingly.
could be to modify your parameter values upon container creation using the --command-line flag mentioned here. Something like
az container create -g MyResourceGroup --name myapp --image myimage:latest --command-line "/bin/sh -c '/path to/myscript.sh val1 val2'"
in which myscript.sh runs your app with the indicated values.
Is it possible to exec a command in a windows container related to a webapp based on a container? I'm not able to find the right name to use on the cli command az app exec. In fact az container list returns me an empty list. However the container exits cause the web app is running. Where can I find the right name to use with az app exec command? I also checked with the name of only one slot in the webapp without success.
The error az app exec returns is:
The Resource 'Microsoft.ContainerInstance/containerGroups/theNameIPRovide' under resource group 'thegroupNameIProvide' was not found.
Unfortunately, there is no Azure CLI command for Web app for Container to exec the command inside the container. But you can use other ways, for example, the API.
There is no CLI command like az app exec. And az container is for ACI, not for the Web App. You can get all CLI command for Web App in az webapp.
Or you can deploy your application in Azure Container Instance, it also can run windows container for you. So that you can use the CLI command az container exec to execute the command inside your application.
Could you have a look at this https://learn.microsoft.com/en-us/cli/azure/container?view=azure-cli-latest#az-container-exec, Couldnt comment, so had to post it in an answer
I used following quick start doc to spin up my first Azure container.
https://learn.microsoft.com/en-us/azure/container-instances/container-instances-quickstart#feedback
It worked fine. but how do I connect to container if I want to debug something?
You cannot connect to the container itself directly to debug, IE you can't SSH or RDP to it. Take a look at this graphic which highlights how a container differs from virtual machines:
You can however pull logs from your container from the container engine. In your case you would want to use the following command in the Azure CLI: az container logs.
https://aka.ms/container_logs
When you invoke CLI through the Portal, you should already be connected through your subscription.To debug or troubleshoot you can look at the container logs. Check out this documentation for the exact commands
https://learn.microsoft.com/en-us/cli/azure/container?view=azure-cli-latest#az-container-logs
When I am building containers to run on ACI, I build them first in a local docker instance where they can be connected to and interactively debugged. When you're happy with how they run locally push them into ACI, and debug from the output logs if needed.
I get to the bash shell in my Azure containers by either the azure-cli package, as the OP noted in a comment:
az container exec --exec-command "/bin/bash"
Or by navigating to a container instance in the Azure portal, then under Settings/Containers there is a "Connect" tab: