ROS2 Tutorials

Understanding Docker for Robotics Development

Docker is a powerful platform for developing, shipping, and running applications inside lightweight, portable containers. In the field of robotics, Docker provides an efficient way to set up and manage complex environments, ensuring consistency across development, testing, and deployment stages.

What is Docker?

Docker is an open-source platform that enables developers to package applications and their dependencies into containers. These containers run consistently across various environments, eliminating the common issue of “it works on my machine” but not elsewhere.

Key concepts of Docker include:

Why Use Docker in Robotics?

Using Docker in robotics projects, especially with frameworks like ROS2, offers several benefits:

  1. Environment Consistency: Ensures all team members work with the same software environment, avoiding dependency mismatches.
  2. Simplified Deployment: Deploy robotic applications seamlessly across different machines and cloud services.
  3. Isolation: Run different ROS versions or other conflicting dependencies without interference.
  4. Portability: Easily move robotic applications between development, testing, and production environments.
  5. Scalability: Deploy multiple instances of robotics applications in distributed systems, such as robotic swarms or cloud-based robotics platforms.

Key Features of .xacro Files

1. Docker Images

A Docker image is a blueprint for creating containers. It contains the necessary files, dependencies, and configurations for running an application.

Example of a Dockerfile for a ROS2 Humble environment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Use official ROS2 Humble image as base
FROM osrf/ros:humble-desktop

# Install additional dependencies
RUN apt-get update && apt-get install -y \
    ros-humble-navigation2 \
    ros-humble-rviz2 \
    && rm -rf /var/lib/apt/lists/*

# Set environment variables
ENV ROS_DOMAIN_ID=0

# Source ROS2 setup script
CMD ["bash", "-c", "source /opt/ros/humble/setup.bash && bash"]

To build the image:

docker build -t my_ros2_environment .

2. Docker Containers

A container is a running instance of a Docker image. It provides an isolated environment for executing applications.

To start a container from the previously built image:

docker run -it --rm my_ros2_environment

Options explained:

3. Docker Volumes

Docker volumes are used to persist data generated and used by Docker containers. In robotics applications, volumes can be used to store logs, configuration files, and calibration data.

Example of running a ROS container with a mounted volume:

docker run -it --rm -v $(pwd)/ros_ws:/root/ros_ws my_ros2_environment

4. Docker Compose

Docker Compose allows you to define and manage multi-container applications using a docker-compose.yml file. This is useful for setting up complex robotic systems involving multiple nodes and services.

Example docker-compose.yml for a robotics project:

1
2
3
4
5
6
7
8
9
10
version: '3'
services:
  ros_master:
    image: osrf/ros:humble-ros-core
    command: roscore
  robot_control:
    image: my_ros2_environment
    depends_on:
      - ros_master
    command: ros2 launch my_robot bringup.launch.py

To start the multi-container application:

docker-compose up

Common Docker Commands for Robotics

Command Description
docker build -t . Build an image from a Dockerfile
docker images List available images
docker run -it Run a container interactively
docker ps List running containers
docker stop Stop a running container
docker rm Remove a stopped container
docker rmi Remove an image
docker volume ls List available volumes
docker-compose up Start services defined in a docker-compose.yml

Advantages of Using Docker for Robotics Projects

  1. Cross-Platform Development: Develop on any machine (Windows, macOS, Linux) and deploy seamlessly.
  2. Version Control: Ensure all dependencies are fixed and reproducible.
  3. CI/CD Integration: Automate the building, testing, and deployment of robotic applications.
  4. Cloud Integration: Easily deploy robot simulations or data processing in the cloud.

Real-World Use Cases

  1. Simulating Robots: Run full robotic simulations using Gazebo and ROS in containers without affecting the host system.
  2. Deployment to Edge Devices: Easily ship pre-configured robot software to embedded devices like Raspberry Pi.
  3. Testing Multiple Configurations: Spin up different ROS environments to test compatibility across versions.

Running a Complete ROS2 Project with Docker

Let’s say you have a ROS2 project with a workspace containing packages. You can use Docker to encapsulate the environment as follows:

  1. Create a Dockerfile:
1
2
3
4
5
6
7
8
9
10
FROM osrf/ros:humble-desktop

WORKDIR /root/ros2_ws
COPY . /root/ros2_ws

RUN apt update && apt install -y \
    ros-humble-rviz2 \
    && rm -rf /var/lib/apt/lists/*

CMD ["bash", "-c", "source /opt/ros/humble/setup.bash && colcon build && ros2 launch my_package my_launch.py"]
docker build -t ros2_project .
  1. Build the Docker image:
docker build -t ros2_project .
  1. Run the project:
docker run -it --rm ros2_project

By leveraging Docker for robotics projects, developers can achieve a more streamlined, consistent, and scalable workflow for managing complex robotic systems.