Working with robotics involves using many different software suites that can often clash with each other. In Python projects, environments like Anaconda or virtual enviroments are commonly used to manage dependencies. However, ROS installations can be tedious and are often prone to breaking. Therefore, using Docker containers is often a better option than relying on a base installation, as they provide a more stable and isolated environment.
Warning
- Skip following instructions in this section if you plan to use any other OS except Ubuntu 22.04 or Ubuntu 20.04. Note that running a docker container is independent of this.
- This section is for Computer with a Nvidia GPU ONLY. People with other GPUs should skip this section.
For any of your projects, having a stable Ubuntu Installation is of foremeost importance as the main image installed in you computer provides a stable foundation for everything else, be it a conda environment or as it is in this case, a Docker Image.
For this course, and for a stable installation, we recommend that you install either Ubuntu 20.04 or Ubuntu 22.04.
Note
Although as of the moment this tutorial is being written, Ubuntu has been out for around 4 months, it doesn't support Lambda stack yet, which we use to have a super stable installation that makes sure that you don't have to deal with driver issues.
Important
Remember to be equipped with the following things before you get started with the installation:
- A computer with an Ubuntu 22.04 or 20.04 installation (prefereable fresh but not important).
- A stable and high - speed internet connection
- High patience. At times, the downloads may be slow regardless of how fast your connection is. It may be just an hour or as long as 6 to 8.
- Habit of periodically checking in.
After you are done installing Ubuntu, follow the following instructions from Lambda Labs to install the Lambda stack
-
Run the first command:
wget -nv -O- https://lambdalabs.com/install-lambda-stack.sh | sh - -
Once it is done, proceed to reboot by doing:
sudo reboot -
Now, proceed to update everything to the latest installation
sudo apt-get update && sudo apt-get dist-upgrade
Note
- It is a good practice to regularly update your system using:
sudo apt-get update && sudo apt-get upgrade - followed by:
sudo apt-get dist-upgrade
-
We now utilize a GPU accelerated docker using the following command:
sudo apt-get install docker.io nvidia-container-toolkit
And that's it, you have a very stable installation that includes PyTorch®, TensorFlow, CUDA, cuDNN, and NVIDIA Drivers!
Now that you have all the prerequisites installed with Ubuntu, we can get started with Docker. Note that we are installing only Docker Engine, and not Docker Desktop.
(Official Instructions to install Docker Engine)
Note
Caution
Skip this sub-section and move directly to sub-section 2.2 if you followed section 1
-
Uninstall old packages:
for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done -
Install Docker Engine using Docker's
aptrepository, by running the sequence of commands below. They are in bullets to avoid confusion.-
sudo apt-get update -
sudo apt-get install ca-certificates curl -
sudo install -m 0755 -d /etc/apt/keyrings -
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc -
sudo chmod a+r /etc/apt/keyrings/docker.asc -
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null -
sudo apt-get update
-
-
Install the Docker Packages:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -
Verify that the Docker Engine installation is successful by running the
hello-worldimage:sudo docker run hello-world
After installation, for Ubuntu based installation, you must run the following commands:
sudo groupadd dockersudo usermod -aG docker $USER
Now, reboot your system with:
sudo reboot
Login again and run the following command:
newgrp docker
Finally, to verify that we can run docker without sudo, run the hello-world container using the command:
docker run hello-world
If it runs, you have successfully run the post-installation steps.
We are finally ready to use our ROS Docker image. To do that, clone this repo, build the docker image, and run it using the following steps:
sudo apt-get install git
-
Clone this repository using the command:
git clone https://github.com/Kelebrimbor97/ROS-Docker.git
Tip
Remeber to do a git pull from within your repo to update your repo to the latest versions.
-
Navigate to to location where the Dockerfile is stored:
cd ROS-Docker/Scripts/Galactic/ -
Build the docker Image using this command:
docker build -t modelling_ros .
Important
Take a look at the comments on lines 9 and 10 of the in Scripts/Galactic/Dockerfile before you run this command.
Tip
modelling_roscan be replaced with any other name of your choice, but remember to replace it in future Dockerfiles you create.
Note
- The following 2 commands are what you have to use everytime you want to run the docker.
-
Allow docker to have port access:
xhost +local:docker -
Spin up a container by using the image created in the previous step:
docker run -it --rm --name=project_0 --gpus=all --net=host --pid=host --privileged --env="DISPLAY=$DISPLAY" modelling_ros
Tip
- You can again have a container name of you choice instead of
project_0.
-
It's Alive! If you see something like this, your docker container is up and running:
root@User:/#If not, feel free to cry...
Warning
- Any changes you make to the docker container will not be saved and will be lost once the current container is shut down.
Once you have a container running, start using it as you would with a terminal. The only difference is that you never need to use sudo again.
For opening a different terminal in the same container, get the container name and exec into it. Example:
docker exec -it project_0 bash
Given that we have a base image with ROS installed, we will create another image from the base one for our turtlebot3. Think of this similar to having a child class inheriting qualities from the parent class.
For doing that, navigate to Scripts/Galactic/Projects/Turtlebot3 and simply build the image from the Dockerfile using the following command:
docker build -t turtlebot3_galactic .
If you would like to use VS Code as a development enviornment, there is an excelent integration with Docker that is recommended. Follow these steps to set up a workspace for VS Code and Docker.
- Run the following command in a terminal to install vs code
sudo snap install --classic code # or code-insiders
-
Open vs code and press ctrl+shift+x
-
type "Dev Containers" and download the extension
Create a workspace folder where you would like your project and copy the .devcontainer folder from Scripts/Galactic/VSCode into it.
ex.
-
cd ~ -
mkdir myworkspace -
cp ./ROS-DOCKER/Scripts/Galactic/VSCode/.devcontainer ./myworkspace
-
Open the workspace folder in vs code.
-
Press ctrl+shift+P and type "Dev Containers: Open Folder in Container" and press enter
-
The VS Code window should reload and you are now working in a docker container!
Tip
- You can open more terminals with ctrl+shift+`