commit a481704293c466da215db1466907b64d8d383ada Author: Slawomir Koszewski Date: Fri Dec 12 12:06:26 2025 +0100 Adding sources. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..008f39d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,40 @@ +FROM ubuntu:24.04 + +# Add essential packages +RUN apt-get update && apt-get upgrade -y && apt-get install -y curl jq gpg software-properties-common apt-transport-https ca-certificates + +# Install Terraform +RUN curl -sL https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o - > /usr/share/keyrings/hashicorp-archive-keyring.gpg +RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com noble main" > /etc/apt/sources.list.d/hashicorp.list +RUN apt-get update && apt-get install -y terraform +RUN cat < /etc/bash_completion.d/terraform +complete -C /usr/bin/terraform terraform +EOF + +# Install Azure CLI +RUN curl -sL -o /tmp/packages-microsoft-prod.deb https://packages.microsoft.com/config/ubuntu/24.04/packages-microsoft-prod.deb && \ + dpkg -i /tmp/packages-microsoft-prod.deb && rm -f /tmp/packages-microsoft-prod.deb + +RUN echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/microsoft-prod.gpg] https://packages.microsoft.com/repos/azure-cli/ noble main" > /etc/apt/sources.list.d/azure-cli.list +RUN apt-get update && apt-get install -y azure-cli + +# Configure Bash +RUN cat <<'EOF' >> /etc/bash.bashrc +if [ -f /usr/lib/git-core/git-sh-prompt ]; then + source /usr/lib/git-core/git-sh-prompt + + # Then PS1 can include __git_ps1 which is optimized and shows branch + state: + PS1='\[\e[32m\]\u@\h\[\e[0m\]:\[\e[34m\]\w\[\e[0m\]\[\e[33m\]$(__git_ps1 " (%s)")\[\e[0m\]\$ ' +fi + +export PATH=$HOME/bin:$PATH +EOF + +# Add more packages (keep it last to optimize layer caching) +RUN apt-get install -y zip unzip tree wget nano neovim \ + python3 python3-venv python3-pip + +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] + diff --git a/README.md b/README.md new file mode 100644 index 0000000..b842df2 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Azure CLI container + + diff --git a/azure-cli b/azure-cli new file mode 100755 index 0000000..e3ac688 --- /dev/null +++ b/azure-cli @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +while [ $# -gt 0 ]; do + case $1 in + --account|-a) + ACCOUNT_NAME="$2" + shift 2 + ;; + --user|-u) + USERNAME="$2" + shift 2 + ;; + *) + break + ;; + esac +done + +IMAGE_NAME="skoszewski/azure-cli:latest" + +# Normalize account name for use in volume name +ACCOUNT_NAME="${ACCOUNT_NAME:-$(id -un)}" +ACCOUNT_NAME="${ACCOUNT_NAME/@/_at_}" +ACCOUNT_NAME="${ACCOUNT_NAME//[-.]/_}" +USERNAME="${USERNAME:-ubuntu}" + +# Find container runtime +if command -v podman &> /dev/null; then + CMD="podman" + HOSTNAME_ARG="--hostname $(hostname -s)" +elif command -v docker &> /dev/null; then + CMD="docker" + HOSTNAME_ARG="--hostname $(hostname -s)" +elif command -v container &> /dev/null; then + # Apple container command line tool + CMD="container" + HOSTNAME_ARG="" +else + echo "Error: No usable container runtime was found." >&2 + exit 1 +fi + +# Check, if the account volume exists, if not create it +# This ensures persistent storage for the Azure CLI configuration +if ! $CMD volume inspect "account_$ACCOUNT_NAME" &> /dev/null; then + echo "Creating volume account_$ACCOUNT_NAME for Azure CLI configuration." + if ! $CMD volume create "account_$ACCOUNT_NAME" &> /dev/null; then + echo "Error: Failed to create volume account_$ACCOUNT_NAME." >&2 + exit 1 + fi +fi + +# Run the container +$CMD run --rm -it \ + -v "account_$ACCOUNT_NAME":/home/${USERNAME} \ + -v $(pwd):/workdir \ + --name "azure-cli" \ + $HOSTNAME_ARG \ + --workdir /workdir \ + "$@" \ + $IMAGE_NAME diff --git a/build b/build new file mode 100755 index 0000000..0152167 --- /dev/null +++ b/build @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +# Find container runtime +if command -v podman &> /dev/null; then + CMD="podman" +elif command -v docker &> /dev/null; then + CMD="docker" +elif command -v container &> /dev/null; then + # Apple container command line tool + CMD="container" +else + echo "Error: No usable container runtime was found." >&2 + exit 1 +fi + +echo "Using container runtime: $CMD" +$CMD build -t skoszewski/azure-cli:latest . diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..4a3326e --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,43 @@ +#!/usr/bin/bash + +set -e + +# Let's set some defaults +SU_USER="ubuntu" + +# Parse command line arguments +while [ $# -gt 0 ]; do + case "$1" in + -u|--user) + SU_USER="$2" + shift 2 + ;; + *) + break + ;; + esac +done + +# Check if the user exists +if ! id -u "$SU_USER" >/dev/null 2>&1; then + useradd -m "$SU_USER" +fi + +# Check if the home directory exists +if [ ! -d "/home/$SU_USER" ]; then + echo "A home directory for $SU_USER does not exist. Exiting." + exit 1 +fi + +# Check user ownership of the home directory +if [ "$(stat -c '%u' /home/$SU_USER)" -ne "$(id -u $SU_USER)" ]; then + # Change ownership of the home directory + chown $SU_USER:$SU_USER /home/$SU_USER +fi + +# Switch to the specified user +if [ $# -gt 0 ]; then + exec su $SU_USER -c "$@" +else + exec su $SU_USER +fi