From c99230348c3e5b68d9aae6f41f57b0a0077e08a1 Mon Sep 17 00:00:00 2001 From: roboticswithjulia Date: Sun, 17 Nov 2024 01:39:36 +0100 Subject: [PATCH 1/7] Change docker file to install cargo and colcon using a non-root custom user --- Dockerfile | 25 ------------------------- docker/Dockerfile | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 25 deletions(-) delete mode 100644 Dockerfile create mode 100644 docker/Dockerfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index b148eafe3..000000000 --- a/Dockerfile +++ /dev/null @@ -1,25 +0,0 @@ -ARG ROS_DISTRO=humble -FROM ros:$ROS_DISTRO as base -ARG DEBIAN_FRONTEND=noninteractive - -# Install dependencies -RUN apt-get update && apt-get install -y \ - curl \ - git \ - libclang-dev \ - tmux \ - python3-pip \ - && rm -rf /var/lib/apt/lists/* - -# Install Rust and the cargo-ament-build plugin -RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain 1.74.0 -y -ENV PATH=/root/.cargo/bin:$PATH -RUN cargo install cargo-ament-build - -RUN pip install --upgrade pytest - -# Install the colcon-cargo and colcon-ros-cargo plugins -RUN pip install git+https://github.com/colcon/colcon-cargo.git git+https://github.com/colcon/colcon-ros-cargo.git - -RUN mkdir -p /workspace && echo "Did you forget to mount the repository into the Docker container?" > /workspace/HELLO.txt -WORKDIR /workspace diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 000000000..cd60c37c1 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,42 @@ +ARG ROS_DISTRO=humble +FROM ros:$ROS_DISTRO AS base +ARG DEBIAN_FRONTEND=noninteractive + +ARG CONTAINER_USER="cuser" +ARG USER_UID=1000 +ARG USER_GID=$USER_UID + +# Create a user in the container to operate as unprivileged +RUN groupadd --gid $USER_GID $CONTAINER_USER \ + && useradd --uid $USER_UID --gid $USER_GID -m $CONTAINER_USER \ + && mkdir -p /home/$CONTAINER_USER +RUN apt-get update \ + && apt-get install -y sudo \ + && rm -rf /var/lib/apt/lists/* \ + && echo $CONTAINER_USER ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$CONTAINER_USER \ + && chmod 0440 /etc/sudoers.d/$CONTAINER_USER + +SHELL ["/bin/bash", "--login", "-c"] + +# Install dependencies +RUN apt-get update && apt-get install -y \ + curl \ + git \ + libclang-dev \ + tmux \ + python3-pip \ + && rm -rf /var/lib/apt/lists/* + +# Install Rust and the cargo-ament-build plugin +USER $CONTAINER_USER +WORKDIR /home/${CONTAINER_USER} +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain 1.74.0 -y +ENV PATH="/home/${CONTAINER_USER}/.cargo/bin:$PATH" +RUN cargo install cargo-ament-build + +RUN pip install --upgrade pytest + +# Install the colcon-cargo and colcon-ros-cargo plugins +RUN pip install git+https://github.com/colcon/colcon-cargo.git git+https://github.com/colcon/colcon-ros-cargo.git + +WORKDIR /home/${CONTAINER_USER}/workspace From 5b70ccea8e64c292b6418ffa0171d80866051d59 Mon Sep 17 00:00:00 2001 From: roboticswithjulia Date: Sun, 17 Nov 2024 01:41:32 +0100 Subject: [PATCH 2/7] Add build, start and shell scripts to help working with docker --- docker/build.sh | 6 ++++++ docker/shell.sh | 2 ++ docker/start.sh | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100755 docker/build.sh create mode 100755 docker/shell.sh create mode 100755 docker/start.sh diff --git a/docker/build.sh b/docker/build.sh new file mode 100755 index 000000000..fb2e2c778 --- /dev/null +++ b/docker/build.sh @@ -0,0 +1,6 @@ +echo -e "Building ros2_rust:lastest image" +name=ros2_rust +DOCKER_BUILDKIT=1 \ +docker build --pull --rm -f ./Dockerfile \ +--build-arg BUILDKIT_INLINE_CACHE=1 \ +--tag $name:latest . \ No newline at end of file diff --git a/docker/shell.sh b/docker/shell.sh new file mode 100755 index 000000000..20c098256 --- /dev/null +++ b/docker/shell.sh @@ -0,0 +1,2 @@ +name=ros2_rust +exec docker exec -it $name sudo -u cuser -i /bin/bash -c "cd ~/workspace && exec /bin/bash" \ No newline at end of file diff --git a/docker/start.sh b/docker/start.sh new file mode 100755 index 000000000..37191ea67 --- /dev/null +++ b/docker/start.sh @@ -0,0 +1,21 @@ +cuser_id="1000" +container_user="cuser" + +name=ros2_rust + +docker stop $name +docker rm -f $name + +echo -e "Starting up ros2_rust container \n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + +docker run -it --privileged \ + --user=${cuser_id}:${cuser_id}\ + --group-add sudo \ + --env="DISPLAY" \ + --env="QT_X11_NO_MITSHM=1" \ + --workdir="/home/${container_user}/workspace" \ + --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \ + --net=host \ + --cap-add=sys_nice \ + --name=$name \ + $name \ \ No newline at end of file From 045d8bcd1911856875ad7f81bd4db28c89f99243 Mon Sep 17 00:00:00 2001 From: roboticswithjulia Date: Sun, 17 Nov 2024 01:44:55 +0100 Subject: [PATCH 3/7] Add the devcontainer file, to endow the repository with the capability to work with devcontainers --- .devcontainer/devcontainer.json | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 .devcontainer/devcontainer.json diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 000000000..53f002fff --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,27 @@ + +{ + "name": "ros2_rust_dev:latest", + "build": { + "context": "..", + "dockerfile": "../docker/Dockerfile", + "args": { + "XDG_RUNTIME_DIR": "${localEnv:XDG_RUNTIME_DIR}" + } + }, + "workspaceFolder": "/home/cuser/workspace", + "runArgs": [ + "--network=host", + "--privileged" + ], + "containerEnv": { + "DISPLAY": "${localEnv:DISPLAY}", + "TERM": "xterm-256color" + }, + + "remoteUser": "cuser", + "containerEnv": { + // Convenience, for shell colors, aliases, etc + "SHELL": "/bin/bash" + } + +} \ No newline at end of file From a0bc22ee4895852be2aa94d8165fc5e01a99a167 Mon Sep 17 00:00:00 2001 From: roboticswithjulia Date: Sun, 24 Nov 2024 10:39:04 +0100 Subject: [PATCH 4/7] Add some scripts to use docker and a README.rst --- docker/Dockerfile => Dockerfile | 0 README.md | 2 +- docker/README.rst | 96 +++++++++++++++++++++++++++++++++ docker/build.sh | 2 +- docker/shell.sh | 4 +- docker/start.sh | 26 +++++---- 6 files changed, 117 insertions(+), 13 deletions(-) rename docker/Dockerfile => Dockerfile (100%) create mode 100644 docker/README.rst diff --git a/docker/Dockerfile b/Dockerfile similarity index 100% rename from docker/Dockerfile rename to Dockerfile diff --git a/README.md b/README.md index 7fb17a8db..f88365fe9 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ cargo install --debug cargo-ament-build # --debug is faster to install pip install git+https://github.com/colcon/colcon-cargo.git pip install git+https://github.com/colcon/colcon-ros-cargo.git -mkdir -p workspace/src && cd workspace +mkdir -p ~/workspace/src && cd ~/workspace git clone https://github.com/ros2-rust/ros2_rust.git src/ros2_rust vcs import src < src/ros2_rust/ros2_rust_humble.repos . /opt/ros/humble/setup.sh diff --git a/docker/README.rst b/docker/README.rst new file mode 100644 index 000000000..5d26f35bc --- /dev/null +++ b/docker/README.rst @@ -0,0 +1,96 @@ +#.. redirect-from:: +# +# Deploying-ROS2-on-IBM-Cloud +# Tutorials/Deploying-ROS-2-on-IBM-Cloud + +ROS2 Rust Docker Environment +========================================================= + +This folder contains scripts to build, start, and interact with a Docker container for a ROS2 Rust environment. + +.. contents:: Table of Contents + :depth: 3 + :local: + +Prerequisites +------------- + +Ensure you have the following installed on your system: + +- `Docker `__ +- Permissions to run Docker commands (sudo may be required on some systems). + + + +Files in the Folder +------------------- + +- `build.sh <./build.sh>`_: Builds the Docker image. +- `start.sh <./start.sh>`_: Starts the Docker container. +- `shell.sh <./shell.sh>`_: Accesses the running container + + +Steps to Use +------------ + +1. Build the Docker Image +~~~~~~~~~~~~~~~~~~~~~~~~~ + +Run the ``build.sh`` script to build the Docker image: + +.. code-block:: bash + + $ ./build.sh + +This script: + +- Builds a Docker image named ``ros2_rust:latest``. +- Uses ``BUILDKIT`` for efficient caching and builds. + + +2. Start the Docker Container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Run the ``start.sh`` script to start the container: + +.. code-block:: bash + + $ ./start.sh + +This script: + +- Checks if a container based on the ros2_rust image exists and removes it if necessary. +- Starts a new container named ros2_rust with the following settings: + - **Interactive mode** (``-it``). + - **User ID Mapping**: Maps the current user (ID ``1000``) to the container user cuser. + - **Privileges**: Grants ``sudo`` group membership and system capabilities (e.g., sys_nice). + - **Networking**: Uses the host's network stack. + - **X11 Forwarding**: Allows graphical applications to be displayed on the host system. + + +3. Access the Running Container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Run the ``shell.sh`` script to open an interactive shell in the running container: + +.. code-block:: bash + + $ ./shell.sh + +This script: + +- Opens a Bash shell as the user ``cuser``. +- Sets the working directory to ``/home/cuser/workspace``. + + +4. Install ros2 rust packages +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Follow the `README.md <../README.md>`_ file to install the overall packages needed to start working with ROS2 and Rust. + + +Notes +----- +- If you encounter permission issues with X11, ensure the host system is configured to allow access in each terminal: + +.. code-block:: bash + + $ xhost local:root \ No newline at end of file diff --git a/docker/build.sh b/docker/build.sh index fb2e2c778..ab8ae94f4 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -1,6 +1,6 @@ echo -e "Building ros2_rust:lastest image" name=ros2_rust DOCKER_BUILDKIT=1 \ -docker build --pull --rm -f ./Dockerfile \ +docker build --pull --rm -f ../Dockerfile \ --build-arg BUILDKIT_INLINE_CACHE=1 \ --tag $name:latest . \ No newline at end of file diff --git a/docker/shell.sh b/docker/shell.sh index 20c098256..fd6fa928e 100755 --- a/docker/shell.sh +++ b/docker/shell.sh @@ -1,2 +1,2 @@ -name=ros2_rust -exec docker exec -it $name sudo -u cuser -i /bin/bash -c "cd ~/workspace && exec /bin/bash" \ No newline at end of file +NAME=ros2_rust +exec docker exec -it $NAME sudo -u cuser -i /bin/bash -c "cd ~/workspace && exec /bin/bash" \ No newline at end of file diff --git a/docker/start.sh b/docker/start.sh index 37191ea67..e6a8f0d17 100755 --- a/docker/start.sh +++ b/docker/start.sh @@ -1,21 +1,29 @@ -cuser_id="1000" -container_user="cuser" +CUSER_ID="1000" +CONTAINER_USER="cuser" -name=ros2_rust +IMAGE_NAME=ros2_rust -docker stop $name -docker rm -f $name +# Check if there are any containers created from the image +CONTAINERS=$(docker ps -a --filter "ancestor=$IMAGE_NAME" -q) + +if [ -n "$CONTAINERS" ]; then + echo "Image has been used to start a container. Removing it." + docker stop $IMAGE_NAME + docker rm -f $IMAGE_NAME +else + echo "Image has not been started. Skipping removal." +fi echo -e "Starting up ros2_rust container \n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" docker run -it --privileged \ - --user=${cuser_id}:${cuser_id}\ + --user=${CUSER_ID}:${CUSER_ID}\ --group-add sudo \ --env="DISPLAY" \ --env="QT_X11_NO_MITSHM=1" \ - --workdir="/home/${container_user}/workspace" \ + --workdir="/home/${CONTAINER_USER}/workspace" \ --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \ --net=host \ --cap-add=sys_nice \ - --name=$name \ - $name \ \ No newline at end of file + --name=$IMAGE_NAME \ + $IMAGE_NAME \ \ No newline at end of file From 27ed0a1a9b46a997bbe55d558097548c3b11c069 Mon Sep 17 00:00:00 2001 From: roboticswithjulia Date: Sun, 24 Nov 2024 11:09:07 +0100 Subject: [PATCH 5/7] Add devcontainer README.rst --- .devcontainer/README.rst | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 .devcontainer/README.rst diff --git a/.devcontainer/README.rst b/.devcontainer/README.rst new file mode 100644 index 000000000..c19014252 --- /dev/null +++ b/.devcontainer/README.rst @@ -0,0 +1,83 @@ +#.. redirect-from:: +# +# Deploying-ROS2-on-IBM-Cloud +# Tutorials/Deploying-ROS-2-on-IBM-Cloud + +Development Container for ROS2 Rust +==================================== + +This repository includes a pre-configured development container setup, designed to facilitate working on ROS2 Rust projects. The setup is managed using Visual Studio Code's DevContainers feature. + +.. contents:: Table of Contents + :depth: 3 + :local: + +File Description +---------------- + +The provided ``devcontainer.json`` file specifies the configuration for running a containerized development environment with the following features: + +- **Image Configuration**: Uses the Docker image ros2_rust_dev:latest. +- **Build Settings**: + - Context: Specifies the root directory of the project (``..``). + - Dockerfile: Uses the Dockerfile located at ``../docker/Dockerfile``. + - Build Args: Includes runtime environment variables such as ``XDG_RUNTIME_DIR``. +- **Workspace**: + - Mounts the folder ``/home/cuser/workspace`` as the development workspace. +- **Container Runtime**: + - Enables ``--network=host`` for network sharing. + - Grants privileged access using ``--privileged``. +- **Environment Variables**: + - ``DISPLAY`` and ``TERM`` are forwarded from the host to the container to support GUI applications and terminal colors. +- **User Settings**: + - The development container runs as the user ``cuser``. + - Configures the shell to use ``/bin/bash`` for a familiar and customizable command-line experience. + +Setting Up and Running the DevContainer in VS Code +-------------------------------------------------- + +To use this development container in Visual Studio Code, follow these steps: + +Prerequisites +~~~~~~~~~~~~~ + +1. **Install VS Code**: Download and install `Visual Studio Code `__. + +2. **Install Required Extensions:** + - `Remote Containers `__ +3. **Install Docker:** Ensure Docker is installed and running on your system. You can download Docker from `here `__. + + +Steps to Launch the DevContainer +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +1. **Clone the Repository**: Clone your project repository to your local machine. + +.. code-block:: bash + + $ git clone git@github.com:ros2-rust/ros2_rust.git + $ cd ros2_rust + + + +2. **Open in VS Code**: Open the repository folder in VS Code. + +.. code-block:: bash + + $ code . + +3. **Open the Command Palette**: Press ``Ctrl+Shift+P`` (Windows/Linux) or ``Cmd+Shift+P`` (macOS) to open the Command Palette. +4. **Reopen in Container**: Search for and select **"Dev Containers: Reopen in Container"**. VS Code will build and start the development container based on the configuration in ``devcontainer.json``. +5. **Start Coding**: Once the container is running, your workspace is ready for development. You can open terminals, run commands, and debug your ROS2 Rust projects seamlessly within the containerized environment. + +Notes +~~~~~ + +- **Container Network**: The ``--network=host`` option is enabled to allow network sharing between the host and container. +- **Privilege**: The ``--privileged`` flag is enabled, which may be required for specific ROS2 features but should be used cautiously. +- If you encounter permission issues with X11, ensure the host system is configured to allow access in each terminal: + +.. code-block:: bash + + $ xhost local:root + $ code & \ No newline at end of file From 6782c50d241e84508ff6de5e7674bd8ec7c947e6 Mon Sep 17 00:00:00 2001 From: roboticswithjulia Date: Sun, 24 Nov 2024 11:25:50 +0100 Subject: [PATCH 6/7] Remove some unused lines from devcontainer and docker README.rst --- .devcontainer/README.rst | 9 ++------- docker/README.rst | 5 ----- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/.devcontainer/README.rst b/.devcontainer/README.rst index c19014252..6afe46c8d 100644 --- a/.devcontainer/README.rst +++ b/.devcontainer/README.rst @@ -1,8 +1,3 @@ -#.. redirect-from:: -# -# Deploying-ROS2-on-IBM-Cloud -# Tutorials/Deploying-ROS-2-on-IBM-Cloud - Development Container for ROS2 Rust ==================================== @@ -17,7 +12,7 @@ File Description The provided ``devcontainer.json`` file specifies the configuration for running a containerized development environment with the following features: -- **Image Configuration**: Uses the Docker image ros2_rust_dev:latest. +- **Image Configuration**: Uses the Docker image ``ros2_rust_dev:latest``. - **Build Settings**: - Context: Specifies the root directory of the project (``..``). - Dockerfile: Uses the Dockerfile located at ``../docker/Dockerfile``. @@ -75,7 +70,7 @@ Notes - **Container Network**: The ``--network=host`` option is enabled to allow network sharing between the host and container. - **Privilege**: The ``--privileged`` flag is enabled, which may be required for specific ROS2 features but should be used cautiously. -- If you encounter permission issues with X11, ensure the host system is configured to allow access in each terminal: +- If you encounter permission issues with X11, ensure the host system is configured to allow access in the vscode: .. code-block:: bash diff --git a/docker/README.rst b/docker/README.rst index 5d26f35bc..b424a0825 100644 --- a/docker/README.rst +++ b/docker/README.rst @@ -1,8 +1,3 @@ -#.. redirect-from:: -# -# Deploying-ROS2-on-IBM-Cloud -# Tutorials/Deploying-ROS-2-on-IBM-Cloud - ROS2 Rust Docker Environment ========================================================= From a80fcfd6b9a443a7288b65184fa7bc0fdfea4475 Mon Sep 17 00:00:00 2001 From: roboticswithjulia Date: Wed, 27 Nov 2024 11:55:03 +0100 Subject: [PATCH 7/7] Add workspace directory creation and reminder message in Dockerfile --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index d7f311da9..3523f6492 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,4 +40,5 @@ RUN pip install --upgrade pytest # Install the colcon-cargo and colcon-ros-cargo plugins RUN pip install git+https://github.com/colcon/colcon-cargo.git git+https://github.com/colcon/colcon-ros-cargo.git +RUN mkdir -p ~/workspace && echo "Did you forget to mount the repository into the Docker container?" > ~/workspace/HELLO.txt WORKDIR /home/${CONTAINER_USER}/workspace