This docker file can be used to setup automatically webcams and make them publish topics on ROS2 Humble.
Digit the following command (in the folder where Dockerfile is located) to create the docker image:
docker build -t webcam_ros2 .From the same folder, digit the following command:
./docker_run.shList video devices:
v4l2-ctl --list-devicesGet info about video devices:
v4l2-ctl --list-formats-ext -d /dev/video9Get real stream frequency:
v4l2-ctl --device=/dev/video9 --stream-mmap --stream-count=100Once inside the container, you can start your ROS2 node by launching multiple webcam publishers:
ros2 launch launcher_pkg launch_file.py \
video_device1:=/dev/video6 \
video_device2:=/dev/video10 \
video_device3:=/dev/video14 \
image_width:=640 \
image_height:=480 \
framerate:=30 \
transport_on:=falseNote: This command must be modified based on the number of webcams and based on the device port numbers (v4l2-ctl --list-devices).
Since the webcame node publishes compressed PNG images, I need transport node to convert them in Image type to display on RVIz. This is done by enabling ''transport_on:=true'' in the roslaunch command.
Otherwise, from terminal:
ros2 run image_transport republish compressed raw \
--ros-args -r in/compressed:=/webcam1/image_raw \
-r out:=/webcam1/image_raw/compressedTo visualize:
ros2 run rqt_image_view rqt_image_viewIt is a tool to package and run software in isolated environments.
- Image: a snapshot of a filesystem + instructions for running. You build images with docker build.
- Container: a running instance of an image. You create/run containers from an image.
# Use official ROS2 Humble base image
FROM ros:humble-ros-base
# Install dependencies
RUN apt-get update && apt-get install -y \
python3-pip \
python3-opencv \
ros-humble-cv-bridge \
ros-humble-image-transport \
ros-humble-compressed-image-transport \
&& rm -rf /var/lib/apt/lists/*
# Copy your ROS2 workspace into container
COPY ros2_ws_webcam /ros2_ws_webcam
# Build the workspace
WORKDIR /ros2_ws_webcam
RUN . /opt/ros/humble/setup.sh && colcon build
# Source ROS2 and workspace
SHELL ["/bin/bash", "-c"]
RUN echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
RUN echo "source /ros2_ws_webcam/install/setup.bash" >> ~/.bashrcFrom folder where you have Dockerfile:
docker build -t webcam_ros2 .- -t: webcam_ros2: gives a name to the image (you can choose whatever you want)
- .:means the Dockerfile is in the current directory
This step copies your workspace ros2_ws_webcam into the container, installs dependencies, and builds the workspace with colcon build.
If the code is copied inside the image (COPY ros2_ws_webcam /ros2_ws_webcam), every time you change a line you need to rebuild the image.
Solution: remove COPY from the Dockerfile and just mount the local folder with the docker run command:
docker run --rm -it \
-v ~/Documents/Coding/ros2_ws_webcam:/ros2_ws_webcam \
webcam_ros2 \Inside the container you will see the same folder from your host (/ros2_ws_webcam).
You can edit from your PC with VSCode, then inside the container just run:
cd /ros2_ws_webcam
colcon build
source install/setup.bash
ros2 run webcam_pkg webcam_nodeNow you do not have to rebuild image every time you modify the code.
To see images:
docker image lsTo see containers:
docker ps -aTo delete images:
docker rmi <image_id_or_name>To delete containers:
docker rm -f <container_id_or_name> # force cancellation
docker container prune # delete all unused containersStart the container launching the following command (from any folder):
bash docker_run.shTo exit running container, just digit:
exitCreate script bash file:
touch docker_run.shInside it you should put the docker run command with all the required settings (device, network host, etc).
Make the file executable:
chmod +x docker_run.shAfter that you can launch it with:
./docker_run.sh