How I Set Up ROS 2 on My MacBook Using Docker Without Losing My Sanity
So there I was, staring at my MacBook with the determination of a person who’s just promised to take up jogging. The task? Get ROS 2 running on this sleek piece of aluminum. But of course, I wasn’t running Ubuntu — the darling child of the ROS community. No, I was on macOS, the oddball cousin who always insists on doing things their own way. And just like that, I knew I was in for an adventure.
The Container Life!
With Docker up and running, I pulled down the ROS 2 image like a pro:
docker pull osrf/ros:foxy-desktop
Running the Docker container was supposed to be the easy part. So I typed in:
docker run -it osrf/ros:foxy-desktop
WARNING: The requested image’s platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested.
Well, of course, I forgot that my MacBook has an M1 chip — an ARM-based powerhouse. And here I was, trying to shove an AMD64 image into it. The error message was basically Docker’s way of saying, “Uh, are you sure about this?”
Not to be outdone by a piece of silicon, I added the platform flag (a bit of emulation), now if you’re a steady believer of — slow and steady, keep reading (else, keep googling :P) :
docker run -it --platform linux/amd64 osrf/ros:foxy-desktop
Sure, it might run slower due to emulation. Okay, so now I had ROS 2 running in Docker. But every time I stopped the container, all my hard work would disappear. I needed a persistent workspace — a place where my ROS 2 code could live forever (or at least until I deleted it).
docker run -d \
--name ros2_container \
--env="DISPLAY=host.docker.internal:0" \
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
-v ~/ros2_ws:/root/ros2_ws \
osrf/ros:foxy-desktop
THE DETACHED MODE does not WORK!
Speaking from experience and a working knowledge of dockers, i.e., when you run a container in detached mode, Docker runs it in the background without attaching your terminal to the container’s standard input, output, or error. This is great for servers but can be tricky when you want a persistent, interactive environment where you can still interact with GUI applications.
The problem is that the container doesn’t stay alive unless it has a command that runs indefinitely or until manually stopped. When you try to run GUI applications in detached mode, they often don’t have the context they need to run properly.
The easiest solution is to simply not use detached mode. Here’s how to keep the container running interactively:
docker run -it \
--name ros2_container \
--env="DISPLAY=host.docker.internal:0" \
--volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
-v ~/ros2_ws:/root/ros2_ws \
osrf/ros:foxy-desktop
This work, why?
- By keeping the container in interactive mode (
-it
), the terminal stays attached, and you have direct control over the container. - The GUI apps can access the display because they’re running in an interactive session where the necessary environment variables are properly set.
Using the container
If you are non a fan of terminal code editor, there’s a way around that, keep reading. The simple way to access it to use the docker exec
:
docker exec -it ros2_container bash
Here’s how you can access it with vscode:
- Open VSCode Command Palette (
Cmd+Shift+P
orCtrl+Shift+P
on Windows/Linux) and type "Dev Containers: Attach to Running Container...". - Select the running container that you started previously with ROS 2.
- VSCode will open a new window connected to your Docker container. You’ll have access to the file system of the container, and you can edit files directly inside it.
- Open the
~/ros2_ws
directory and start coding.
Pro Tips for the Future:
- Persisting Changes: Since your workspace is mounted to your Mac, all changes persist. If you ever need to switch containers or update your environment, everything stays in place.
- Extension Support: Consider installing relevant VSCode extensions (like the ROS extension) to enhance your development experience. It’s not mandatory, but it can make life easier.
- Container Reuse: If you want to reuse the same container setup in the future, consider saving the exact
docker run
command or creating a small script for easy re-deployment.
In conclusion, by connecting directly to the Docker container from VSCode, you’ve created a powerful and flexible ROS 2 development environment on your Mac. This setup is both efficient and effective, allowing you to focus on building and running your ROS 2 projects without the usual headaches. Well done!