Execute Azure CLI command at the same time as docker run, and discard container when execution is completed.
How to execute az command using official docker image
Azure CLI has an official Docker image. microsoft / azure-cli
Using this image, I thought I would execute the az command with docker run, but when I looked at how to use it, the az command prompt just started up, and then I had to execute the command manually. Usage is like this image.
- docker run execution
$ docker run -it microsoft/azure-cli
bash-4.4#
- az login manual execution
bash-4.4 # is the login
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and the code xxxxxxx to authenticate.
- Arbitrary az command execution
bash-4.4# az vm list
In this way, since it cannot be executed from the Bot or placed on the CI if it is interactive, we made an image that executes and destroys the target command at the time of docker run.
Run the az command at the same time as docker run to destroy the container
- Create a Dockerfile.
- Create service principals in advance.
- Build
$ docker build -f Dockerfile -t azure-cli-centos:v1.0 .
- Execute the az command. If you write “Secret”, the history will remain, so you should write it in a separate file and read it at runtime.
Summary
Although there was a sense of force, I was able to execute the az command at the same time as docker run. As with the official image, the image size is quite large (less than 500MB). I tried to reduce the weight with Alpine Linux, but I was unable to install Azure CLI, so I settled on CentOS.
Postscript: alpine version of Dockerfile
I was told the alpine version of Dockerfile by comment. With reference to the contents you received, Dockerfile was created as follows so that CLI can be executed simultaneously with docker run.By using alpine, the image size can be reduced from less than 500MB to about 220MB.