Configure Docker environment of ROS

Configure Docker environment of ROS


  • Configure two independent Docker environments, ROS1 and ROS2
  • GUI applications in the ros docker container on the local display server, such as rqt, rviz, etc
  • From the local, you can directly ssh log in to the ROS container of the server
  • Mount a directory of the server into the container to facilitate file transfer
  • Expose the ROS port to externally connect the nodes in the container

Method 1

1. Obtain the ROS image with desktop version from OSRF


docker pull osrf/ros:noetic-desktop-full-focal  # ros-noetic ubuntu20.02
docker pull osrf/ros:galactic-desktop  # ros-galactic

2. Xshell and Xmanager preparation

  1. In order to display the GUI applications in the docker container on the server locally, you need to check X11 transfer and open Xmanager -Passive to receive the interface from the server

3. Create container

export DISPLAY=local ip:0.0
docker run -it \  interactive
		   --name ros-noetic-container \
		   --privileged \  # root permissions: including device permissions
		   -v /tmp/.X11-unix:/tmp/.X11-unix \  # Map display service node
		   -e DISPLAY=$DISPLAY \  # Configure display parameter local ip:0.0
		   -v $HOME/noetic_dir:/root/noetic_shared  \ # share directory
		   osrf/ros:noetic-desktop-full-focal  \ # Official image

4. Setting enables local users to directly ssh log in to the container

  1. Modify the root password of the container: passwd, and enter a new password, for example: 123

  2. Install VIM: apt get update, apt get install VIM - y

  3. Install the openssh server of the container, and enter apt get install openssh server - Y

  4. After successful installation, vim /etc/ssh/sshd_config, modify the following two configurations

    PermitRootLogin yes
    UsePAM no

  5. Start ssh service, service ssh start

  6. Exit the container, enter exit, and then enter docker ps -a to view the ID of the container

  7. The submission container becomes a new image, such as ROS noetic SSH, docker commit container ID ROS noetic SSH

  8. Start the mirrored container, map a local idle port (for example, 10000) to port 22 of the container, and start the sshd of the container

    docker run -d --name ros-noetic-container --privileged -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -v $HOME/noetic_dir:/home/noetic_shared -p 10000:22 ros-noetic-ssh /usr/sbin/sshd -D
  9. At this point, you can use the xshell remote server ip and port 10000 to directly access the container, and the server directory $HOME/noetic_dir is also mounted to the container / home/noetic_shared is in

5. Local display of GUI app in container

# Installation (in server and container)
sudo apt install xserver-xorg x11-xserver-utils
# (optional) allow all users to access xserver
xhost +
# Set the server environment variable to forward X11 to the local machine (server and container)
export DISPLAY=Local machine ip address:0.0  # Write ~ / In bashrc
# vim ~/.bashrc adds the following:
export DISPLAY=
export ROS_DISTRO=noetic
source /

6. commit image

docker commit container ID ros-noetic-ssh  # galactic isomorphism

Method 2

The above method can ensure that different containers are isolated. In order to expose the ROS node ports in the container, the following methods can be used

1. Create a container with -- net=host, share the network with the host, and no mapping port is required:

docker run -it --name ros-galactic-container --privileged -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -v $HOME/galactic_dir:/root/galactic_shared --net=host ros-galactic-ssh /bin/bash  # The image after commit in the previous method is used here: openssh is installed and the GUI in the container can be displayed locally

2. Enter the container and modify the ssh port number

vim /etc/ssh/sshd_config

3. Exit the container, start the container in the background, and log in to the container remotely via ssh

docker exec -d ros-galactic-container /usr/sbin/sshd -D

Container use

  1. The server ( has opened two containers: Noetic and Galactic, which can be viewed through docker ps -a

  2. You can ssh directly into the container:

    • Noetic
      • Host: 10.118 seven point one four two
      • Port: 10000
      • User name: root
      • Password: 123
      • SSH tunnel check forwarding X11 to connect to X DISPLAY: localhost:0.0
    • Galactic
      • Host: 10.118 seven point one four two
      • Port: 10001
      • User name: root
      • Password: 123
      • SSH tunnel check forwarding X11 to connect to X DISPLAY: localhost:0.0
  3. Other versions of ROS can be configured according to the above method

Keywords: Linux Docker ssh ROS

Added by K3nnnn on Fri, 17 Dec 2021 12:20:43 +0200