Skip to content

Commit

Permalink
#888 Fix IP address for Docker (#889)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
adamlm authored Jan 29, 2024
1 parent 399a0eb commit 1812230
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions webots_ros2_driver/scripts/webots_tcp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@

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

from pathlib import Path
import os
import socket
import subprocess
import sys
import time


def is_docker():
mountinfo = Path("/proc/self/mountinfo")
return mountinfo.is_file() and "docker" in mountinfo.read_text()


def get_host_ip():
if is_docker():
return "host.docker.internal"

try:
output = subprocess.run(['ip', 'route'], check=True, stdout=subprocess.PIPE, universal_newlines=True)
for line in output.stdout.split('\n'):
Expand Down
8 changes: 8 additions & 0 deletions webots_ros2_driver/webots_ros2_driver/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ def container_shared_folder():
return shared_folder_list[1]


def is_docker():
mountinfo = Path("/proc/self/mountinfo")
return mountinfo.is_file() and "docker" in mountinfo.read_text()


def get_host_ip():
if is_docker():
return "host.docker.internal"

try:
output = subprocess.run(['ip', 'route'], check=True, stdout=subprocess.PIPE, universal_newlines=True)
for line in output.stdout.split('\n'):
Expand Down

0 comments on commit 1812230

Please sign in to comment.