Introduction
Developing applications with Robot Operating System 2 (ROS 2) can be challenging due to the complex dependencies, system compatibility issues, and collaboration hurdles that often arise. Managing consistent environments across different machines or teams is often a time-consuming process that can detract from actual development. However, by utilizing VS Code Devcontainers, you can simplify ROS 2 development with Docker-based, isolated, and reproducible development environments that are easy to share and deploy.
In this article, we’ll dive into why VS Code Devcontainers are an ideal choice for ROS 2 development, how to set them up using a ready-made template, and what benefits they offer to streamline your workflow.
Why Use VS Code Devcontainers for ROS 2 Development?
- Reproducibility Across Teams
- Devcontainers ensure that every developer works in the same environment, eliminating the frustrating “works on my machine” issues. The entire workspace, including dependencies and system configurations, is containerized and easily shared.
- Quick Setup and Onboarding
- New developers can start working immediately after cloning the repository and opening it in VS Code, with no need to manually configure the development environment. Docker handles all dependencies, saving time and effort.
- System Isolation
- By using Docker, devcontainers isolate your ROS 2 environment from your host machine. This ensures no conflicts with other local software and keeps your host system clean and stable.
- Cross-Platform Compatibility
- Whether you’re working on Windows, macOS, or Linux, devcontainers provide a unified, containerized development environment. This eliminates platform-specific quirks, ensuring consistent results across the entire team.
- Easy CI/CD Integration
- Because your development environment is Docker-based, it can easily be integrated into continuous integration (CI) pipelines, ensuring that the development, testing, and production environments remain identical.
Setting Up a ROS 2 Devcontainer
Using the ROS 2 Devcontainer Template greatly simplifies the setup process. Below are the steps to get started.
1. Install Prerequisites
To use VS Code devcontainers for ROS 2 development, you need to have the following tools installed:
- Docker: Download and install Docker Desktop, and ensure it’s running on your machine.
- VS Code: Download and install Visual Studio Code, the primary editor you’ll use for development.
- Remote – Containers Extension: Install this extension from the VS Code marketplace to enable container-based development within the IDE.
2. Use the ROS 2 Devcontainer Template
For this tutorial we will be using the ROS 2 Devcontainer Template found at Robotics-Content-Lab/ros2_devcontainer.
You can either create a new repository from this template via the GitHub CLI or directly through GitHub’s interface:
- Using Github CLI:
gh repo create ros2_ws --template Robotics-Content-Lab/ros2_devcontainer --private --clone
- Using GitHub Web UI:
- Click the “Use this template” button on the repository page.
- Create a new repository.
After cloning the template, you can use a variety of helpful scripts to manage your ROS 2 Docker environment. For example:
- run_container.sh / .ps1: Easily launch the ROS 2 Docker container on Unix or Windows.
- attach_terminal.sh / .ps1: Quickly attach a terminal to an already running container.
These scripts simplify interaction with the containerized development environment. The container configuration is managed through the devcontainer.json
file, located in the .devcontainer/
folder. This file defines the build configuration, extensions, and other essential settings.
Additionally, two Dockerfiles are available to customize your environment:
- Dockerfile.base: Sets up the base image with core ROS 2 dependencies.
- Dockerfile.full: Extends the base image by adding extra development tools and packages.
The Docker container also utilizes an entrypoint script to ensure the correct environment setup. You can find and modify this script in .devcontainer/entrypoint.sh
.
3. Add your ROS 2 Packages to the Workspace
If you have existing ROS 2 packages you want to work on, add them to a folder on your local machine. This folder will be mounted into the devcontainer, allowing you to access and modify the packages from within the container.
4. Open the Repository in VS Code
Once you’ve cloned the template, open the project in VS Code:
code .
VS Code will detect the .devcontainer
folder and prompt you to reopen the project in a containerized environment.
5. Reopen in Container
When prompted by VS Code, click “Reopen in Container”. This will trigger the creation of a Docker container based on the configurations in the Dockerfile
and devcontainer.json
. The setup process might take a few minutes the first time, as Docker pulls and installs all necessary dependencies for ROS 2.
Once the container is up and running, you will be inside a terminal with a fully configured ROS 2 environment.
6. Start Developing with ROS 2
Now that your devcontainer is set up, you can begin developing ROS 2 applications. The container includes ROS 2, allowing you to use all familiar ROS 2 CLI tools. Additionally, any dependencies or packages you need can be managed within the container, avoiding clutter on your host machine.
7. Customizing Your Devcontainer
You can tailor the default devcontainer configuration to meet your project’s specific requirements by modifying the devcontainer.json
file. For example, you can add new extensions or dependencies:
{
"image": "ghcr.io/robotics-content-lab/jazzy:full",
"extensions": [
"ms-iot.vscode-ros",
"ms-azuretools.vscode-docker"
],
"postCreateCommand": "rosdep update && rosdep install --from-paths src --ignore-src -r -y",
"mounts": [
"source=${localWorkspaceFolder},target=/workspace,type=bind"
]
}
In this example, the postCreateCommand ensures ROS dependencies are automatically installed, and the extensions provide additional tools for developing with ROS 2 and Docker.
Key Benefits of Using VS Code Devcontainers for ROS 2 Development
- Simplified Setup for ROS 2
- Setting up ROS 2 manually on your local machine can be time-consuming and complex due to numerous dependencies. Devcontainers handle the entire setup automatically, providing you with a fully configured ROS 2 environment from the start.
- Consistent Development Environment
- Since the entire environment is containerized, every team member works with the same setup, regardless of their operating system. This ensures everyone has the correct versions of tools and packages.
- Efficient Collaboration
- Sharing your development environment with new team members is as simple as sharing the repository. Devcontainers eliminate the need for manual environment setup, helping new developers get started more quickly.
- Seamless Integration with CI Pipelines
- Since the Docker configuration used in development is the same as the one used in testing and production, you can integrate your devcontainer setup directly into your CI/CD pipelines. This reduces the likelihood of bugs due to environmental inconsistencies.
- Improved ROS 2 Tooling
- The ROS 2 VS Code extension integrates tightly with the devcontainer setup, providing features like code completion, syntax highlighting, and debugging capabilities for ROS 2 nodes directly in VS Code.
Conclusion
Utilizing VS Code devcontainers for ROS 2 development significantly reduces the time spent on environment setup, eliminates compatibility issues across systems, and enhances collaboration within teams. The ROS 2 Devcontainer Template provides a pre-configured, reproducible environment that allows you to focus on what matters most—developing robust ROS 2 applications.
Give it a try today, and streamline your ROS 2 development process!
FAQs
1. What is a devcontainer?
- A devcontainer is a containerized development environment defined using Docker, allowing for easy replication and sharing of setups. For ROS 2 development, it ensures consistent environments across machines and platforms.
2. Do I need ROS 2 installed on my local machine?
- No, the devcontainer setup includes ROS 2 within the Docker container, so it doesn’t need to be installed on your local system.
3. Can I customize the ROS 2 devcontainer for my project?
- Yes, you can modify the
devcontainer.json
file to add custom packages, extensions, or environment settings tailored to your project needs.
4. Does the devcontainer work on all operating systems?
- Yes, as long as you have Docker and VS Code with the Remote – Containers extension, the devcontainer setup works on Windows, macOS, and Linux.
5. How do I integrate the devcontainer with a CI/CD pipeline?
- You can configure your CI pipeline to build and test code within the same Docker environment as your devcontainer, ensuring consistency across development, testing, and production environments.
6. Can I use this setup for different ROS 2 distributions?
- Yes, the template supports various ROS 2 distributions. You can modify the Dockerfile to specify which ROS 2 version to use, such as Foxy or Humble.
One Response
Hello! This post couldn’t be written any better! Reading through this post reminds me of my old room mate!
He always kept talking about this. I will forward this page to him.
Fairly certain he will have a good read. Thank you for sharing!