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

How to build using Colcon inside a Dockerfile

asked 2021-03-14 19:36:26 -0500

jandaa gravatar image

updated 2021-03-16 09:55:27 -0500

I'm trying to use docker to build and run a ROS2 node, however in order for Colcon to build the node as an instruction in the Dockerfile, it must first have ROS2 sourced. It seems though that you cannot source a script in a dockerfile instruction, so is there a way to build a Colcon workspace as part of a dockerfile?

Essentially what I'm trying to do assuming a colcon workspace has been copied to the image is:

RUN /bin/bash -c "source /opt/ros/eloquent/" && colcon build

edit retag flag offensive close merge delete



I suggest you add the "Update" as an answer and accept it instead of updating the question.

jeremya gravatar image jeremya  ( 2021-03-16 08:10:25 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2021-03-17 17:07:25 -0500

ruffsl gravatar image

Instead of using the source in bash, you can use the dot operator instead, as it's supported in dash (the default shell in ubuntu based images)

For example:

RUN . /opt/ros/$ROS_DISTRO/ && \
    colcon build

You can also change the default shell in the Dockerfile:

but I'd suggest keeping it simple with the dot operator as above.

edit flag offensive delete link more


Thanks that works as well!

jandaa gravatar image jandaa  ( 2021-03-17 19:27:20 -0500 )edit

answered 2021-03-15 16:50:29 -0500

jeremya gravatar image

Instead of

RUN /bin/bash -c "source /opt/ros/eloquent/" && colcon build

try moving the double quote:

RUN /bin/bash -c "source /opt/ros/eloquent/ && colcon build"

edit flag offensive delete link more


Ah you're right thank you!

jandaa gravatar image jandaa  ( 2021-03-16 09:54:58 -0500 )edit

answered 2021-03-16 09:55:57 -0500

jandaa gravatar image

I found you can also write a script and execute it in the instruction. For example:

source /opt/ros/"${ROS_DISTRO}"/setup.bash
colcon build

and inside the docker file RUN

edit flag offensive delete link more

Question Tools



Asked: 2021-03-14 19:36:26 -0500

Seen: 2,213 times

Last updated: Mar 17 '21