This was a tad more complicated than i hoped
Running as root
The standard setup:
{
    "name": "azure-cli-alpine",
    "build": { "dockerfile": "Dockerfile"}
}
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"
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"
}
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"
Enjoy :)
 

 
    
Top comments (0)