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

Call ROS inside a Docker container

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

rob gravatar image

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


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

def handler():

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

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


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

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

lucascoelho gravatar image

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

Use a ROS docker image instead 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:

edit flag offensive delete link more


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 -0600 )edit

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

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 -0600 )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 -0600 )edit

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

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

Question Tools


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

Seen: 1,002 times

Last updated: Jan 21 '20