Skip to main content

az-container-helper

Helper classes and methods for using Azure services from inside a local Docker container.

Why another helper package for azure? Well, I found none that addresses the issue that it is not possible to access Azure Services from inside a local Docker container, see

This is unfortunate, because it makes running your Python app inside a local container (e.g. for testing) impossible, if it uses Azure Services. The workarounds from the github issue only work, if you install the azure cli tools inside your container, which will not only increase the container size by a huge amount, but the cli tools are also strictly unnecessary when running your container on the Azure cloud.

Therefor this package offers some minimal wrapper classes and utils around the default Python Azure SDK, so you can use Azure services in your Python app also from inside a local container.

Usage

Retrieving credentials

To retrieve credentials for your app you can use a wrapper function in your Python app

from az_container_helper import get_credentials

credentials = get_credentials()

It will first try to read an existing access token from the ACCESS_TOKEN env variable. If no existing token is found it will fall back to standard DefaultAzureCredential workflow.

To make use of that wrapper when running in a local docker container first generate an access token for your azure service (e.g. for a key vault) using the az cli tools and export it as ACCESS_TOKEN env variable.

az login
export ACCESS_TOKEN=$(az account get-access-token --resource https://vault.azure.net --output tsv)

Then you can run your app inside a local docker container and inject the token as env variable in the container

docker run -e ACCESS_TOKEN=$ACCESS_TOKEN your-image

Retrieving secrets from an Azure KeyVault

To conveniently access secret from an Azure KeyVault, this package offers a wrapper class. The usage is quite simple. Here is this example we retrieve an api key from the Azure Key Vault and use it to initialize an OpenAI client.

from pathlib import Path
from az_container_helper.keyvault import KeyVault
from openai import AzureOpenAI

kv_url = "https://<your-keyvault-name>.vault.azure.net"
secrets_path = Path.cwd() / ".secrets"

kv = KeyVault(kv_url, secrets_path = secrets_path)

apikey = kv.get_secret("<name-of-secret>")

openai_client = AzureOpenAI(api_key=apikey.get_secret_valu()...)

Our KeyVault class makes use of the get_credentials() function. So to connect to the KeyVault from your local container you can inject an ACCESS_TOKEN as shown above.

However in case you cannot create an ACCESS_TOKEN locally (e.g. because you are not allowed to install az cli tools on your developers machine) you simply can't access the KeyVault from your local machine. In that case our KeyVault class will try to read secrets from a .secrets file, so this file acts an alternative local KeyVault. The format of the file is simply

name_of_secret=value_of_secret
name_of_secret_2=value_of_secret_2
...

This file can simply be copied into your local DockerContainer in the working directory of the application. With that you can run your app inside a local container even without any local access to the Azure KeyVault.

Metadata

Release files for az-container-helper 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for az-container-helper 0.1.3
File Size Uploaded
az_container_helper-0.1.3.tar.gz 5.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for az-container-helper 0.1.3
File Interpreter ABI Platform
az_container_helper-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 10.7 kB

Release files / az_container_helper-0.1.3.tar.gz

Download URL az_container_helper-0.1.3.tar.gz
Size 5.1 kB
Tags Source
SHA-256 checksum
How to use checksums
77df363adba9d783736396e26ebb2bc3879ba8f35a3d0dca272197a50313f8c8
BLAKE2b-256 checksum
How to use checksums
95d204020e8e518ca449f50c541c0d65c08cf0866e5ecf4c67f7316ff40d0df3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.7

Release files / az_container_helper-0.1.3-py3-none-any.whl

Download URL az_container_helper-0.1.3-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d959451bd65ebf9347c24d58a97118b28b44652cdb6d3f89690b39ab13b84aa7
BLAKE2b-256 checksum
How to use checksums
a1fb9619790399d9e102bc4356eecbdbb8f4884a799f7eb970aa2ea7db2f68d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.7

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page