Fixes for Apple container.
All checks were successful
Build Docker Image / build (push) Successful in 16s

This commit is contained in:
2025-12-21 22:14:21 +01:00
parent e6b4fdec1a
commit 68f0d2efc9
2 changed files with 33 additions and 35 deletions

View File

@@ -2,11 +2,13 @@
# Set default values # Set default values
ACCOUNT_NAME="$(id -un)" ACCOUNT_NAME="$(id -un)"
USER_NAME="ubuntu" USER_NAME="${USER_NAME:-ubuntu}"
EXTRA_ARGS=() EXTRA_ARGS=()
LIST="" LIST=""
VOLUME_NAME="" VOLUME_NAME=""
HOME_DIR="/home/${USER_NAME}"
# Parse command line arguments
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case $1 in case $1 in
--list|-l) --list|-l)
@@ -30,6 +32,11 @@ while [ $# -gt 0 ]; do
EXTRA_ARGS+=("--mount" "type=bind,source=${VOL_SRC},target=${VOL_DST}") EXTRA_ARGS+=("--mount" "type=bind,source=${VOL_SRC},target=${VOL_DST}")
shift 2 shift 2
;; ;;
--root)
USER_NAME="root"
HOME_DIR="/root"
shift
;;
--) --)
# Stop parsing arguments # Stop parsing arguments
shift shift
@@ -114,9 +121,7 @@ if [ ! "FOUND" = "$_FOUND" ]; then
# Volume created, initialize it # Volume created, initialize it
echo "Initializing volume $VOLUME_NAME." echo "Initializing volume $VOLUME_NAME."
if ! $CMD run --rm -it \ if ! $CMD run --rm -it --mount "type=volume,source=$VOLUME_NAME,target=$HOME_DIR" $IMAGE_NAME; then
--mount "type=volume,source=$VOLUME_NAME,target=/home/${USER_NAME}" \
skoszewski/azure-cli:latest --user "$USER_NAME"; then
echo "Error: Failed to initialize volume $VOLUME_NAME." >&2 echo "Error: Failed to initialize volume $VOLUME_NAME." >&2
exit 1 exit 1
fi fi
@@ -127,10 +132,17 @@ EXTRA_ARGS+=(
"--mount" "type=volume,source=$VOLUME_NAME,target=/home/${USER_NAME}" "--mount" "type=volume,source=$VOLUME_NAME,target=/home/${USER_NAME}"
"--mount" "type=bind,source=$(pwd),target=/workdir" "--mount" "type=bind,source=$(pwd),target=/workdir"
"--env" "ACCOUNT_NAME=$ACCOUNT_NAME" "--env" "ACCOUNT_NAME=$ACCOUNT_NAME"
"--env" "USER_NAME=$USER_NAME"
"--env" "HOME_DIR=$HOME_DIR"
"--name" "azure-cli-$VOLUME_NAME" "--name" "azure-cli-$VOLUME_NAME"
"--user" "$USER_NAME"
"--workdir" "/workdir" "--workdir" "/workdir"
) )
if [ "$USER_NAME" != "root" ]; then
EXTRA_ARGS+=(
"--user" "$USER_NAME"
)
fi
# Run the container as the specified user # Run the container as the specified user
$CMD run --rm -it ${EXTRA_ARGS[@]} $IMAGE_NAME --user "$USER_NAME" "$@" $CMD run --rm -it ${EXTRA_ARGS[@]} $IMAGE_NAME "$@"

View File

@@ -3,47 +3,33 @@
set -e set -e
# Setup default values # Setup default values
USER_NAME="ubuntu" USER_NAME="${USER_NAME:-ubuntu}"
HOME_DIR="${HOMED_DIR:-/home/${USER_NAME}}"
# Parse arguments
while [ $# -gt 0 ]; do
case "$1" in
--user|-u)
USER_NAME="$2"
shift 2
;;
--)
# Stop parsing arguments
shift
break
;;
*)
break
;;
esac
done
# Check, if we are running as root # Check, if we are running as root
if [ "$(id -u)" -eq 0 ]; then if [ "$(id -u)" -eq 0 ]; then
# Check, if the home directory exists for the specified user # Check, if the home directory exists for the specified user
if [ ! -d "/home/${USER_NAME}" ]; then if [ ! -d "$HOME_DIR" ]; then
echo "Error: Home directory for user '${USER_NAME}' does not exist." >&2 echo "Error: Home directory for user '${USER_NAME}' does not exist." >&2
exit 1 exit 1
fi fi
echo "Preparing home directory for user '${USER_NAME}' at '${HOME_DIR}'."
# Check, ownership of the home directory # Check, ownership of the home directory
OWNER_UID=$(stat -c '%u' "/home/${USER_NAME}") if [ "$(stat -c '%u' "$HOME_DIR")" -eq 0 ]; then
OWNER_GID=$(stat -c '%g' "/home/${USER_NAME}") # The home directory is a fresh volume owned by root, change ownership
if [ "${OWNER_UID}" -ne 0 ] || [ "${OWNER_GID}" -ne 0 ]; then echo "Changing ownership of home directory to user '${USER_NAME}'."
# The home directory is not owned by the specfied user, correct it chown "${USER_NAME}:${USER_NAME}" "$HOME_DIR"
chown "${USER_NAME}:${USER_NAME}" "/home/${USER_NAME}"
fi fi
if [ "$USER_NAME " != "root" ]; then
# Re-initialize the contents of the home directory # Re-initialize the contents of the home directory
su - "${USER_NAME}" -c "cp -a /etc/skel/. /home/${USER_NAME}/" su - "${USER_NAME}" -c "cp -a /etc/skel/. $HOME_DIR"
# We are done as root, quit. The container will be re-run as the specified user. # We are done as root, quit. The container will be re-run as the specified user.
exit 0 exit 0
fi
fi fi
# Verify that we are running as the user owning the home directory # Verify that we are running as the user owning the home directory