Skip to content

Commit 3212b0f

Browse files
authored
[DEVCONTAINER] support customization and run as non-root user (open-telemetry#3270)
1 parent a7f9daf commit 3212b0f

File tree

6 files changed

+191
-19
lines changed

6 files changed

+191
-19
lines changed

.devcontainer/Dockerfile.dev

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,54 @@
33

44
FROM otel/cpp_format_tools
55

6+
ARG USER_UID=1000
7+
ARG USER_GID=1000
8+
ARG INSTALL_PACKAGES=
9+
10+
ARG CXX_STANDARD=17
11+
ARG ABSEIL_CPP_VERSION=20230125.3
12+
ARG PROTOBUF_VERSION=23.3
613
ARG GRPC_VERSION=v1.55.0
7-
ARG PROTOBUF_VERSION=23.4
8-
ARG ABSEIL_CPP_VERSION=20240116.1
914

10-
ENV PROTOBUF_VERSION=${PROTOBUF_VERSION}
15+
ENV CXX_STANDARD=${CXX_STANDARD}
1116
ENV ABSEIL_CPP_VERSION=${ABSEIL_CPP_VERSION}
17+
ENV PROTOBUF_VERSION=${PROTOBUF_VERSION}
18+
ENV GRPC_VERSION=${GRPC_VERSION}
1219

1320
COPY ci /opt/ci
1421

1522
RUN apt update && apt install -y wget \
1623
ninja-build \
1724
libcurl4-openssl-dev \
18-
markdownlint
25+
clang-tidy \
26+
shellcheck
1927

2028
RUN cd /opt/ci && bash setup_cmake.sh
2129
RUN cd /opt/ci && bash setup_ci_environment.sh
2230
RUN cd /opt && bash ci/setup_googletest.sh \
23-
&& bash ci/setup_grpc.sh -r ${GRPC_VERSION}
31+
&& bash ci/install_abseil.sh \
32+
&& bash ci/install_protobuf.sh \
33+
&& bash ci/setup_grpc.sh -r $GRPC_VERSION -s $CXX_STANDARD -p protobuf -p abseil
2434

2535
ADD https://github.com/bazelbuild/bazelisk/releases/download/v1.22.1/bazelisk-linux-amd64 /usr/local/bin
2636

2737
RUN git config --global core.autocrlf input \
2838
&& chmod +x /usr/local/bin/bazelisk-linux-amd64
39+
40+
ENV INSTALL_PACKAGES=${INSTALL_PACKAGES}
41+
ENV USER_NAME=devuser
42+
ENV USER_UID=${USER_UID}
43+
ENV USER_GID=${USER_GID}
44+
ENV IS_CONTAINER_BUILD=true
45+
46+
COPY ./.devcontainer/customize_container.sh /tmp/opentelemetry_cpp/devcontainer/customize_container.sh
47+
RUN /tmp/opentelemetry_cpp/devcontainer/customize_container.sh
48+
RUN apt install -y npm && npm install -g markdownlint-cli
49+
50+
USER devuser
51+
52+
WORKDIR /workspaces/opentelemetry-cpp
53+
54+
ENTRYPOINT []
55+
56+
CMD ["/bin/bash"]

.devcontainer/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Customizing Your Dev Container
2+
3+
Customize your dev container using build arguments (for direct Docker builds) or
4+
environment variables (for evaluation in `devcontainer.json`).
5+
6+
* **CXX standard:**
7+
This is the C++ standard to build from (eg: 17, 20, ...). (Default: 17)
8+
* Docker ARG:
9+
`CXX_STANDARD`
10+
* Host Environment Variable:
11+
`OTEL_CPP_DEVCONTAINER_CXX_STANDARD`
12+
13+
* **abseil-cpp version:**
14+
This is the version of abseil-cpp that will be used to build protobuf, gRPC,
15+
and opentelemetry-cpp (when WITH_ABSEIL is set).
16+
* Docker ARG:
17+
`ABSEIL_CPP_VERSION`
18+
* Host Environment Variable:
19+
`OTEL_CPP_DEVCONTAINER_ABSEIL_CPP_VERSION`
20+
21+
* **Protobuf version:**
22+
* Docker ARG:
23+
`PROTOBUF_VERSION`
24+
* Host Environment Variable:
25+
`OTEL_CPP_DEVCONTAINER_PROTOBUF_VERSION`
26+
27+
* **gRPC version:**
28+
* Docker ARG:
29+
`GRPC_VERSION`
30+
* Host Environment Variable:
31+
`OTEL_CPP_DEVCONTAINER_GRPC_VERSION`
32+
33+
* **User ID (UID):**
34+
User ID (Default: `1000`)
35+
* Docker ARG:
36+
`USER_UID`
37+
* Host Environment Variable:
38+
`OTEL_CPP_DEVCONTAINER_USER_UID`
39+
40+
* **Group ID (GID):**
41+
User group ID (Default: `1000`)
42+
* Docker ARG:
43+
`USER_GID`
44+
* Host Environment Variable:
45+
`OTEL_CPP_DEVCONTAINER_USER_GID`
46+
47+
* **Install Packages:**
48+
These are the additional packages that will be installed via `apt install` in the devcontainer. This is a space separated list.
49+
* Docker ARG:
50+
`INSTALL_PACKAGES` (Default: ``)
51+
* Host Environment Variable:
52+
`OTEL_CPP_DEVCONTAINER_INSTALL_PACKAGES` (Default: ``)
53+
54+
## Examples
55+
56+
* `docker build --build-arg CXX_STANDARD="20" --build-arg INSTALL_PACKAGES="nano gitk"...`
57+
* `export OTEL_CPP_DEVCONTAINER_CXX_STANDARD=20`
58+
* `export OTEL_CPP_DEVCONTAINER_INSTALL_PACKAGES="nano gitk"`
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
# Copyright The OpenTelemetry Authors
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
set -eu
7+
8+
if [[ $IS_CONTAINER_BUILD != "true" ]]; then
9+
echo "This script should only run inside a Docker container."
10+
exit 1
11+
fi
12+
13+
if [[ -n "$INSTALL_PACKAGES" ]]; then
14+
packages=($INSTALL_PACKAGES)
15+
for package in "${packages[@]}"; do
16+
apt install -y "$package"
17+
done
18+
fi
19+
20+
if [[ $(id "$USER_NAME" 2>/dev/null) ]]; then
21+
echo "User '$USER_NAME' already exists. Removing it."
22+
userdel -rf "$USER_NAME"
23+
elif [[ $(id -u "$USER_UID" 2>/dev/null) ]]; then
24+
OTHER_USER=$(getent passwd "$USER_UID" | cut -d: -f1)
25+
echo "User '$OTHER_USER' exists with UID $USER_UID. Removing it."
26+
userdel -rf "$OTHER_USER"
27+
fi
28+
29+
if [[ ! $(getent group "$USER_GID" 2>/dev/null) ]]; then
30+
echo "Group '$USER_GID' does not exist. Adding it."
31+
groupadd -g "$USER_GID" "$USER_NAME"
32+
fi
33+
34+
useradd -m -u "$USER_UID" -g "$USER_GID" -s /bin/bash "$USER_NAME"
35+
echo "Created user '$USER_NAME' (UID: $USER_UID, GID: $USER_GID)."
36+
37+
echo "$USER_NAME ALL=(ALL) NOPASSWD:ALL" | tee /etc/sudoers.d/"$USER_NAME"
38+
39+
echo "User and group setup complete."

.devcontainer/devcontainer.json

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,27 @@
88
"context": "..",
99
"dockerfile": "Dockerfile.dev",
1010
"args": {
11-
"GRPC_VERSION": "v1.55.0",
12-
"PROTOBUF_VERSION": "23.4",
13-
"ABSEIL_CPP_VERSION":"20240116.1"
11+
"USER_UID": "${localEnv:OTEL_CPP_DEVCONTAINER_USER_UID:1000}",
12+
"USER_GID": "${localEnv:OTEL_CPP_DEVCONTAINER_USER_GID:1000}",
13+
"INSTALL_PACKAGES": "${localEnv:OTEL_CPP_DEVCONTAINER_INSTALL_PACKAGES:}",
14+
"CXX_STANDARD": "${localEnv:OTEL_CPP_DEVCONTAINER_CXX_STANDARD:17}",
15+
"GRPC_VERSION": "${localEnv:OTEL_CPP_DEVCONTAINER_GRPC_VERSION:v1.55.0}",
16+
"PROTOBUF_VERSION": "${localEnv:OTEL_CPP_DEVCONTAINER_PROTOBUF_VERSION:23.3}",
17+
"ABSEIL_CPP_VERSION":"${localEnv:OTEL_CPP_DEVCONTAINER_ABSEIL_CPP_VERSION:20230125.3}"
1418
}
1519
},
16-
"settings": {
17-
"terminal.integrated.shell.linux": "/bin/sh"
20+
"customizations": {
21+
"vscode": {
22+
"extensions": [
23+
"ms-vscode.cpptools",
24+
"ms-azuretools.vscode-docker",
25+
"ms-vscode.cpptools-extension-pack"
26+
],
27+
"settings": {
28+
"terminal.integrated.shell.linux": "/bin/bash",
29+
}
30+
}
1831
},
19-
"extensions": [
20-
"ms-vscode.cpptools",
21-
"ms-azuretools.vscode-docker",
22-
"ms-vscode.cpptools-extension-pack"
23-
],
2432

25-
"remoteUser": "root"
33+
"remoteUser": "devuser"
2634
}

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Increment the:
2424
* [SDK] Add meter scope configurator
2525
[#3268](https://github.com/open-telemetry/opentelemetry-cpp/pull/3268)
2626

27+
* [DEVCONTAINER] Support customization and run as non-root user
28+
[#3270](https://github.com/open-telemetry/opentelemetry-cpp/pull/3270)
29+
2730
Important changes:
2831

2932
* [SDK] Support OTEL_SDK_DISABLED environment variable

CONTRIBUTING.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ Before getting started, ensure you have the following installed:
8989
files provided (e.g., `.devcontainer/devcontainer.json`). This setup will install
9090
required dependencies, tools, and environment variables needed for the project.
9191

92+
* **Container Customization**:
93+
See `.devcontainer/README.md` for devcontainer configuration options.
94+
9295
#### Available Commands
9396

9497
Once inside the DevContainer, you can use the following commands to run tests
@@ -145,6 +148,33 @@ If you encounter issues:
145148
* **Bazelisk Documentation**: [https://github.com/bazelbuild/bazelisk](https://github.com/bazelbuild/bazelisk)
146149
* **VSCode DevContainer Documentation**: [https://code.visualstudio.com/docs/remote/containers](https://code.visualstudio.com/docs/remote/containers)
147150

151+
### Docker Development Image
152+
153+
The `.devcontainer/Dockerfile.dev`
154+
dockerfile can be built directly with the following command.
155+
156+
```sh
157+
docker build -t opentelemetry-cpp-dev -f ./.devcontainer/Dockerfile.dev .
158+
```
159+
160+
You can customize the image using build arguments
161+
to match permissions with the host user.
162+
163+
```sh
164+
docker build -t opentelemetry-cpp-dev \
165+
--build-arg USER_UID="$(id -u)" \
166+
--build-arg USER_GID="$(id -g)" \
167+
-f ./.devcontainer/Dockerfile.dev .
168+
169+
```
170+
171+
Run an interactive bash session binding your host
172+
opentelemetry-cpp directory to the container's workspace:
173+
174+
```sh
175+
docker run -it -v "$PWD:/workspaces/opentelemetry-cpp" opentelemetry-cpp-dev bash
176+
```
177+
148178
## Pull Requests
149179

150180
### How to Send Pull Requests
@@ -195,6 +225,12 @@ If you made changes to the Markdown documents (`*.md` files), install the latest
195225
markdownlint .
196226
```
197227

228+
If you modified shell scripts (`*.sh` files), install `shellcheck` and run:
229+
230+
```sh
231+
shellcheck --severity=error <path to shell script>.sh
232+
```
233+
198234
Open a pull request against the main `opentelemetry-cpp` repo.
199235

200236
To run tests locally, please read the [CI instructions](ci/README.md).
@@ -271,11 +307,11 @@ the C++ repository.
271307

272308
* [OpenTelemetry
273309
Specification](https://github.com/open-telemetry/opentelemetry-specification)
274-
* The OpenTelemetry Specification describes the requirements and expectations
275-
of for all OpenTelemetry implementations.
310+
* The OpenTelemetry Specification describes the requirements and expectations
311+
of for all OpenTelemetry implementations.
276312

277313
* Read through the OpenTelemetry C++ documentation
278-
* The
314+
* The
279315
[API](https://opentelemetry-cpp.readthedocs.io/en/latest/api/api.html)
280316
and
281317
[SDK](https://opentelemetry-cpp.readthedocs.io/en/latest/sdk/sdk.html)

0 commit comments

Comments
 (0)