Skip to content
Open
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
22 changes: 16 additions & 6 deletions .docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ ARG USERNAME=user

WORKDIR /workspaces

# Delete existing user if it exists
RUN if getent passwd ${USER_UID}; then \
userdel -r $(getent passwd ${USER_UID} | cut -d: -f1); \
fi

# Delete existing group if it exists
RUN if getent group ${USER_GID}; then \
groupdel $(getent group ${USER_GID} | cut -d: -f1); \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this exactly do?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first checks if there is an existing group for the provided USER_GID in the docker image:

❯ getent group 100;
users:x:100:

The second gets the group name:

❯ getent group 100 | cut -d: -f1
users

and deletes it from the docker image.

This is to ensure that when creating our own user/group in the following command it'll work regardless of whether the user's UID/GID matches with existing users in the base docker image.

fi

RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
&& mkdir -p -m 0700 /run/user/"${USER_UID}" \
&& mkdir -p -m 0700 /run/user/"${USER_UID}"/gdm \
&& chown user:user /run/user/"${USER_UID}" \
&& chown user:user /workspaces \
&& chown user:user /run/user/"${USER_UID}"/gdm
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
&& mkdir -p -m 0700 /run/user/"${USER_UID}" \
&& mkdir -p -m 0700 /run/user/"${USER_UID}"/gdm \
&& chown $USERNAME:$USERNAME /run/user/"${USER_UID}" \
&& chown $USERNAME:$USERNAME /workspaces \
&& chown $USERNAME:$USERNAME /run/user/"${USER_UID}"/gdm

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
Expand Down