diff --git a/Dockerfile b/Dockerfile index 7a636cc..5b4eec6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 \ @@ -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 @@ -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