Skip to content

Commit

Permalink
Some more changes in alpine
Browse files Browse the repository at this point in the history
  • Loading branch information
gvatsal60 committed Sep 21, 2024
1 parent 2485813 commit da9f32f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
32 changes: 13 additions & 19 deletions Dockerfiles/scripts/alpine-util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,39 +148,43 @@ if id -u "${USERNAME}" >/dev/null 2>&1; then

if [ -n "${USER_GID}" ] && [ "${USER_GID}" -ne "${current_gid}" ]; then
group_name="$(id -gn "${USERNAME}")"
groupmod --gid "${USER_GID}" "${group_name}"
usermod --gid "${USER_GID}" "${USERNAME}"
addgroup -g "${USER_GID}" "${group_name}" || exit 1
deluser "${USERNAME}" "${group_name}" || exit 1
adduser "${USERNAME}" "${group_name}" || exit 1
fi

if [ -n "${USER_UID}" ] && [ "${USER_UID}" -ne "${current_uid}" ]; then
usermod --uid "${USER_UID}" "${USERNAME}"
# Changing UID directly is not supported; we have to recreate the user
current_groups=$(id -Gn "${USERNAME}" | tr ' ' ',')
deluser "${USERNAME}" || exit 1
adduser -u "${USER_UID}" -G "${current_groups}" -s /bin/sh -D "${USERNAME}" || exit 1
fi
else
# Create user
if [ -n "${USER_GID}" ]; then
groupadd --gid "${USER_GID}" "${USERNAME}"
addgroup -g "${USER_GID}" "${USERNAME}"
else
groupadd "${USERNAME}"
addgroup "${USERNAME}"
fi

if [ -n "${USER_UID}" ]; then
useradd -s /bin/bash --uid "${USER_UID}" --gid "${USERNAME}" -m "${USERNAME}"
adduser -u "${USER_UID}" -G "${USERNAME}" -s /bin/sh -D "${USERNAME}"
else
useradd -s /bin/bash --gid "${USERNAME}" -m "${USERNAME}"
adduser -G "${USERNAME}" -s /bin/sh -D "${USERNAME}"
fi
fi

# Add sudo support for non-root user
if [ "${USERNAME}" != "root" ] && [ "${EXISTING_NON_ROOT_USER}" != "${USERNAME}" ]; then
# Create or update the sudoers file with the correct configuration
echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" | sudo tee "/etc/sudoers.d/${USERNAME}" >/dev/null
echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" | tee "/etc/sudoers.d/${USERNAME}" >/dev/null
chmod 0440 "/etc/sudoers.d/${USERNAME}"
EXISTING_NON_ROOT_USER="${USERNAME}"
fi

# Add ${USER} to 'docker' group if the group exists
if getent group docker >/dev/null; then
usermod -aG docker "${USERNAME}"
addgroup "${USERNAME}" docker || exit 1
else
print_err "==> Error: 'docker' group does not exist."
exit 1
Expand All @@ -193,16 +197,6 @@ else
user_rc_path="/home/${USERNAME}"
fi

# Restore user .bashrc defaults from skeleton file if it doesn't exist or is empty
if [ ! -f "${user_rc_path}/.bashrc" ] || [ ! -s "${user_rc_path}/.bashrc" ]; then
cp /etc/skel/.bashrc "${user_rc_path}/.bashrc"
fi

# Restore user .profile defaults from skeleton file if it doesn't exist or is empty
if [ ! -f "${user_rc_path}/.profile" ] || [ ! -s "${user_rc_path}/.profile" ]; then
cp /etc/skel/.profile "${user_rc_path}/.profile"
fi

# Add RC snippet and custom bash prompt
# Check if the user exists
if id "${USERNAME}" >/dev/null 2>&1; then
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ Welcome! This guide will walk you through the process of setting up Windows Subs
Build and run the Docker containers:
```sh
docker-compose up --build
docker-compose build
```
This command will build the Docker image according to the Dockerfile and start the container as specified in docker-compose.yml.
This command will build the Docker image according to the Dockerfile as specified in docker-compose.yml.
## Additional Resources
Expand Down

0 comments on commit da9f32f

Please sign in to comment.