Say that you are using Windows or macOS, which do not support ROS/ROS2. Or, say that you want to work with F1Tenth-Gym-ROS, which uses ROS-Foxy (❗and we definitely do in this Bootcamp❗). In this case, you would need Ubuntu 20.04 to run ROS-Foxy natively. Otherwise, you would need to use Docker.
For example, the steps are as follows if you are using Ubuntu 22.04:
-
Follow the Docker installation instructions
-
Confirm that Docker works by running
docker --version
If this shows a version, you're good.
-
Pull the ROS-Foxy Docker image.
Docker Hub has an official image for ROS Foxy based on Ubuntu 20.04. Run:
docker pull ros:foxy
This downloads the base image with:
- Ubuntu 20.04
- ROS-Foxy pre-installed
-
Start the container (Interactive Terminal)
Now run the container and enter it:
docker run -it --name ros2_foxy_container ros:foxy
This opens a terminal inside the Ubuntu 20.04 + ROS Foxy container.
-
Source ROS inside the container
Inside the container, run:
source /opt/ros/foxy/setup.bashNote that, each time you want to use ROS2, you need the run the command above.
-
Check if you can use ROS-Foxy
You can now use ROS-Foxy inside the container. Try:
ros2 --help
-
(Optional) Exit and restart
To exit the container, run:
exitTo restart it later, run:
docker start -ai ros2_foxy_container
-
(Optional) To delete the container
First, exit the container:
exitThen, on your host system, remove it using:
docker rm ros2_foxy_container
-
(Optional) To list Docker containers
If you want to list only the containers that are running
docker ps
If you want to list all containers (running + stopped)
docker ps -a
When you make a Docker container, you are automatically directed to the root directory. When using Ubuntu, it may not be recommended to run everything as root. Then, you might want to have a user within the Docker. However, if you delete the Docker container, that user directoy is also gone.
Also, you probably don't want to specify that the Docker container needs to start from ROS-Foxy all the time when you start a container.
For these reasons, and many other possible reasons, you can use a Dockerfile. A Dockerfile is just a plain text file named Dockerfile (no file extension) that contains instructions for building a custom Docker image. Think of it like a recipe: it tells Docker how to create a container environment exactly the way you want — e.g., install ROS, create a user, set paths, etc.
-
Make a new folder for your Docker setup
On your host, open terminal and run:
mkdir -p ~/ros2_docker cd ~/ros2_docker
-
Create the
DockerfileInside
~/ros2_docker, run:touch Dockerfile vim Dockerfile
Paste the following into the file and save it:
# Start from official ROS Foxy image FROM ros:foxy # Create a user named 'dino' with a home directory RUN useradd -m -s /bin/bash dino \ && echo "dino ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers # Switch to that user USER dino WORKDIR /home/dino # Automatically source ROS 2 when shell starts RUN echo "source /opt/ros/foxy/setup.bash" >> /home/dino/.bashrc
-
Build the Docker image
From the same folder, run:
docker build -t ros2-foxy-dino .This will:
- download the
ros:foxyimage, - create a new user named
dino, - save this as a new image named
ros2-foxy-dino.
It may take a few minutes to complete it for the first time.
- download the
-
Run a container from this image
docker run -it --name ros2_foxy_container ros2-foxy-dino
In many cases, it's good to share a local ROS2 workspace into the container so your code and builds are persistent and you can access the ROS2 workspace from your host computer. This could be done through "bind-mounting".
This is helpful because ❗if you created a workspace inside the container without a bind-mount, and then delete the container, the workspace will be permanently lost❗
-
Create a workspace folder in your host computer
usr@host:~$ mkdir -p ~/basic_ws/src
-
Go into the workspace folder in your host computer
usr@host:~$ cd basic_ws
-
Run the following to start a Docker container with a directory bound-mounted from the host into the container
If you're not using an image, run
usr@host:~/basic_ws$ docker run -it -v /home/usr/basic_ws/:/home/dino/basic_ws/ --name ros2_foxy_basic ros:foxyIf you're using an image, run
usr@host:~/basic_ws$ docker run -it -v /home/usr/basic_ws/:/home/dino/basic_ws --name ros2_foxy_basic ros2-foxy-dinoThis does the following:
docker run: Starts a new Docker container.-it: Interactive terminal session (-i= interactive,-t= allocate a pseudo-TTY).-v <host_path>:<container_path>: Bind-mounts a directory from the host into the container. This allows you to share files between your host and container. ❗Note that only the files inside the directory will be shared between your host and container. Everything else lives inside the container❗--name <container_name>: Assigns the container a name.ros:foxy: Uses the official ROS 2 Foxy Docker image (based on Ubuntu 20.04).
-
Create the workspace in the container
dino@container:~$ cd /dino/basic_ws/ dino@container:~/basic_ws$ colcon build
-
Once the build is complete, you'll see somethings like the following
dino@container:~/basic_ws$ ll total 24 drwxrwxr-x 6 dino dino 4096 Jun 3 01:23 ./ drwx------ 1 dino dino 4096 Jun 3 01:22 ../ drwxr-xr-x 2 dino dino 4096 Jun 3 01:23 build/ drwxr-xr-x 2 dino dino 4096 Jun 3 01:23 install/ drwxr-xr-x 3 dino dino 4096 Jun 3 01:23 log/ drwxrwxr-x 2 dino dino 4096 Jun 3 01:20 src/ -
Again, to use ROS2 in the container, you need to source it inside the container
dino@container:~/basic_ws$ source /opt/ros/foxy/setup.bash
tmux is recommended when you're working inside a container. It could be installed in the container via:
apt update && apt install tmuxtmux allows you to have multiple bash session in the same terminal window. This will be very convenient working inside containers. A quick reference on how to use tmux can be found here. You can start a session with tmux. Then you can call different tmux commands by pressing ctrl+B first and then the corresponding key. For example, to add a new window, press ctrl+B first and release and press c to create a new window. You can also move around with ctrl+B then n or p.
A cheatsheet for the original tmux shortcut keys can be found here. To know about how to change the configuration of tmux to make it more useable (for example, if you want to toggle the mouse mode on when you start a tmux bash session or change the shortcut keys), you can find a tutorial here.