From d412d37a31a7cc584aae0981c7dd067bda72ca33 Mon Sep 17 00:00:00 2001 From: dpugliese6 Date: Sun, 5 Jan 2025 20:24:29 +0100 Subject: [PATCH] docker: easily start a simulation in a docker container --- .devcontainer/Dockerfile | 47 ------------------------------------- Dockerfile | 45 +++++++++++++++++++++++++++++++++++ README.md | 15 ++++++++++++ tools/prepare-ns3-docker.sh | 27 +++++++++++++++++++++ tools/simulate.sh | 23 ++++++++++++++++++ 5 files changed, 110 insertions(+), 47 deletions(-) delete mode 100644 .devcontainer/Dockerfile create mode 100644 Dockerfile create mode 100755 tools/prepare-ns3-docker.sh create mode 100755 tools/simulate.sh diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile deleted file mode 100644 index 16070e7..0000000 --- a/.devcontainer/Dockerfile +++ /dev/null @@ -1,47 +0,0 @@ -FROM ubuntu:20.04 - -ENV PYTHONUNBUFFERED=1 -ENV DEBIAN_FRONTEND=noninteractive - -ARG USERNAME=user -ARG USER_UID=1000 -ARG USER_GID=$USER_UID - -#### -# Machine Setup -#### -RUN apt-get update \ - && apt-get install --no-install-recommends --quiet --assume-yes \ - git \ - gcc \ - g++ \ - gdb \ - valgrind \ - patch \ - ssh \ - sudo \ - # ns3 dependencies - python3 \ - python-is-python3 \ - python3-dev \ - python3-pip \ - python3-pybindgen \ - python3-pygccxml \ - pkg-config \ - sqlite3 \ - #libgtk-3-dev \ - #libsqlite3-dev \ - # pybindgen dependencies - #libboost-dev \ - #castxml \ - # IoD_Sim dependencies - rapidjson-dev \ - libgsl-dev \ - libxml2-dev \ - # User setup allows devcontainer to use the same UID as host linux user - && groupadd --gid $USER_GID $USERNAME \ - && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \ - && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ - && chmod 0440 /etc/sudoers.d/$USERNAME - -USER $USERNAME diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f7ccb26 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,45 @@ +FROM ubuntu:22.04 + +ENV PYTHONUNBUFFERED=1 +ENV DEBIAN_FRONTEND=noninteractive + +#### +# Machine setup and dependecies installation +#### +RUN apt-get update \ + && apt-get install --no-install-recommends --quiet --assume-yes \ + clang-format \ + cmake \ + g++ \ + gdb \ + gcc \ + git \ + make \ + libgsl-dev \ + libxml2-dev \ + patch \ + pkg-config \ + python3 \ + python3-venv \ + python3-pip \ + python-is-python3 \ + rapidjson-dev \ + doxygen \ + graphviz + +# Use local versione of the simulator + +#RUN mkdir IoD_Sim +#COPY . ./IoD_Sim/ + +# Use remote version of the simulator +RUN git clone https://github.com/telematics-lab/IoD_Sim.git + +WORKDIR ./IoD_Sim +RUN ./tools/prepare-ns3-docker.sh +RUN ./tools/configure-iodsim.sh +WORKDIR ./ns3 +RUN ./ns3 build -j$(grep -c ^processor /proc/cpuinfo) +WORKDIR .. + +ENTRYPOINT ["/bin/bash","./tools/simulate.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 5170c21..e326432 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,21 @@ The documentation can be generated in HTML and LaTeX formats by running: doxygen ./docs/IoD_Sim_doc ``` +## Working with docker + +After creating your own scenario through the JSON file, you can use [Docker](https://docs.docker.com/engine/) to quickly proceed to simulation in a few steps. The first time, the container image must be built by running the following command in the main directory of the repository: + +``` +docker build . -t docker_iod_sim +``` + +Next, the simulation can be run by launching: + +``` +sudo docker run --rm -v ./results:/IoD_Sim/results -v ./scenario:/IoD_Sim/scenario docker_iod_sim [--help] +``` + +When completed, the `results` folder will contain the results of the simulation. It should be noted that if a new scenario is placed in `./scenario`, there will be no need to rebuild the container. ## License diff --git a/tools/prepare-ns3-docker.sh b/tools/prepare-ns3-docker.sh new file mode 100755 index 0000000..4f86920 --- /dev/null +++ b/tools/prepare-ns3-docker.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +set -e + +check_availability() { + PRG_NAME=$1 + + command -v $PRG_NAME &> /dev/null + if [ "$?" -ne 0 ]; then + echo "The program \"${PRG_NAME}\" is not available in your system. \ + Please install it, then re-run this script." + exit 1 + fi +} + +# check dependencies +check_availability git +check_availability patch + +git submodule update --init + +pushd ns3 > /dev/null +for patchfile in ../tools/*.patch +do + patch -s -p1 < $patchfile +done +popd > /dev/null diff --git a/tools/simulate.sh b/tools/simulate.sh new file mode 100755 index 0000000..dedb674 --- /dev/null +++ b/tools/simulate.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +usage() { + echo "Usage:" + echo "sudo docker run --rm -v ./results:/IoD_Sim/results -v ./scenario:/IoD_Sim/scenario docker_iod_sim [--help] " + echo + echo "Options:" + echo " --help Shows help message." + echo " Name of the .json file to simulate." + exit 1 +} + +if [[ "$1" == "--help" ]]; then + usage +elif [[ -z "$1" ]]; then + usage +elif [[ ! -f "./scenario/$1.json" ]]; then + echo "Error: File '$1.json' doesn't exist." + usage +else + source .venv/bin/activate && ./ns3/ns3 run "iodsim --config=../scenario/$1.json" + chmod -R 777 ./results/* +fi \ No newline at end of file