Skip to content

Commit efdf2b8

Browse files
Copilotrjaegers
andcommitted
Add non-root 'code' user to both C++ and Rust devcontainers
Co-authored-by: rjaegers <[email protected]>
1 parent 9effda6 commit efdf2b8

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

.devcontainer/cpp/Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,32 @@ RUN --mount=type=cache,target=/var/log,sharing=locked \
116116
&& update-alternatives --install /usr/bin/python python /usr/bin/python3 10 \
117117
&& cp /etc/skel/.bashrc /root/.bashrc
118118

119+
# Create the 'code' user
120+
RUN groupadd --gid 1000 code \
121+
&& useradd --uid 1000 --gid code --shell /bin/bash --create-home code \
122+
&& cp /etc/skel/.bashrc /home/code/.bashrc \
123+
&& chown -R code:code /home/code
124+
119125
# Set up package managers CPM and Conan
120126
# - Install CPM.cmake to the CMake module path
121127
# - Configure a default profile for Conan and set the CMake generator to Ninja
122128
RUN --mount=type=cache,target=/cache,sharing=locked \
123129
wget --no-hsts -qP /usr/local/lib/python*/dist-packages/cmake/data/share/cmake-*/Modules/ https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_VERSION}/CPM.cmake \
124130
&& conan profile detect \
125131
&& echo -e "\n[conf]\ntools.cmake.cmaketoolchain:generator=Ninja" >> "$(conan profile path default)"
132+
133+
# Create cache directories with appropriate permissions for the code user
134+
RUN mkdir -p /cache/.ccache /cache/.cpm /cache/.python \
135+
&& chown -R code:code /cache \
136+
&& chmod -R 755 /cache
137+
138+
# Set up conan for the code user
139+
USER code
140+
RUN conan profile detect \
141+
&& echo -e "\n[conf]\ntools.cmake.cmaketoolchain:generator=Ninja" >> "$(conan profile path default)"
142+
143+
# Switch back to root temporarily for any remaining setup
144+
USER root
145+
146+
# Set the default user
147+
USER code

.devcontainer/cpp/devcontainer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"dockerfile": "Dockerfile",
44
"context": "../.."
55
},
6+
"remoteUser": "code",
67
"forwardPorts": [
78
6080
89
],

.devcontainer/rust/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,15 @@ RUN --mount=type=cache,target=/var/log,sharing=locked \
6767
# Install additional rust tools
6868
RUN wget -qO - "https://github.com/cargo-bins/cargo-binstall/releases/download/v${CARGO_BINSTALL_VERSION}/cargo-binstall-$(uname -m)-unknown-linux-gnu.tgz" | tar xz -C "/usr/bin" \
6969
70+
71+
# Create the 'code' user
72+
RUN groupadd --gid 1000 code \
73+
&& useradd --uid 1000 --gid code --shell /bin/bash --create-home code \
74+
&& cp /etc/skel/.bashrc /home/code/.bashrc \
75+
&& chown -R code:code /home/code
76+
77+
# Ensure the code user has access to cargo and rustup
78+
RUN chmod -R a+rX /usr/local/cargo /usr/local/rustup
79+
80+
# Set the default user
81+
USER code

.devcontainer/rust/devcontainer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"dockerfile": "Dockerfile",
44
"context": "../.."
55
},
6+
"remoteUser": "code",
67
"privileged": true,
78
"remoteEnv": {
89
"CONTAINER_FLAVOR": "rust"

0 commit comments

Comments
 (0)