In this scenario Docker is used for running scripts that manages Azure resources. All files in local repository are mounted in local folder in docker. The code can be run locally in docker or directly from docker shell.
The advantages of this solution is to separate the environment and it can be run anywhere where docker is supported (Linux, Windows).
Steps:
- Prepare Dockerfile file
- Get Azure credentials and setup environment variables
- Build a docker image
- Run script in doker
1. Dockerfile preparation
Create repository folder named “azure”. Then in that folder create the file Dockerfile. Then update the file with all required settings in our example:
Python version
First decide what Python versions are available in docker official images. In this example Python 3.7 is used:
FROM python:3.7
Code repository
Define the current working directory and copy all code from local repository. Working repository is “azure” and all files will be copied from local folder to “azure” folder in docker:
WORKDIR /azure COPY . /azure
Microsoft Azure SDK for Python
Azure SDK for Python provides set of packages that is used to manage Azure resources like networks, storage, virtual machines and so on. It is developed in Gighub and available here: https://github.com/Azure/azure-sdk-for-python
To install Azure SDK add in Dockerfile the following:
pip install azure
Define script that will be executed
Create the Python file list_regions.py in azure folder that will be doing some tasks on Azure account (list regions in this example):
from azure.common.credentials import ServicePrincipalCredentials from azure.mgmt.resource import SubscriptionClient import os AZ_TENANT_ID = os.getenv('AZ_TENANT_ID') AZ_CLIENT_ID = os.getenv('AZ_CLIENT_ID') AZ_CLIENT_SECRET = os.getenv('AZ_CLIENT_SECRET') AZ_SUBSCRIPTION = os.getenv('AZ_SUBSCRIPTION') credentials = ServicePrincipalCredentials( client_id=AZ_CLIENT_ID, secret=AZ_CLIENT_SECRET, tenant=AZ_TENANT_ID ) client = SubscriptionClient(credentials) regions = client.subscriptions.list_locations(AZ_SUBSCRIPTION) for location in regions: print(location.name)
In Dockerfile define what script will be executed during docker run:
CMD ["python"] ["./list_regions.py"]
Final Dockerfile
This is the final Dockerfile file:
FROM python:3.7 WORKDIR /azure COPY . /azure pip install azure CMD ["python"] ["./list_regions.py"]
All files in azure folder:
$ ls -a . .. .idea Dockerfile list_regions.py $
2. Azure Credentials and environment variables
For the Azure management there is needed Azure credentials like SUBSCRIPTION, TENANT, CLIENT and KEY. To get these items visit the Azure portal. More information how to get these credentials visit here.
Add these credentials to your shell profile like .bash_profile and export:
# Azure Credentials export AZ_SUBSCRIPTION="XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" export AZ_CLIENT_ID="f844c3e6-0502-4685-8acb-2477e01fd1d7" export AZ_CLIENT_SECRET="Pyf8.zKBs54NT*3M?Fa?iq92Rx_lDL69" export AZ_TENANT_ID="a50ba86d-ee84-42a6-9fb3-811fb481daeb"
Once running the docker these credentials will be copied to docker and exported.
3. Build docker image
To build a docker run docker command in the same directory where there is Dockerfile and python file:
$ docker build -t azure1 .
This command creates a docker container. The option “-t” adds a tag and this tag is a name of a docker image.
4. Run
Run command after docker start
The script list_regions.py will be executed on docker and exit. If there is needed to do some update in script the docker needs to be re-created. To run the script add environment options to pass Azure credential variables:
$ docker run -e AZ_SUBSCRIPTION=${AZ_SUBSCRIPTION} -e AZ_TENANT_ID=${AZ_TENANT_ID} -e AZ_CLIENT_ID=${AZ_CLIENT_ID} -e AZ_CLIENT_SECRET=${AZ_CLIENT_SECRET} azure1:latest eastasia southeastasia centralus eastus eastus2 westus northcentralus southcentralus northeurope westeurope japanwest japaneast brazilsouth australiaeast australiasoutheast southindia centralindia westindia canadacentral canadaeast uksouth ukwest westcentralus westus2 koreacentral koreasouth francecentral francesouth australiacentral australiacentral2 southafricanorth southafricawest $
Run command from docker and mount local repository
To have option to update the script and automatically upload to docker run this command:
$ docker run -it --entrypoint /bin/bash -v /Users/dragan/PYPROJECTS/azure:/azure -e AZ_SUBSCRIPTION=${AZ_SUBSCRIPTION} -e AZ_TENANT_ID=${AZ_TENANT_ID} -e AZ_CLIENT_ID=${AZ_CLIENT_ID} -e AZ_CLIENT_SECRET=${AZ_CLIENT_SECRET} azure1:latest root@a5e41a5bae7f:/azure# root@a5e41a5bae7f:/azure# python list_regions.py eastasia southeastasia centralus eastus eastus2 westus northcentralus southcentralus northeurope westeurope ... root@a5e41a5bae7f:/azure#
Options:
-i - Keep STDIN open even if not attached
-t - Allocate a pseudo-TTY
--entrypoint - Overwrite the default ENTRYPOINT of the image (there will be run /bin/bash)
-v - Bind mount a volume (mount local volume to volume in docker)
-e - Set environment variables
If you wan to make script modifications all changes will be instantly done in docker as well.
Leave a Reply