Skip to content

Commit d5b4be8

Browse files
authored
Merge pull request #655 from serenity-js/copilot/update-devcontainer-user-settings
Run devcontainer as non-root serenity-js user
2 parents ea9bf88 + 1455cd0 commit d5b4be8

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

.devcontainer/Dockerfile

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ FROM mcr.microsoft.com/playwright:v1.56.1-noble
33
ARG DEBIAN_FRONTEND=noninteractive
44
ARG TZ=UTC
55

6+
# Add user args (defaults can be overridden via devcontainer.json build.args)
7+
ARG USERNAME=serenity-js
8+
ARG USER_UID=1000
9+
ARG USER_GID=1000
10+
611
ENV SHELL=/bin/bash
712
ENV PATH="/opt/google/chrome:${PATH}"
13+
ENV HOME=/home/${USERNAME}
814

915
RUN \
10-
# Install Java
16+
# Install Java, chrome, edge etc.
1117
apt-get -y update && \
1218
apt-get -y install default-jre && \
1319
# Install Chrome
@@ -28,4 +34,22 @@ RUN \
2834
apt-get clean && \
2935
rm -rf /var/lib/apt/lists/*
3036

37+
# Create a non-root user to run the workspace (more secure than using root)
38+
RUN set -eux; \
39+
# create group (if it doesn't already exist) and user with specified uid/gid
40+
if ! getent group ${USER_GID} >/dev/null 2>&1; then \
41+
groupadd --gid ${USER_GID} ${USERNAME}; \
42+
fi; \
43+
if ! id -u ${USERNAME} >/dev/null 2>&1; then \
44+
useradd -m -s /bin/bash -u ${USER_UID} -g ${USER_GID} ${USERNAME}; \
45+
fi; \
46+
# Ensure /workspaces exists and is owned by the non-root user so postCreateCommand can write
47+
mkdir -p /workspaces; \
48+
chown -R ${USERNAME}:${USERNAME} /workspaces /home/${USERNAME}
49+
50+
# Copy welcome message for Codespaces/Dev Containers
3151
COPY welcome-message.txt /usr/local/etc/vscode-dev-containers/first-run-notice.txt
52+
53+
# Switch to the non-root user for the running container
54+
USER ${USERNAME}
55+
WORKDIR /workspaces

.devcontainer/devcontainer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
{
22
"build": {
33
"context": ".",
4-
"dockerfile": "Dockerfile"
4+
"dockerfile": "Dockerfile",
5+
"args": {
6+
"USERNAME": "serenity-js",
7+
"USER_UID": "1000",
8+
"USER_GID": "1000"
9+
}
510
},
611

712
"features": {
@@ -48,5 +53,7 @@
4853
}
4954
}
5055
}
51-
}
56+
},
57+
58+
"remoteUser": "serenity-js"
5259
}

0 commit comments

Comments
 (0)