Skip to content

Commit 1812230

Browse files
authored
#888 Fix IP address for Docker (#889)
When this package's nodes run in a Docker container, the get_host_ip function can return the wrong IP address, causing routing failures. Now the function checks if the process is running in a Docker container, returning the special host.docker.internal DNS name. Signed-off-by: Adam Morrissett <[email protected]>
1 parent 399a0eb commit 1812230

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

webots_ros2_driver/scripts/webots_tcp_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@
1616

1717
"""TCP client to start Webots on the host."""
1818

19+
from pathlib import Path
1920
import os
2021
import socket
2122
import subprocess
2223
import sys
2324
import time
2425

2526

27+
def is_docker():
28+
mountinfo = Path("/proc/self/mountinfo")
29+
return mountinfo.is_file() and "docker" in mountinfo.read_text()
30+
31+
2632
def get_host_ip():
33+
if is_docker():
34+
return "host.docker.internal"
35+
2736
try:
2837
output = subprocess.run(['ip', 'route'], check=True, stdout=subprocess.PIPE, universal_newlines=True)
2938
for line in output.stdout.split('\n'):

webots_ros2_driver/webots_ros2_driver/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,15 @@ def container_shared_folder():
123123
return shared_folder_list[1]
124124

125125

126+
def is_docker():
127+
mountinfo = Path("/proc/self/mountinfo")
128+
return mountinfo.is_file() and "docker" in mountinfo.read_text()
129+
130+
126131
def get_host_ip():
132+
if is_docker():
133+
return "host.docker.internal"
134+
127135
try:
128136
output = subprocess.run(['ip', 'route'], check=True, stdout=subprocess.PIPE, universal_newlines=True)
129137
for line in output.stdout.split('\n'):

0 commit comments

Comments
 (0)