DEV Community

Theodor Heiselberg
Theodor Heiselberg

Posted on

1

Install azure-cli in an alpine container

This was a tad more complicated than i hoped

Running as root

The standard setup:

{
    "name": "azure-cli-alpine",
    "build": { "dockerfile": "Dockerfile"}
}
Enter fullscreen mode Exit fullscreen mode

Now for the complex part:

FROM alpine:3.21.0

# Install system dependencies
RUN apk add --no-cache --update \
  python3 \
  py3-pip \
  gcc \
  musl-dev \
  python3-dev \
  libffi-dev \
  openssl-dev \
  cargo \
  make

# Create and activate a virtual environment for Azure CLI
RUN python3 -m venv /opt/venv \
  && . /opt/venv/bin/activate \
  && pip install --upgrade pip \
  && pip install --no-cache-dir azure-cli \
  && deactivate

# Clean up unnecessary build tools
RUN apk del \
  gcc \
  musl-dev \
  python3-dev \
  libffi-dev \
  openssl-dev \
  cargo \
  make \
  && rm -rf /var/cache/apk/*

# Update PATH to include the virtual environment
ENV PATH="/opt/venv/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

Up and running!

Now we can run eg: az login :D

Running as non-root

Personally I prefer not to run my containers as root - therefore:

{
    "name": "azure-cli-alpine",
    "build": { "dockerfile": "Dockerfile" },
    "remoteUser": "container-user"
}
Enter fullscreen mode Exit fullscreen mode
FROM alpine:3.21.0

# Install system dependencies
RUN apk add --no-cache --update \
  python3 \
  py3-pip \
  gcc \
  musl-dev \
  python3-dev \
  libffi-dev \
  openssl-dev \
  cargo \
  make

# Create and activate a virtual environment for Azure CLI
RUN python3 -m venv /opt/venv \
  && . /opt/venv/bin/activate \
  && pip install --upgrade pip \
  && pip install --no-cache-dir azure-cli \
  && deactivate

# Clean up unnecessary build tools
RUN apk del \
  gcc \
  musl-dev \
  python3-dev \
  libffi-dev \
  openssl-dev \
  cargo \
  make \
  && rm -rf /var/cache/apk/*

# Create a non-root user set up permissions and add folders the non root user can't create
ARG USERNAME=container-user
ARG USER_UID=1000
ARG USER_GID=$USER_UID

RUN addgroup -g 1000 $USERNAME \
  && adduser -u 1000 -G $USERNAME -h /home/$USERNAME -D $USERNAME\
  && mkdir -p /home/$USERNAME/.azure \
  && chown -R $USERNAME:$USERNAME /home/$USERNAME

# Update PATH to include the virtual environment
ENV PATH="/opt/venv/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

Enjoy :)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay