By Kevin McAleer, 3 Minutes
To make the process of building our ROS2 container simpler I have created a dockerfile and accompanying docker-compose.yml file. These two files help us build and then run the ROS2 docker container.
Docker-Compose.yml
The
docker-compose.ymlfile contains instructions on which docker image to use and also any parameters we want to include, such as host device (we’ll want to use the lidar sensor which is found at/dev/ttyUSB0on the host file system), and also any volume or folder mappings.Contents of the
docker-compose.yml(change/home/kevto your own home folder, for example/home/pi)version: "3.9" services: ros2: build: . # restart: no ports: - "3332:3332" volumes: - /home/kev/ros:/home/ros - /home/kev/cubie-1:/ros2 devices: - /dev/ttyUSB0:/dev/ttyUSB0 tty: true
Folder mappings
We can map local folders from our Raspberry Pi into our container so that any files we create inside or out of the container will show up. Normally any changes made within the container are lost when we shut it down, however by mapping (called
bindingin Docker) the local host folder to the container we can ensure changes are retained.We will map two host folders:
/home/kev/rosto/home/roswithin the container
/home/kev/cubie-1to/ros2within the container
cd cubie-1/docker
docker build -t ros2 .
Where ros2 is the name of the docker image we have created

docker compose up -d
The container will now be running. You can check using the docker ps command.
No LiDAR plugged in yet?
The
devicessection passes the LiDAR on/dev/ttyUSB0into the container, and Docker won’t start the container if that device doesn’t exist. Comment out the twodeviceslines with a#until your LiDAR is connected.
Quick quiz 1 of 2
Why do we map (bind) folders from the Pi into the container?
Quick quiz 2 of 2
Which command starts the container in the background?
-d means detached: the container keeps running after the command returns.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments