Search code examples
python-3.xazuredockerpipdockerfile

Azure CLI in a Docker Container


I have an Ubuntu 18.04 Docker image that I need Azure CLI installed in. For a Docker image, it seems the preferred way is to use pip, however, I have other pip Azure libraries needed in the container that overlap with the CLI install and get blown away because Azure CLI requires older versions; then making it so I can't run my python scripts.

I have tried to use the script installer but that hasn't worked and errored out because I believe the install is interactive.

The last option I can find is the manual apt install, though I am not sure this is a correct way nor do I have a good idea of how to replicate that in a Dockerfile.

Is there a preferred/good way of getting Azure CLI in a container not using pip?

    FROM ubuntu:18.04

    RUN apt-get update && apt-get -y upgrade && \
        apt-get -f -y install curl python3-pip python-pip && \
        pip3 install --upgrade pip && \
        pip2 install --upgrade pip && \
        pip3 install azure-storage-blob==12.3.0 & \\
        pip3 install azure-cli

Solution

  • I have a preference to use the package manager to install dependencies, it's why I will do something like that:

    • Add base dependencies for https repostory and curl
    • Add the gpg key and repository for the CLI
    • Add the CLI

    This is the Dockerfile with thoses steps:

    FROM ubuntu:18.04
    RUN apt-get update && apt-get -y upgrade && \
        apt-get -f -y install curl apt-transport-https lsb-release gnupg python3-pip python-pip && \
        curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > /etc/apt/trusted.gpg.d/microsoft.asc.gpg && \
        CLI_REPO=$(lsb_release -cs) && \
        echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ ${CLI_REPO} main" \
        > /etc/apt/sources.list.d/azure-cli.list && \
        apt-get update && \
        apt-get install -y azure-cli && \
        rm -rf /var/lib/apt/lists/*
    

    In addition, I clean up the apt cache by removing /var/lib/apt/lists. Tt reduces the image size, since the apt cache is not stored in a layer.