ROS 2 and Docker
In this exercise you run ROS 2 in containers: you explore a ROS image, see which docker run options ROS
needs, and start an Intel RealSense camera with
realsense_docker.
You need Docker (see Docker basics) and ROS 2 Jazzy on the host. Parts 1 to 4, 6 and the new services of part 7 work without a camera; part 5 and the first steps of part 7 need a RealSense camera.
Slides: ROS 2 Basics → Docker and ROS 2.
1. Explore a ROS image
Start a shell in the ROS 2 Jazzy base image (ros:jazzy is ros:jazzy-ros-base):
docker run --rm -it ros:jazzy
Inside the container:
whoami # who are you?
getent passwd 1000 # which user has the UID 1000?
ls /opt/ros # which ROS distribution?
ros2 pkg list | wc -l # how many ROS packages?
which colcon rosdep vcs # build tools
cat /ros_entrypoint.sh # why does ros2 work without sourcing setup.bash?
exit
Questions:
Is
rviz2in this image? Which image would you use for it? (Slides: “ROS images from OSRF”)What happens to a file that you create in the container after
exit?
3. Files in mounted folders
mkdir -p ~/docker_test
docker run --rm -v ~/docker_test:/data ros:jazzy touch /data/as_default
docker run --rm -v ~/docker_test:/data --user $(id -u):$(id -g) ros:jazzy touch /data/as_me
ls -l ~/docker_test
Who owns the two files? Can you delete as_default without sudo?
(Clean up: docker run --rm -v ~/docker_test:/data ros:jazzy rm /data/as_default.)
Now start ROS as your user:
docker run --rm --user $(id -u):$(id -g) ros:jazzy bash -c 'whoami; echo HOME=$HOME; ros2 topic list'
If your UID is 1000, you are ubuntu and it works. With another UID, the image does not know you:
HOME is /, and ROS cannot create its log folder. Fix: -e ROS_HOME=/tmp/.ros, or create the user in
the Dockerfile (part 4).
4. A real Dockerfile: realsense_docker
git clone https://github.com/ROS-Industrial-Session-Common/realsense_docker.git
cd realsense_docker
Read the Dockerfile:
Which base image? Why this one and not
ros:jazzy?Which ROS packages are installed?
Which user is created, and with which UID? Why
chmod -R a+rwX /home/rosandENV HOME=...?
Read compose.yaml:
find the options of part 2 and 3 (network_mode, ipc, user). Which variables does it read from your shell?
Get the image: pull it from the GitHub Container Registry, or build it yourself.
docker compose pull # download the image named in compose.yaml (image:)
docker compose build # or build it from the Dockerfile: same name
docker images | grep realsense
compose.yaml names the image ghcr.io/ros-industrial-session-common/realsense_docker:jazzy
(image:) and says how to build it (build:). docker compose up uses the image with that name if it
exists, and builds it otherwise. GitHub Actions builds the same image and pushes it on every change of
the repository.
5. Start a camera with Docker Compose
Plug in the camera. Set the environment (your UID, the video group, the stream profile), then find
the serial number of your camera, from inside the container:
source set_camera_env.sh
docker compose run --rm realsense_1 rs-enumerate-devices -s
The RealSense driver expects the serial with a leading underscore:
export REALSENSE_1_SERIAL=_<your serial>
docker compose up realsense_1
In a second terminal, in the same folder:
source set_camera_env.sh
docker compose exec realsense_1 bash
ros2 topic list | grep camera
ros2 topic hz /camera/camera_1/color/image_raw
RViz in the container: run xhost +local:docker once on the host, then rviz2 in the container shell.
Add an Image display with /camera/camera_1/color/image_raw.
On the host: ros2 topic list | grep camera. The container uses CycloneDDS
(RMW_IMPLEMENTATION=rmw_cyclonedds_cpp in set_camera_env.sh): use the same on the host if the topics do
not appear (sudo apt install ros-jazzy-rmw-cyclonedds-cpp).
Stop with Ctrl+C, then docker compose down.
6. Add a dependency to the image
Add ros-${ROS_DISTRO}-rqt-image-view to the RealSense apt-get install in the Dockerfile, then:
docker compose build
Which steps are CACHED, which run again? Why does the order of the instructions matter?
Check: docker compose run --rm realsense_1 ros2 pkg prefix rqt_image_view.
With the camera running, look at its image: docker compose exec realsense_1 ros2 run rqt_image_view rqt_image_view.
7. Change compose.yaml
compose.yaml describes how the containers run, not what is in the image: after a change, restart the
service, no rebuild is needed.
A variable (camera): start the camera in HD:
source set_camera_env.sh hd docker compose up realsense_1
Where does
compose.yamluseREALSENSE_COLOR_PROFILE? Where does its value come from?A launch parameter (camera): in the
commandofrealsense_1, setenable_depth:=false. Restart the service: which topics are gone (ros2 topic list)?A new service (no camera needed): look at the commented-out services at the end of
compose.yaml(realsense_2_static_tf, …). They publish the pose of a camera in the cell withstatic_transform_publisher, and reuse all the settings of the camera services with<<: *realsense-base. Add a service for camera 1 in the same way, underservices::camera_1_tf: <<: *realsense-base container_name: camera_1_tf command: > ros2 run tf2_ros static_transform_publisher --x 0.5 --y 0.0 --z 1.0 --yaw 1.57 --frame-id base_link --child-frame-id camera_1_link
Start it, and check the transform from a second terminal:
docker compose up camera_1_tf docker compose exec camera_1_tf ros2 run tf2_ros tf2_echo base_link camera_1_link
With the camera:
docker compose up realsense_1 camera_1_tf. In RViz (fixed framebase_link, TF display), the camera frames now hang belowbase_link.Your turn: uncomment
realsense_3_static_tf, or add a service of your own (e.g. a frame for the table). Which frames does it publish?
Undo your changes with git checkout compose.yaml Dockerfile.
8. Clean up
docker compose down
docker system df # how much space do images and containers use?
docker image prune # remove unnamed images left by rebuilds