Docker for robots: ending the "works on my machine" problem
Atlasbot · 20/8/2026 · 11 min read
A ROS2 project runs fine on your laptop, but your cloned teammate reports a missing library error — or worse, the code runs fine on Ubuntu 22.04 but the real robot is installing Ubuntu 20.04 which is not ROS2 Humble compatible. Docker solves this class of problems: packaging the entire environment (background OS, ROS2, libraries, environment variables) into a image that runs identically on every machine with Docker, regardless of what version of Ubuntu that machine actually has installed.
How are containers different from virtual machines (VMs).
A virtual machine (VM) emulates an entire computer, including its own kernel — heavy, slow to boot (in minutes), consumes a lot of RAM/CPU just to run the emulated operating system. Container (of which Docker is the most popular tool) does not emulate the kernel — it shares the kernel of the (real) host machine, only encapsulates the userspace (libraries, programs, environment variables) — starts up in a few seconds, consuming almost as much resources as running a regular program.
Trade-off: containers are not completely isolated like VMs (sharing the same kernel), but for the purpose of "encapsulating a software environment to run consistently across multiple machines" — exactly the problem of ROS2 — containers are a much lighter and faster option.
How are Image and Container different?
- Image — a pre-packaged "blueprint": base operating system + installed software + configuration, in static form (not running), can be shared via Docker Hub like sharing a file.
- Container — an image that is running — can create multiple containers from the same image, each container operating independently.
In short: Image is like a fully ready installation file; The container is the version that is actually running from that file — similar to the relationship between a class and an object in object-oriented programming.
Dockerfile — image creation recipe
FROM ros:humble-ros-base
# Install additional necessary packages
RUN apt update && apt install -y \
ros-humble-navigation2 \
ros-humble-slam-toolbox
# Copy the ROS2 workspace code into the image
COPY ./ros2_ws /root/ros2_ws
WORKDIR /root/ros2_ws
RUN . /opt/ros/humble/setup.sh && colcon build
CMD ["bash"]
FROM ros:humble-ros-base start from the official ROS2 Humble image (which already has Ubuntu 22.04 + ROS2 fully installed) instead of installing from scratch — this is why Docker solves the "wrong Ubuntu-ROS2 version" problem mentioned in the Ubuntu article: the official image always has the correct, verified Ubuntu-ROS2 pair.
Build and run containers
# Build image from Dockerfile in current directory, name "my_robot"
$ docker build -t my_robot .
# Run container, --network host for ROS2 discovery to work (see Common Errors section)
$ docker run -it --network host my_robot
root@a1b2c3:/root/ros2_ws# ros2 topic list
/parameter_events
/rosout
Technical details
Formulas
Each command line in the Dockerfile creates a cached layer — Docker only rebuilds from the first layer with changes onwards, keeping previous layers intact. The order of commands in the Dockerfile therefore directly affects the rebuild speed:
# LESS EFFECTIVE — copy code before installing package
COPY ./ros2_ws /root/ros2_ws
RUN apt install -y ros-humble-navigation2 # Rebuild EVERY TIME even if you only change one line of code
# MORE EFFICIENT — install the package (with few changes) first, copy the code (with changes) later
RUN apt install -y ros-humble-navigation2 # This layer CACHE, do not rebuild
COPY ./ros2_ws /root/ros2_ws # Only this layer rebuilds when editing code
Putting less-changing steps (system settings) first, and frequently changing steps (copying source code) last helps rebuild significantly faster in the daily development loop.
Parameters
Reference image size — determines load/storage time, especially important if images need to be transferred over a slow network to a robot in the field:
| Image type | Reference size |
|---|---|
ros:humble-ros-base (minimum, no GUI) | ~1GB |
ros:humble-desktop-full (full GUI, RViz2, simulation) | ~4-5GB |
| Image builds Nav2/SLAM Toolbox itself | Plus a few hundred MB depending on the package |
Practical applications
Docker also solves the problem of "the code runs on a CI machine (automated testing) but fails when deployed to a real robot" — using exactly 1 Docker image for both environments: CI pipeline (eg: GitHub Actions) builds and runs tests inside the same image, then that tested image is deployed directly to the robot - ensuring the test environment and the real running environment are absolutely identical, completely eliminating the layer of risk "The CI machine has a different robot machine configuration" that has caused many errors that only appear after deployment.
Practical example: mount source code to fix the code without rebuilding the image
Rebuilding the image every time you edit a line of code is very slow — use volume mount to let the container directly read the code on the real machine, the changes take effect immediately without needing to rebuild:
$ docker run -it --network host \
-v ~/ros2_ws/src:/root/ros2_ws/src \
my_robot
-v <host-path>:<container-path> is the mount syntax — edit the file on the real machine (using the familiar VS Code/editor) and the container sees the changes immediately.
Common mistakes
permission deniedwhen running commanddocker— user is not in groupdocker, same cause as errordialoutin basic Linux article:sudo usermod -aG docker $USER, then log out/log in again.- The ROS2 node in the container cannot see the node on another machine/container — ROS2 uses DDS multicast to self-discover the node, by default Docker isolates the container network from the host network — must be run with flag
--network hostfor DDS discovery to work across containers, as in the example above. - Container cannot access
/dev/ttyUSB0of LiDAR/Arduino — by default the container cannot see the host's hardware device, need to add a flag when running:docker run --device=/dev/ttyUSB0 ...(or--privilegedto see the entire device, less secure but convenient when debugging). - RViz2/GUI does not appear when running in a container — similar to the X11 forwarding problem in the SSH article, need to configure additional variables
DISPLAYand mount/tmp/.X11-unix— more complicated than the scope of this article, you can find a base image with a pre-configured GUI instead of configuring it yourself from scratch.
Conclusion
Docker solves the problem of "inconsistent software environments across machines" by packaging both the underlying operating system and ROS2 into a shared image — no need to worry about whether the real robot and the dev laptop have the same version of Ubuntu. The last two articles in the section turn to specific hardware: NVIDIA Jetson — the most popular line of embedded computers with GPUs for AI robots, and how to install the Jetson Orin Nano specifically.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.