ROS development environment configuration of VScode combined with docker

Posted by raymie on Fri, 11 Feb 2022 10:45:42 +0100

Software installation


There are three installation methods, as follows:

  • stay vscode official website Download the required version and install it directly.
  • Search and install in the software center under ubuntu (the easiest installation, recommended):
  • Install using apt:
wget -q -O- | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] stable main"
sudo apt update
sudo apt install code


reference resources Install Docker Engine , choose the installation tutorial on the left side of other platforms. I won't repeat it.
After installation, you need to add the current user to the docker group. Otherwise, you need root permission to use the docker instruction. The commands of different platforms are different. Here, take Ubuntu 18 04 as an example:

sudo usermod -aG docker <your_user_name>

docker image construction and operation

1. Write a dockerfile to build a dockerfile of image in ros2 foxy version Txt as an example:

# Build based on an image, such as a version of ubuntu or an official image of ros
FROM ros:foxy-ros-base-focal

# Install the environment you need
RUN apt-get update && apt-get install -y --no-install-recommends \
    ros-foxy-desktop \
    && rm -rf /var/lib/apt/lists/*

In addition, if you need to use nvidia graphics card in docker, you need to:

  • x86 platform: additional installation nvidia container toolchain , and add the following environment variable configuration in dockerfile:
# You need to configure environment variables when you need to use nvidia graphics card

2. build image, in dockerfile Txt run in the same directory:

docker build -t <your_image_name>:<tag> .
docker build -f <your_dockerfile_name> -t <your_image_name>:<tag> .

3. Run container:

docker run -it --rm \
  --user=$(id -u $USER):$(id -g $USER) \
  --workdir="/home/$USER" \
  --volume="/home/$USER:/home/$USER" \
  --volume="/etc/group:/etc/group:ro" \
  --volume="/etc/passwd:/etc/passwd:ro" \
  --volume="/etc/shadow:/etc/shadow:ro" \
  --volume="/etc/sudoers.d:/etc/sudoers.d:ro" \
  --net=host \
  --gpus all \
  -e DISPLAY \
  -e QT_X11_NO_MITSHM=1 \
  -e XAUTHORITY=/tmp/.docker.xauth \
  -v /tmp/.docker.xauth:/tmp/.docker.xauth \
  -v /tmp/.X11-unix:/tmp/.X11-unix \

The first seven items above are to mount the machine. The user is the default user in the container. Otherwise, the container will be entered as root user. It is not recommended to use root user for development operations-- net=host is to map the local network to the container; Next 6 items are related to the use of graphics card and display; Refer to the official documents for more usage.
The use of VSCODE generally requires a fixed long-term development environment. Therefore, the container will not be deleted after the operation is completed. When starting, remove the – rm. After the operation is completed, there will be a container in the ros2 environment.

VSCODE configuration

Open VSCODE, take C + + development as an example, and install in the extension:

  • ROS
  • C++ Extension Pack
    After installation, restart or reload VSCODE, and the container that has been started at this time can be seen in the left side bar:

    Right click container - > attach to container and you can easily complete the link between VSCODE and container. At this time, the IDE is already in the ros2 foxy development environment. You can normally open the ROS workspace for development. The same is true for python.
    To use ROS plug-in, first of all, you need to have src folder in the workspace, and the codes are all in this folder. Secondly, you need to have ROS related environment variables when opening VSCODE. There are two implementation methods:
    1. Set source / opt / ROS / foxy / setup Bash is written into the current system user folder The end of bashrc is also a routine operation after ROS installation, but it will take effect only when the container is run with the above command script. Therefore, the script will load the current system user as the default user of the container and will also be executed at the same time Commands in bashrc.
    2. When building docker image, add entrypoint SH, execute source / opt / ROS / foxy / setup Bash, there are many tutorials, which will not be repeated here.

Generally speaking, the ROS plug-in will automatically detect the ROS version and automatically open the ROS workspace Configure in the vscode folder, but there are occasional problems when using docker. At this time, you need to manually set the ROS version in the ROS plug-in settings, as follows:

Reopen the workspace or manually run some commands of the ROS plug-in. Please refer to the instructions provided by the ROS plug-in for details.

Cland configuration

vscode's built-in intellisense has many problems when used for code completion, especially in the docker environment. Therefore, it is recommended to change to Cland for development. Using Cland requires additional configuration. The steps are as follows:
1. Search for Cland in the plug-in and install it. After the installation is completed, the lower right corner will automatically prompt to install Cland server. Click yes.
2. Compile the whole project once in ROS environment, and add "- DCMAKE_EXPORT_COMPILE_COMMANDS=1" instruction. Take ros2 foxy as an example:


It is necessary to ensure that the compilation is completed successfully, otherwise the compilation commands of some files cannot be generated, resulting in that Cland cannot find the index of this file, which is similar to non ROS environment.
3. Restart VSCODE or Cland server.

Topics: Docker Ubuntu Visual Studio Code