Location>code7788 >text

Azure Developer] Explanation of questions about the Execute command to go to the Azure Container Instance service using the Python SDK.

Popularity:837 ℃/2024-08-13 20:57:17

Introduction to Azure Container Instance Services

Azure Container Instances (ACI) is a serverless container solution that allows users to run Docker containers in Azure cloud environments without having to set up virtual machines, clusters, or orchestrators.

ACI is suitable for any scenario where you can operate in isolated containers, including event-driven applications, rapid deployment from container development pipelines, data processing, and generating jobs.

 

The purpose of the Execute command is to execute commands in a running container instance.

This is particularly useful during application development and troubleshooting, with the most common use being to launch an interactive shell to debug issues in a running container

Description of the problem

utilizationAfter the command line az container exec, you will enter this container and have a window to interact with it.

The commands are as follows:

az container exec --resource-group <container_instance_rg> --name <testcontainer-instance> --container-name <testcontainer-instance> --exec-command "/bin/bash"

The results are as follows:

However, if you call the Python SDK and the REST API to call Execute to execute a command, what you get back is really a webSocketUri plus password JSON object.

Could this be the difference between the two ways of executing the same command? Why does it behave so differently?

 

Problem solving

After research and studiesContainers - Execute Command - REST API (Azure Container Instances) | Microsoft Learn The documentation, az container exec, is like the Python SDK in that it invokes the REST API to execute commands. The CMD window that executes the az command automatically parses the webSockerUri and Password returned by the command, so it displays an interactive page.

 

However, if you use the REST API or Python SDK, you need to return the request as a Web Socket JSON object and communicate with the target container via websocket by executing the following JavaScript script in the browser's console:

wsUri = '<webSocketUri>';
wsPass = '<password>';
var aWebSocket = new WebSocket(wsUri);
(wsPass);

You can see the root@xxxx : content in the result after execution. This is the same as the result of the command window!

 

So, the final conclusion is: Python SDK and az container exec results are exactly the same, just the execution method is different, Python SDK returns the results need to use websocket way to interact!

 

bibliography

 

Containers - Execute Command : /en-us/rest/api/container-instances/containers/execute-command?view=rest-container-instances-2023-05-01&tabs=Python#containerexec

Web Socket Authentication :/en/stable/topics/