Skip to content

Commit a0bc22e

Browse files
Add some scripts to use docker and a README.rst
1 parent 045d8bc commit a0bc22e

File tree

6 files changed

+117
-13
lines changed

6 files changed

+117
-13
lines changed
File renamed without changes.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ cargo install --debug cargo-ament-build # --debug is faster to install
4141
pip install git+https://github.com/colcon/colcon-cargo.git
4242
pip install git+https://github.com/colcon/colcon-ros-cargo.git
4343

44-
mkdir -p workspace/src && cd workspace
44+
mkdir -p ~/workspace/src && cd ~/workspace
4545
git clone https://github.com/ros2-rust/ros2_rust.git src/ros2_rust
4646
vcs import src < src/ros2_rust/ros2_rust_humble.repos
4747
. /opt/ros/humble/setup.sh

docker/README.rst

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#.. redirect-from::
2+
#
3+
# Deploying-ROS2-on-IBM-Cloud
4+
# Tutorials/Deploying-ROS-2-on-IBM-Cloud
5+
6+
ROS2 Rust Docker Environment
7+
=========================================================
8+
9+
This folder contains scripts to build, start, and interact with a Docker container for a ROS2 Rust environment.
10+
11+
.. contents:: Table of Contents
12+
:depth: 3
13+
:local:
14+
15+
Prerequisites
16+
-------------
17+
18+
Ensure you have the following installed on your system:
19+
20+
- `Docker <https://docs.docker.com/engine/install/>`__
21+
- Permissions to run Docker commands (sudo may be required on some systems).
22+
23+
24+
25+
Files in the Folder
26+
-------------------
27+
28+
- `build.sh <./build.sh>`_: Builds the Docker image.
29+
- `start.sh <./start.sh>`_: Starts the Docker container.
30+
- `shell.sh <./shell.sh>`_: Accesses the running container
31+
32+
33+
Steps to Use
34+
------------
35+
36+
1. Build the Docker Image
37+
~~~~~~~~~~~~~~~~~~~~~~~~~
38+
39+
Run the ``build.sh`` script to build the Docker image:
40+
41+
.. code-block:: bash
42+
43+
$ ./build.sh
44+
45+
This script:
46+
47+
- Builds a Docker image named ``ros2_rust:latest``.
48+
- Uses ``BUILDKIT`` for efficient caching and builds.
49+
50+
51+
2. Start the Docker Container
52+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53+
Run the ``start.sh`` script to start the container:
54+
55+
.. code-block:: bash
56+
57+
$ ./start.sh
58+
59+
This script:
60+
61+
- Checks if a container based on the ros2_rust image exists and removes it if necessary.
62+
- Starts a new container named ros2_rust with the following settings:
63+
- **Interactive mode** (``-it``).
64+
- **User ID Mapping**: Maps the current user (ID ``1000``) to the container user cuser.
65+
- **Privileges**: Grants ``sudo`` group membership and system capabilities (e.g., sys_nice).
66+
- **Networking**: Uses the host's network stack.
67+
- **X11 Forwarding**: Allows graphical applications to be displayed on the host system.
68+
69+
70+
3. Access the Running Container
71+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72+
Run the ``shell.sh`` script to open an interactive shell in the running container:
73+
74+
.. code-block:: bash
75+
76+
$ ./shell.sh
77+
78+
This script:
79+
80+
- Opens a Bash shell as the user ``cuser``.
81+
- Sets the working directory to ``/home/cuser/workspace``.
82+
83+
84+
4. Install ros2 rust packages
85+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86+
87+
Follow the `README.md <../README.md>`_ file to install the overall packages needed to start working with ROS2 and Rust.
88+
89+
90+
Notes
91+
-----
92+
- If you encounter permission issues with X11, ensure the host system is configured to allow access in each terminal:
93+
94+
.. code-block:: bash
95+
96+
$ xhost local:root

docker/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
echo -e "Building ros2_rust:lastest image"
22
name=ros2_rust
33
DOCKER_BUILDKIT=1 \
4-
docker build --pull --rm -f ./Dockerfile \
4+
docker build --pull --rm -f ../Dockerfile \
55
--build-arg BUILDKIT_INLINE_CACHE=1 \
66
--tag $name:latest .

docker/shell.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
name=ros2_rust
2-
exec docker exec -it $name sudo -u cuser -i /bin/bash -c "cd ~/workspace && exec /bin/bash"
1+
NAME=ros2_rust
2+
exec docker exec -it $NAME sudo -u cuser -i /bin/bash -c "cd ~/workspace && exec /bin/bash"

docker/start.sh

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
cuser_id="1000"
2-
container_user="cuser"
1+
CUSER_ID="1000"
2+
CONTAINER_USER="cuser"
33

4-
name=ros2_rust
4+
IMAGE_NAME=ros2_rust
55

6-
docker stop $name
7-
docker rm -f $name
6+
# Check if there are any containers created from the image
7+
CONTAINERS=$(docker ps -a --filter "ancestor=$IMAGE_NAME" -q)
8+
9+
if [ -n "$CONTAINERS" ]; then
10+
echo "Image has been used to start a container. Removing it."
11+
docker stop $IMAGE_NAME
12+
docker rm -f $IMAGE_NAME
13+
else
14+
echo "Image has not been started. Skipping removal."
15+
fi
816

917
echo -e "Starting up ros2_rust container \n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
1018

1119
docker run -it --privileged \
12-
--user=${cuser_id}:${cuser_id}\
20+
--user=${CUSER_ID}:${CUSER_ID}\
1321
--group-add sudo \
1422
--env="DISPLAY" \
1523
--env="QT_X11_NO_MITSHM=1" \
16-
--workdir="/home/${container_user}/workspace" \
24+
--workdir="/home/${CONTAINER_USER}/workspace" \
1725
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
1826
--net=host \
1927
--cap-add=sys_nice \
20-
--name=$name \
21-
$name \
28+
--name=$IMAGE_NAME \
29+
$IMAGE_NAME \

0 commit comments

Comments
 (0)