Last active
June 9, 2020 15:00
-
-
Save ppanyukov/e265870d57304e354fa59b7c9249ae93 to your computer and use it in GitHub Desktop.
How to run Azure CLI in docker as if it's locally install and not notice a difference :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to use dockerised version Azure CLI (or anything else) | |
# in a totally transparent way. This solves several challenges: | |
# - Persistent login. Don't want to login every time. | |
# - Container startup time. It can be noticable. | |
# - Access to host's disk. | |
# - Run without ever using "docker" command. | |
# | |
# WINDOWS NOTE: | |
# - You *will* run into volume mapping issues on Windows, sadly. | |
# - Either remove any args with '-v` and 'v` and then everything | |
# should broadly work, but you will not have access to host file system. | |
# - OR. Try to figure out how to make it work and suggest a patch, | |
# this will be much appreciated. | |
# | |
# Essentially this makes 'az' command look and behave completely | |
# like it's a locally installed utility and not running in docker at all! | |
# First, pin the Azure CLI to correct version. | |
IMAGE=mcr.microsoft.com/azure-cli:2.7.0 | |
# Now make az image always run with home dir mapped into container. | |
# Anything you do in the running container will persist: | |
# - any changes to mapped volume will be reflected permanently; | |
# - any changes in other directories will live until the container is deleted. | |
# | |
# This means your login to azure will persist until the the container is stoped and removed. | |
# This will remove the need to login every single time. | |
docker run \ | |
-d \ | |
--name az \ | |
-v "${HOME}":"${HOME}":cached \ | |
-w "${HOME}" \ | |
"${IMAGE}" \ | |
bash -c 'while true; do sleep 5; done' | |
# Now login to Azure. | |
docker exec az az login | |
# Verify the login has persisted | |
docker exec az az account show | |
# Obviously using 'docker exec' every time is utter nonsense. | |
# So we make a bash alias for az command. | |
# Use '-w' to exec this in the current host directory. | |
# This will work as long as we are under $HOME on the host | |
# which should be the case most of the time. And we have the entire $HOME | |
# mapped as volume into the container. | |
# To make this persistent, add to ~/.bashrc or something. | |
alias az="docker exec -w \"${PWD}\" az az" | |
# Now every time you run 'az', you will run it in docker :) | |
# To run host-installed az use backslash prefix for az '\az' | |
# You can temporarily stop the container if you want. | |
# The Azure login is still persisted until container is actually deleted. | |
# Obviously the 'az' alias will now produce error. | |
docker kill az | |
# You can restart the container again. | |
docker restart az | |
# Verify that you are still logged in | |
az account show | |
# Completely zap the container when not needed. | |
docker kill az || true; docker rm az || true | |
# The same concepts apply to all command line utilities pretty much. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment