Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions garden/ros2_gz_docker_bridge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# ROS2 + Gazebo Docker Bridge
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "ROS 2" (with a space), here and elsewhere.


A two-container Docker setup for running Gazebo and ROS2 as separate services connected over a custom Docker network, with the ros_gz_bridge handling topic communication between them

---

## Compatibility

| ROS2 | Gazebo |
|---|---|
| Rolling | Jetty |
| Kilted | Ionic |
| Jazzy | Harmonic |
| Iron | Harmonic |
| Humble | Fortress |

---

## Prerequisites

- Docker installed and running
- Both images built:
- `gazebo` — from `dockerfile.gazebo`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is dockerfile.gazebo supposed to be a file included in this tutorial?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes docker image for gazebo i have specified as docker.gazebo same for ros as docker.ros

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ros2 below says "from your ROS2 Dockerfile", which is fine, but putting dockerfile.gazebo makes it look like the tutorial should provide that file, but I don't see that in this PR.

- `ros2` — from your ROS2 Dockerfile
- `ros-gz-bridge` installed

---

## Setup

### 1. Create the Docker Network

```bash
docker network create ros-link
```

### 2. Start Containers

```bash
docker run -it -d --name gazebo-container --network ros-link gazebo sleep infinity
docker run -it -d --name ros2-container --network ros-link ros2 sleep infinity
```

### 3. Check Container IPs


```bash
docker network inspect ros-link
```

Example output:
```json
[
{
"Name": "ros-link",
"Driver": "bridge",
"IPAM": {
"Config": [{ "Subnet": "172.21.0.0/16", "Gateway": "172.21.0.1" }]
},
"Containers": {
"281b3cbfba6a...": {
"Name": "ros2-container",
"IPv4Address": "172.21.0.3/16" //<ros-ip>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subnet mask (/16) would need to be removed before setting GZ_IP. Can you add a statement or example about that?

},
"c2410d610249...": {
"Name": "gazebo-container",
"IPv4Address": "172.21.0.2/16" //<gz-ip>
}
}
}
]
```

---

### 4. Configure Environment Variables

**In `gazebo-container`:**
```bash
export GZ_IP=<gz-ip>
export GZ_PARTITION=gazebo-container
```

**In `ros2-container`:**
```bash
export GZ_IP=<ros-ip>
export GZ_PARTITION=gazebo-container
source /opt/ros/<ros-distro>/setup.bash
```

> Add these to `~/.bashrc` in each container to be available across sessions.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you emphasize this more since the commands below won't work without it? You can use an admonition like so

Suggested change
> Add these to `~/.bashrc` in each container to be available across sessions.
:::{important}
Add these to `~/.bashrc` in each container to be available across sessions.
:::

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok ill make the changes


---

### 5. Install the Bridge

```bash
apt-get update && apt-get install -y ros-<ros-distro>-ros-gz-bridge
```

---

## Using a Config File (optional)

if you have a `bridge.yaml` config file, you can use it instead of passing topics as arguments:

```bash
ros2 run ros_gz_bridge parameter_bridge --ros-args -p config_file:=/path/to/bridge.yaml
```

a typical `bridge.yaml` looks like this:

```yaml
- ros_topic_name: /clock
gz_topic_name: /clock
ros_type_name: rosgraph_msgs/msg/Clock
gz_type_name: gz.msgs.Clock
direction: GZ_TO_ROS
```

---

## Running the Bridge

### Terminal 1 — Start Gazebo sim

```bash
docker exec -it gazebo-container bash
gz sim shapes.sdf
```

### Terminal 2 — Start the bridge

```bash
docker exec -it ros2-container bash
source /opt/ros/<ros-distro>/setup.bash
ros2 run ros_gz_bridge parameter_bridge \
/clock@rosgraph_msgs/msg/Clock@gz.msgs.Clock
```
Comment on lines +138 to +147
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should clarify that this step is not needed if using the yaml method mentioned above.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok


### Terminal 3 — Verify

```bash
docker exec -it ros2-container bash
source /opt/ros/<ros-distro>/setup.bash
ros2 topic echo /clock
```
if you see something like this :-
```bash
clock:
sec: 0
nanosec: 0
---
clock:
sec: 0
nanosec: 0
---
clock:
sec: 0
nanosec: 0
---
```
then the connection between the two docker containers is succesfull

---

## Final Network Info

| Container | IP | Role |
|---|---|---|
| `gazebo-container` | `<gz-ip>` | Runs Gazebo simulation |
| `ros2-container` | `<ros-ip>` | Runs ROS2 + bridge |

Network name: `ros-link`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary

1 change: 1 addition & 0 deletions garden/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ These tutorials cover general concepts to help get you started with Gazebo.
* [ROS 2 Integration](ros2_integration)
* [ROS 2 Interoperability](ros2_interop)
* [ROS 2 Integration Template](ros_gz_project_template_guide)
* [ROS 2 Docker Bridge Setup](ros2_gz_docker_bridge)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is specifically addressing cross-container communuication, the title should indicate that.


## Per-library tutorials

Expand Down
174 changes: 174 additions & 0 deletions harmonic/ros2_gz_docker_bridge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# ROS2 + Gazebo Docker Bridge

A two-container Docker setup for running Gazebo and ROS2 as separate services connected over a custom Docker network, with the ros_gz_bridge handling topic communication between them

---

## Compatibility

| ROS2 | Gazebo |
|---|---|
| Rolling | Jetty |
| Kilted | Ionic |
| Jazzy | Harmonic |
| Iron | Harmonic |
| Humble | Fortress |

---

## Prerequisites

- Docker installed and running
- Both images built:
- `gazebo` — from `dockerfile.gazebo`
- `ros2` — from your ROS2 Dockerfile
- `ros-gz-bridge` installed

---

## Setup

### 1. Create the Docker Network

```bash
docker network create ros-link
```

### 2. Start Containers

```bash
docker run -it -d --name gazebo-container --network ros-link gazebo sleep infinity
docker run -it -d --name ros2-container --network ros-link ros2 sleep infinity
```

### 3. Check Container IPs


```bash
docker network inspect ros-link
```

Example output:
```json
[
{
"Name": "ros-link",
"Driver": "bridge",
"IPAM": {
"Config": [{ "Subnet": "172.21.0.0/16", "Gateway": "172.21.0.1" }]
},
"Containers": {
"281b3cbfba6a...": {
"Name": "ros2-container",
"IPv4Address": "172.21.0.3/16" //<ros-ip>
},
"c2410d610249...": {
"Name": "gazebo-container",
"IPv4Address": "172.21.0.2/16" //<gz-ip>
}
}
}
]
```

---

### 4. Configure Environment Variables

**In `gazebo-container`:**
```bash
export GZ_IP=<gz-ip>
export GZ_PARTITION=gazebo-container
```

**In `ros2-container`:**
```bash
export GZ_IP=<ros-ip>
export GZ_PARTITION=gazebo-container
source /opt/ros/<ros-distro>/setup.bash
```

> Add these to `~/.bashrc` in each container to be available across sessions.

---

### 5. Install the Bridge

```bash
apt-get update && apt-get install -y ros-<ros-distro>-ros-gz-bridge
```

---

## Using a Config File (optional)

if you have a `bridge.yaml` config file, you can use it instead of passing topics as arguments:

```bash
ros2 run ros_gz_bridge parameter_bridge --ros-args -p config_file:=/path/to/bridge.yaml
```

a typical `bridge.yaml` looks like this:

```yaml
- ros_topic_name: /clock
gz_topic_name: /clock
ros_type_name: rosgraph_msgs/msg/Clock
gz_type_name: gz.msgs.Clock
direction: GZ_TO_ROS
```

---

## Running the Bridge

### Terminal 1 — Start Gazebo sim

```bash
docker exec -it gazebo-container bash
gz sim shapes.sdf
```

### Terminal 2 — Start the bridge

```bash
docker exec -it ros2-container bash
source /opt/ros/<ros-distro>/setup.bash
ros2 run ros_gz_bridge parameter_bridge \
/clock@rosgraph_msgs/msg/Clock@gz.msgs.Clock
```

### Terminal 3 — Verify

```bash
docker exec -it ros2-container bash
source /opt/ros/<ros-distro>/setup.bash
ros2 topic echo /clock
```
if you see something like this :-
```bash
clock:
sec: 0
nanosec: 0
---
clock:
sec: 0
nanosec: 0
---
clock:
sec: 0
nanosec: 0
---
```
then the connection between the two docker containers is succesfull

---

## Final Network Info

| Container | IP | Role |
|---|---|---|
| `gazebo-container` | `<gz-ip>` | Runs Gazebo simulation |
| `ros2-container` | `<ros-ip>` | Runs ROS2 + bridge |

Network name: `ros-link`
2 changes: 1 addition & 1 deletion harmonic/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ These tutorials cover general concepts to help get you started with Gazebo.
* [ROS 2 Integration via Bridge](ros2_integration)
* [ROS 2 Interoperability](ros2_interop)
* [ROS 2 Integration Template](ros_gz_project_template_guide)

* [ROS 2 Docker Bridge Setup](ros_docker_gz_integration)
## Per-library tutorials

See the *API & Tutorials* sections on the [Libraries page](/libs){.external} page for more specific content correlating to each Gazebo library.
Expand Down
Loading
Loading