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 rviz2 in 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?

2. Network and shared memory

Start a publisher in the background, then a subscriber, both with the default network:

docker run -d --name talker -e ROS_DOMAIN_ID=42 ros:jazzy \
  ros2 topic pub -r 2 /chatter std_msgs/msg/String "{data: hello}"
docker run --rm -it -e ROS_DOMAIN_ID=42 ros:jazzy ros2 topic echo /chatter

The messages arrive: containers on the same computer see each other. Stop with Ctrl+C, then docker rm -f talker.

Now both with the host’s network, so that other computers would see them too:

docker run -d --name talker --network host -e ROS_DOMAIN_ID=42 ros:jazzy \
  ros2 topic pub -r 2 /chatter std_msgs/msg/String "{data: hello}"
docker run --rm -it --network host -e ROS_DOMAIN_ID=42 ros:jazzy ros2 topic list
docker run --rm -it --network host -e ROS_DOMAIN_ID=42 ros:jazzy ros2 topic echo /chatter

/chatter is listed, but no message arrives. With --network host, both containers seem to be on the same computer, so Fast DDS (the default middleware) sends the data through shared memory. Each container has its own shared memory, unless it runs with --ipc host. Remove the talker and repeat with --network host --ipc host on both commands: the messages arrive.

docker rm -f talker

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/ros and ENV 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.

  1. A variable (camera): start the camera in HD:

    source set_camera_env.sh hd
    docker compose up realsense_1
    

    Where does compose.yaml use REALSENSE_COLOR_PROFILE? Where does its value come from?

  2. A launch parameter (camera): in the command of realsense_1, set enable_depth:=false. Restart the service: which topics are gone (ros2 topic list)?

  3. 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 with static_transform_publisher, and reuse all the settings of the camera services with <<: *realsense-base. Add a service for camera 1 in the same way, under services::

      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 frame base_link, TF display), the camera frames now hang below base_link.

  4. 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