Azure SDK for Python in Docker container

综合编程 2018-05-26 阅读原文

Using Docker can be really annoying if you are trying to use it for purposes it probably was not designed for - at least in my opinion. But it is really great solution if you don't want to maintain a VM or other virtual environment.

In my case, I have Python script which I need to run periodically. I don't need and I don't want to maintain VM for that. I just want to run this script for time to time. Of course it is not just a script - it has dependencies (Azure SDK for Python), so it's more like a bundle than a script - it's normal for Python and many other languages.

Docker is a perfect solution for me in that case. I can bundle SDK and other dependencies and use it as a base image for my script runtime environment - all without storing any data on image itself.

I have clear prerequisites:

  1. I need Azure SDK for Python.
  2. I have my script written in Python 3.6.
  3. I need to pass some parameters to my script to prevent hardcoding.
  4. I wish to run this script on almost any machine -Linux, Mac and Windows.
  5. I wish to run this script periodically.

Azure SDK for Python

You can find it on PyPi (
) and you can install it using pip install azure
. There is no philosophy here - it's open, it's developed on GitHub and it's available on PyPi.

An additional prerequisite for Azure SDK for Python is keyrings.alt
package - due tothis issue.

So, I have:

pip install azure keyrings.alt

Python 3.6

I'm working on Python 3.6 environment locally on my computer, where I'm developing, so I wish to have the same environment on the script's runtime. It's probably compatible with 3.5, 2.4 and 3.3 but... I'm working on 3.6.

Let see my script (sample) - it is listing all resource groups in my subscription:


As you can see, I'm not hardcoding stuff like tenant, application ID, application key or subscription ID in my script. I'm using os.getenv()
to extract it from environment variables. It means, that I need to include some "sensitive" data in my environment.


I don't want to focus on the question if my script will run on Windows Server or Linux... or Mac, that I'm using personally. Python is Python but... environments differs between Operating Systems. And this is the place where Docker comes to the game. it does not matter where you will run your Docker image - it will be totally the same from the code/script perspective.

We have two options in Docker world - first is to use pre-built images, prepared by community or team-mates; second is to use custom images we are building by our own.

If you are looking for ready-to-use images, check on Docker Hub
or Docker Store

But if you are looking for more flexible solution or you just want to have a lot of fun, try to build your own Docker image, using Dockerfile. As I assumed above, we need Python 3.6, azure
package and keyrings.alt
package. Let's create Dockerfile for that:

As you can see, it's really simple. We are getting Python image with tag pointing for version 3.6 from Docker Hub - a community repository - and it is an official Python image for docker. You can check it here

The second step is to install packages wee need on top of python image. To do that, we are using pip
of course. Image building means, that having Python 3.6 image, we are installing additional packages we want, and then we are generating new image based on the base one and changes we made. After that, we have a static environment image with Python 3.6 and Azure SDk for Python.

Complex Docker image

Having our script and a Docker image based on Python 3.6 official image, we can prepare a complex Docker image with ready-to-use solution. We need to merge the script with the environment image. We will do it using Dockerfile, adding script to it:


Assuming, that we have created image above, we have a complex solution: we have a Python 3.6 interpreter, an Azure SDK for Python and keyrings.alt
package. But when this image will runs, it will do... nothing. The script is inside, but the command is not declared. We need to declare what the image will do on startup:

And this is a complete solution. On startup, conainer will run
script, using Python 3.6 interpreter, where Azure SDk for Python is installed along with keyrings.alt


At this point, we need to build our Docker imageand to do so we need to have
script and a Dockerfile
in the same directory. Using shell where docker is installed - no matter on what OS - go to this directory and perform a command:

docker build -t imagename .

We have built the Docker image based on definition from Dockerfile, tagging it as "imagename" which tag will be used as the image name when running.


No we know three things:

  1. We have an "imagename" Docker image.
  2. We want to run
    script which is on that image.
  3. We need to "inject" environment variables with sensitive data to the runtime.

script will run automatically because we built an image in that way. Only thing we need is to pass environment variables to the image on startup. To do it, we will use -e
arguments to the *docker run * command. Let's do it:

docker run -e "TENANT_ID=" -e "CLIENT=" -e "KEY=" -e "SUBSCRIPTION=" imagename:latest


The script should run and should start treating Docker as your daily-basis tool. Not because it's fancy and cool. Just because it's easy to use, simple and it works almost everywhere.

If you don't want to wait for image to build with SDK, I have created a ready-to-use image. You can find it here
and use as a base:

责编内容by:Linux on Azure 【阅读原文】。感谢您的支持!


Lessons Learned: Installing SkFlow SkFlow is the scikit-learn interface to Google’s opensource AI library TensorFlow. Installation started off straightforward enough and simply install...
BrandPost: Raise Your Hand and Ask: What’s R? Note: Most people don’t want to be the uncool one to raise their hand and ask a question, but in many cases we really should. These occasional ‘R...
使用docker+jenkins构建nodejs前端项目 前文 使用Docker搭建Jenkins+Docker持续集成环境 我们已经搭建了基于docker+jenkins的持续集成环境,并构建了基于maven的项目。这一节,我们继续扩展功能,增加对Nodejs的支持,实现nodejs项目构建、并打包成docker镜像和自动部署。 1. 配置Nod...
如何迈出 Python 学习第一步? 编者按:朋友圈转发过的Python学习教程是否已经被你遗忘在收藏夹里?原作者William Koehrsen是一名应用数据科学研究员、超级马拉松亚军,本文是他学习Python的入门心得,或许能够帮助迟迟未行动的你。 回忆我的第一个Python程序 出于怀旧的缘故,我想分享我两年前的第...
Python之深浅拷贝 拷贝就是拷贝,何来深浅之说? Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果 其实这个是由于共享内存导致的结果 浅拷贝 l1 = ] l2 = l1.copy() print(l2) #] l2= print(l1) ...