Skip to content

Commit 8cb1ef4

Browse files
dariusarnoldkon-fooNeroBurner
authored
Add Dockerfile and devcontainer.json (#174)
This allows both interactive use in IDEs/editors with dev container support as well as building inside the dev container from the command line. The changes are based on https://github.com/kon-foo/InfiniSim/tree/docker-builder-image. The original PR was extended with the following features: - ccache and ninja are installed for optional faster builds. Using ninja instead of make speeds up the build from ~ 28 s to 17 s on my machine with 16 threads. - git and sudo are added for interactive use of the devcontainer - Store bash history in volume to preserve it across rebuilds - The non-root user infinitime with sudo password "it" was added and is used to avoid producing build artifacts owned by root - Split args into CMake generation time and CMake build time args - Allow passing the build directory in docker run - Change the container user to ubuntu:ubuntu since that one already exists anyway. There were also permission issues stemming from not using ubuntu since the external user would likely have UID 1000 (at least on single user systems). That would lead to external files mapped into the container being owned by ubuntu, not infinitime, which has UID 1001 - Split installation into three sections to increase layer granularity for caching - Remove node install script after usage to reduce container size - Add gdb to allow debugging inside of the dev container - Set name of dev container bash history volume to infinisim-bashhistory Add a workaround for error "Status 500: unable to find user ubuntu: no matching entries in passwd file" when build dev container in CLion Clion tries to read the local /etc/password when building to change the UID of ther user in the container to the one from outside. If your /etc/passwd does not have this entry, it will fail to build. Comment this in if you do want to use the dev container CLion (at least CLion 2024.3.4, Build #JBC-243.25659.42. It is reported to Jetbrains). Co-authored-by: kon-foo <25391223+kon-foo@users.noreply.github.com> Co-authored-by: NeroBurner <pyro4hell@gmail.com>
1 parent 914dc95 commit 8cb1ef4

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

.devcontainer/Dockerfile

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
FROM ubuntu:24.04
2+
3+
# Install apt dependencies
4+
ARG DEBIAN_FRONTEND=noninteractive
5+
ARG NODE_MAJOR=20
6+
RUN apt-get update -qq \
7+
&& apt-get install -y \
8+
curl \
9+
cmake \
10+
ccache \
11+
libsdl2-dev \
12+
g++ \
13+
gdb \
14+
git \
15+
libpng-dev \
16+
ninja-build \
17+
sudo \
18+
python3-pip \
19+
python3-venv \
20+
&& rm -rf /var/cache/apt/* /var/lib/apt/lists/*
21+
22+
# Install nodejs based dependencies
23+
RUN curl -sL https://deb.nodesource.com/setup_${NODE_MAJOR}.x -o nodesource_setup.sh \
24+
&& bash nodesource_setup.sh \
25+
&& apt-get update -qq \
26+
&& apt-get install -y nodejs \
27+
&& npm install -g \
28+
lv_font_conv@1.5.2 \
29+
&& rm nodesource_setup.sh \
30+
&& rm -rf /var/cache/apt/* /var/lib/apt/lists/*
31+
32+
# Install Python dependencies
33+
RUN pip install --break-system-packages \
34+
wheel \
35+
Pillow \
36+
&& pip cache purge
37+
38+
# The user ubuntu already exists set its password to ubuntu and add to sudo group for developing in devcontainer
39+
RUN usermod -aG sudo ubuntu \
40+
&& echo "ubuntu:ubuntu" | chpasswd
41+
42+
# Persist bash history across container rebuilds
43+
# Reference: https://code.visualstudio.com/remote/advancedcontainers/persist-bash-history
44+
ARG USERNAME=ubuntu
45+
RUN SNIPPET="export PROMPT_COMMAND='history -a' && export HISTFILE=/commandhistory/.bash_history" \
46+
&& mkdir /commandhistory \
47+
&& touch /commandhistory/.bash_history \
48+
&& chown -R $USERNAME /commandhistory \
49+
&& echo "$SNIPPET" >> "/home/$USERNAME/.bashrc"
50+
51+
USER ubuntu
52+
53+
# Section for interactive compilation during docker run
54+
55+
WORKDIR /sources
56+
# Directory if InfiniTime source code
57+
ENV InfiniTime_DIR="/sources/InfiniTime"
58+
# Passed to CMake generate step
59+
ENV GENERATE_ARGS=""
60+
# Passed to CMake build step
61+
ENV BUILD_ARGS=""
62+
# Build directory
63+
ENV BUILD_DIRECTORY="build"
64+
65+
CMD ["bash", "-c", "cmake -S . -B ${BUILD_DIRECTORY} -G Ninja -DInfiniTime_DIR=${InfiniTime_DIR} ${GENERATE_ARGS} \
66+
&& cmake --build ${BUILD_DIRECTORY} ${BUILD_ARGS}"]

.devcontainer/devcontainer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// For format details, see https://aka.ms/devcontainer.json.
2+
{
3+
"name": "InfiniSim Dev Container",
4+
"build": {
5+
"dockerfile": "Dockerfile"
6+
},
7+
8+
// Configure tool-specific properties.
9+
"customizations": {
10+
"vscode": {
11+
// Add the IDs of extensions you want installed when the container is created.
12+
"extensions": [
13+
"ms-vscode.cpptools",
14+
"ms-vscode.cmake-tools",
15+
"notskm.clang-tidy",
16+
"mjohns.clang-format",
17+
"timonwong.shellcheck"
18+
]
19+
}
20+
},
21+
22+
"mounts": [
23+
// Local volume to store bash history across rebuilds
24+
"source=infinisim-bashhistory,target=/commandhistory,type=volume"
25+
// Uncomment and modify path to mount external InfiniTime source into the container
26+
//,"source=/home/example/git/InfiniTime,target=/workspaces/InfiniTime,type=bind,consistency=cached"
27+
],
28+
29+
// Sudo password "ubuntu"
30+
"remoteUser": "ubuntu"
31+
// This might be needed when you do not have a local user called ubuntu but your dev container implementation
32+
// tries to find it in the local /etc/passwd and fails to build your container.
33+
// The default is true.
34+
//,"updateRemoteUserUID": false
35+
}

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ The following configuration settings can be added to the first `cmake -S . -B bu
110110
Ex: `-DENABLE_USERAPPS="Apps::Timer, Apps::Alarm"`.
111111
The default list of user applications will be selected if this variable is not set.
112112

113+
### Build with Docker
114+
115+
You can also build the simulator using Docker.
116+
This is useful if you don't want to install all the dependencies on your system.
117+
First build the Docker image:
118+
```sh
119+
docker build -t infinisim-build .devcontainer
120+
```
121+
122+
Afterwards you can build the simulator with:
123+
```sh
124+
docker run --rm -it -v ${PWD}:/sources infinisim-build
125+
```
126+
127+
By default this builds the simulator using the InfiniTime files from the submodule in your `${PWD}`.
128+
If you want to use a different repository, you got to mount it and pass the path to the `INFINITIME_DIR` variable:
129+
```sh
130+
docker run --rm -it -v ${PWD}:/sources -v ${PWD}/../InfiniTime:/infinitime -e INFINITIME_DIR=/infinitime infinisim-build
131+
```
132+
133+
Other CMake generation and build arguments can be passed to the `GENERATE_ARGS` and `BUILD_ARGS` variables:
134+
```sh
135+
docker run --rm -it -v ${PWD}:/sources -e GENERATE_ARGS=-DENABLE_USERAPPS="Apps::Timer,Apps::Alarm" -e BUILD_ARGS=-j16 infinisim-build
136+
```
137+
138+
113139
## Run Simulator
114140

115141
When the build was successful the simulator binary can be started with

0 commit comments

Comments
 (0)