Why run ROS in Docker?

ROS has different versions for different Ubuntu releases. Just to name a few:

Some packages are built for one version while others are build for some different version. For example, many of the open source implementations of SLAM papers are for ROS Melodic. While some are for Kinetic. This can get really frustrating.

Since docker provides a containerized environment, we can have any version of ROS on our system using docker.

Installing Docker

You can follow the official installation guide: https://docs.docker.com/engine/install/ubuntu/

Or you can just copy paste the commands below:

# Install docker
curl -fsSL <https://download.docker.com/linux/ubuntu/gpg> | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88

# NOTE: replace ```  $(lsb_release -cs) ---> focal  ```, if using mint 20 or any other "Ubuntu 20 like" OSes
sudo add-apt-repository \\
   "deb [arch=amd64] <https://download.docker.com/linux/ubuntu> \\
   $(lsb_release -cs) \\
   stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io -y
sudo usermod -aG docker <Your Username> # Run docker without sudo
# Important: Restart/LogOut after this

Docker and Ros

Setting Up ROS Docker

# Create a catkin_ws on host
mkdir -p ~/catkin_ws/src

# Pull docker image
docker pull osrf/ros:melodic-desktop-full

# Allow local connections to X server
xhost +local:root

# Running the docker container **for the first time | With GUI Support**
docker run \\
    -it \\
    -v ~/catkin_ws:/root/catkin_ws \\
    --env="DISPLAY=$DISPLAY" \\
    --env="QT_X11_NO_MITSHM=1" \\
    --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \\
    --name rrcros \\
    osrf/ros:melodic-desktop-full

# Once inside the container, we add ros setup filer to bashrc
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc

# Exiting the container from inside
exit  

############

# Once initialized (3), we will use the following commands
# to interact with the container:

# Restarting the container | This will start it in background
docker start rrcros

# Getting inside a container running in background:
docker exec -it rrcros bash

# Stopping the container (from outside)
docker stop rrcros

Husky Simulator Setup on ROS Docker