Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ ENV PS_VERSION=${PWSH_LTS_VERSION}
ENV PS_MAJOR_VERSION=${PWSH_LTS_MAJOR_VERSION}
ENV PS_INSTALL_FOLDER=/opt/microsoft/powershell/${PS_MAJOR_VERSION}

# If exists, remove 'ubuntu' user
RUN if id "ubuntu" &>/dev/null; then \
echo "Deleting user 'ubuntu'" && userdel -f -r ubuntu || echo "Failed to delete ubuntu user"; \
else \
echo "User 'ubuntu' does not exist"; \
fi;

# Install sudo and other necessary packages
RUN apt-get update \
&& apt-get -y install --no-install-recommends \
Expand All @@ -48,8 +41,10 @@ RUN apt-get update \
wget

# Set US English and UTF-8 Locale
RUN localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG=en_US.utf8
RUN sed -i '/^# *en_US.UTF-8 UTF-8/s/^# *//' /etc/locale.gen \
&& locale-gen en_US.UTF-8 \
&& update-locale LANG=en_US.UTF-8
ENV LANG=en_US.UTF-8

# Defining non-root User
ARG USERNAME=coder
Expand All @@ -58,10 +53,28 @@ ARG USER_GID=$USER_UID

# Set up User and grant sudo privileges
# apt-get package: sudo
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID --shell /bin/bash --create-home $USERNAME \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME
RUN existing_group="$(getent group "$USER_GID" | cut -d: -f1 || true)" \
&& if [ -n "$existing_group" ]; then \
[ "$existing_group" = "$USERNAME" ] || groupmod --new-name "$USERNAME" "$existing_group"; \
else \
groupadd --gid "$USER_GID" "$USERNAME"; \
fi \
&& existing_user="$(getent passwd "$USER_UID" | cut -d: -f1 || true)" \
&& if [ -n "$existing_user" ]; then \
if [ "$existing_user" = "$USERNAME" ]; then \
usermod --gid "$USER_GID" --shell /bin/bash --home "/home/$USERNAME" "$USERNAME"; \
else \
usermod --login "$USERNAME" --home "/home/$USERNAME" --move-home --gid "$USER_GID" --shell /bin/bash "$existing_user"; \
fi; \
elif id "$USERNAME" &>/dev/null; then \
usermod --uid "$USER_UID" --gid "$USER_GID" --shell /bin/bash --home "/home/$USERNAME" "$USERNAME"; \
else \
useradd --uid "$USER_UID" --gid "$USER_GID" --shell /bin/bash --create-home "$USERNAME"; \
fi \
&& mkdir -p "/home/$USERNAME" \
&& chown "$USER_UID":"$USER_GID" "/home/$USERNAME" \
&& echo "$USERNAME ALL=\(root\) NOPASSWD:ALL" > "/etc/sudoers.d/$USERNAME" \
&& chmod 0440 "/etc/sudoers.d/$USERNAME"
WORKDIR /home/$USERNAME

# Clean up
Expand Down
Loading