ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Call ROS inside a Docker container

asked 2020-01-21 05:13:00 -0500

rob gravatar image

updated 2020-01-21 11:04:37 -0500

Hi,

I have a docker container running with a python program. I want to publish a string to a ROS topic when the program is called. It looks something like this

#!/usr/bin/env python

import rospy from std_msgs.msg import
String

def handler():

    rospy.init_node("tts_publisher", anonymous=True)
    pub = rospy.Publisher('string_tts', String,
    queue_size=10)

    tts_text = "Program called"
    msg_to_publish = String()
    msg_to_publish.data = tts_text

    rospy.sleep(3)
    pub.publish(msg_to_publish)

ROS is installed on my (host) machine, so the Docker doesn't now ROS and can't publish from within the program. The program needs to be in this Docker container, since it gets called from another docker container.

How can this be done conceputally?

Thank you very much for any help!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2020-01-21 06:20:45 -0500

lucascoelho gravatar image

updated 2020-01-21 06:21:11 -0500

Use a ROS docker image instead https://hub.docker.com/_/ros It has ROS installed and will run your script, and it is available on docker hub.

If you want the docker image to publish to the host ROS master, then you will have to setup your network, see this page: http://wiki.ros.org/ROS/NetworkSetup

edit flag offensive delete link more

Comments

Thank you very much! But the program gets called from another docker image, so in your scenario I would have two different docker images, right?

rob gravatar image rob  ( 2020-01-21 08:11:11 -0500 )edit
1

Sorry being boring, but you should have said: "gets called from another docker container". Images are static, containers are the ones that actually run stuff https://stackoverflow.com/a/23736802/...

Since it's called from another docker image, yes, you will need two docker containers. But if they are actually the same image... it depends on your setup. Does your first container need to run ROS? If yes, then it's one docker image and two docker containers. If not, you can use two different images, and two containers.

lucascoelho gravatar image lucascoelho  ( 2020-01-21 10:44:38 -0500 )edit

No, you're absolutley righ! I edited my question acordingly, thank you! My first container doesn't need to run ROS, but my concern was if I have ROS in another container I can't work with ROS from within the first container. So if I get it right my setup is two different images and two containers. But how can I make the two containers communication in order to call ros (container 2) from my program (container 1)? Thank you so much for your time

rob gravatar image rob  ( 2020-01-22 02:38:19 -0500 )edit
1

Well, after installing docker, you might have seen a new network interface in your computer called docker0 (if not, run ifconfig in your computer and you will see). This is a virtual network created by docker to connect containers and the host. By default, each container created will receive an IP on that network. So, one way to solve the problem in the way you have described would be to ssh into the ROS container with that IP and then launch the ROS nodes you need. I’ve found this link if you want to know more about docker0 https://developer.ibm.com/recipes/tut...

lucascoelho gravatar image lucascoelho  ( 2020-01-22 02:49:04 -0500 )edit

Question Tools

Stats

Asked: 2020-01-21 04:50:58 -0500

Seen: 1,051 times

Last updated: Jan 21 '20