A simple Docker setup for running ROS 2 Humble tutorials with a basic publisher/subscriber example using C++ and Python scripts.
.
├── docker
│ ├── docker-compose.yml
│ ├── Dockerfile
│ └── entrypoint.sh
├── README.md
└── ros2_ws
└── src
├── cpp_pubsub
│ ├── CMakeLists.txt
│ ├── LICENSE
│ ├── package.xml
│ └── src
│ ├── publisher_member_function.cpp
│ └── subscriber_member_function.cpp
└── py_pubsub
├── LICENSE
├── package.xml
├── py_pubsub
│ ├── __init__.py
│ ├── publisher_member_function.py
│ └── subscriber_member_function.py
├── resource
│ └── py_pubsub
├── setup.cfg
├── setup.py
└── test
├── test_copyright.py
├── test_flake8.py
└── test_pep257.py
- Dockerfile: Simplified ROS Noetic container setup
- docker-compose.yml: Container orchestration configuration
- cpp_pubsub package: Contains the classic ROS 2 tutorial scripts
talker: Publisher node that sends "hello world" messageslistener: Subscriber node that receives and logs messages
- py_pubsub package: Contains the classic ROS 2 tutorial scripts
talker: Publisher node that sends "hello world" messageslistener: Subscriber node that receives and logs messages
- Docker installed (and opened) on your system
Open your terminal and navigate to a directory where you want to save locally save this repository.
# Clone this repository
git clone https://github.com/usc-robosub/ros-tutorials.git
# Enter the repository
cd ros-tutorials# Enter the docker directory
cd docker
# Build and start the container in detached mode
docker compose up -dThis will:
- Build the Docker image with ROS 2 Humble and your colcon workspace
- Start the container named
ros2-humble-tutorial - Run in the background (detached mode)
You'll need two separate terminal windows/tabs for this demo.
Open a new terminal window/tab:
# Connect to the container in a new session
docker exec -it ros2-humble-tutorial bash# Run the listener node from the cpp_pubsub package
ros2 run cpp_pubsub listenerThe listener is now waiting for messages. You should see a blank terminal waiting for a response from the Talker Node.
Open a third terminal window/tab:
# Connect to the container in a new session
docker exec -it ros2-humble-tutorial bash# Run the talker node from the py_pubsub package
ros2 run py_pubsub talkerYou should now see:
- Terminal 2 (talker): Publishing "hello world" messages
- Terminal 1 (listener): Receiving and displaying those messages
Terminal 2 (Talker):
[INFO] [1772491778.921476429] [minimal_publisher]: Publishing: "Hello World: 0"
[INFO] [1772491779.408108096] [minimal_publisher]: Publishing: "Hello World: 1"
[INFO] [1772491779.907574222] [minimal_publisher]: Publishing: "Hello World: 2"
Terminal 1 (Listener):
[INFO] [1772491778.906705804] [minimal_subscriber]: I heard: 'Hello World: 0'
[INFO] [1772491779.407363763] [minimal_subscriber]: I heard: 'Hello World: 1'
[INFO] [1772491779.907296013] [minimal_subscriber]: I heard: 'Hello World: 2'
In each terminal (in reverse order):
- Terminal 2 (talker): Press
Ctrl+Cto stop the talker node - Terminal 1 (listener): Press
Ctrl+Cto stop the listener node
In each terminal:
# Exit the container bash session
exit# Stop and remove the container, networks, and orphaned containers
docker compose down --remove-orphans --rmi allIf you want to modify the Python or C++ scripts without rebuilding the container, use the dev profile when starting up the container
# Build and start the dev container in detached mode
docker compose --profile dev up -dNow two images and containers have been made, make sure to enter the ros2-humble-tutorial-dev container
# Connect to the container
docker exec -it ros2-humble-tutorial bashAfter making edits to any python scripts within the directories with catkin_ws/src on your local computer, make sure to rebuild resource the ros workspace
# Change back to the root of the workspace
cd /ros2_ws
# Rebuild the ros 2 workspace
colcon build --symlink-install# Resource the new build into the current workspace
source install/setup.bashWhen finished using and exiting the dev container, run the following command from your local machine
# Stop and remove the containers, networks, and orphaned containers
docker compose --profile dev down --rmi all --remove-orphans# View running containers
docker ps
# View container logs
docker-compose logs
# View real-time logs
docker-compose logs -f# Check container status
docker-compose ps
# View detailed logs
docker-compose logs ros2-humble-tutorial
docker-compose logs ros2-humble-tutorial-dev- Ensure all three terminals are connected to the same container
- Check that the container is using
network_mode: host
- Modify the Python or C++ scripts to experiment with different message types
- Add more ROS 2 nodes to the package
- Explore ROS topics with
ros2 topic listandros2 topic echo /topic