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 :)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

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

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay