Docker for ROS
Here’s a much more detailed and expanded version of your “Docker for ROS” documentation in English:
Docker for ROS
Motivation
Using Docker for Robot Operating System (ROS) development offers significant advantages, especially when dealing with compatibility issues between ROS distributions and host operating systems. For instance, ROS 2 Humble is officially supported on Ubuntu 22.04, but not on Ubuntu 20.04. Instead of reinstalling the entire operating system or setting up complex virtual environments, Docker provides a clean, reproducible, and isolated environment for developing and running ROS applications.
Other benefits of using Docker with ROS include:
- Isolation: Prevents system-level dependency conflicts.
- Portability: Containerized applications can run consistently across different machines.
- Reproducibility: Environment configurations can be version-controlled and shared.
- Simplicity: Simplifies setup and onboarding for new developers.
Step-by-step Process
Step 1: Install Docker on the Ubuntu Machine
Before you begin, ensure your Ubuntu machine meets the OS requirements outlined by Docker.
Installation steps:
- Uninstall old versions (if any):
sudo apt-get remove docker docker-engine docker.io containerd runc
- Update the apt package index and install prerequisites:
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
- Add Docker’s official GPG key:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- Set up the stable repository:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
- Install Docker Engine:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
- Verify the installation:
sudo docker run hello-world
- (Optional) Add your user to the
docker
group to avoid usingsudo
:
sudo usermod -aG docker $USER
newgrp docker
📖 Official reference: Install Docker on Ubuntu
Step 2: Pull ROS 2 Docker Image
To run ROS 2 applications in Docker, you need to pull a suitable image. OSRF (Open Source Robotics Foundation) provides official Docker images.
For example, to pull the ROS 2 Humble desktop image:
docker pull osrf/ros:humble-desktop
You can also use tags like ros:humble
, ros:humble-ros-base
, or ros:humble-desktop-full
depending on your needs.
📖 ROS Docker Tutorial: Run 2 nodes in Docker
Step 3: Run Docker with Display Support
To run GUI tools like rviz2
, rqt
, or gazebo
, you need to allow the Docker container to access your host’s X11 display server.
Example command:
xhost +local:docker # allow local docker containers to access X11
docker run -it --rm \
--env DISPLAY=$DISPLAY \
--volume /tmp/.X11-unix:/tmp/.X11-unix:rw \
--network host \
osrf/ros:humble-desktop
📝 Explanation:
-
--env DISPLAY=$DISPLAY
: Passes your display environment variable into the container. -
--volume /tmp/.X11-unix:/tmp/.X11-unix
: Mounts the X11 socket. -
--network host
: Shares the network with your host (important for ROS networking). -
--rm
: Automatically removes the container when it exits. -
-it
: Runs in interactive mode with a TTY.
To persist the container or build your own, consider using a Dockerfile and volumes instead.
Other Useful Tricks
Connect ROS 2 Docker Container to the Host Ubuntu System
To interact with ROS 2 nodes running on your host or in other containers, consider these practices:
- Use
--network host
: Ensures ROS 2 DDS-based discovery works across host and container. - Set appropriate ROS environment variables inside the container:
export ROS_DOMAIN_ID=0 # Make sure host and container use the same domain
export RMW_IMPLEMENTATION=rmw_fastrtps_cpp # Default DDS implementation
- Persistent Workspace Mount: Mount your local ROS workspace into the container:
docker run -it --rm \
--volume ~/ros2_ws:/ros2_ws \
--workdir /ros2_ws \
--network host \
osrf/ros:humble-desktop \
bash
Then inside the container:
source /opt/ros/humble/setup.bash
colcon build
. install/setup.bash
- Build Custom Docker Image with your workspace prebuilt:
FROM osrf/ros:humble-desktop
WORKDIR /root/ros2_ws
COPY ./ros2_ws ./ros2_ws
RUN apt update && apt install -y python3-colcon-common-extensions
RUN . /opt/ros/humble/setup.sh && colcon build
Then build and run:
docker build -t my_ros2_image .
docker run -it --rm --network host my_ros2_image
Enjoy Reading This Article?
Here are some more articles you might like to read next: