Dcoker + ROS

Using ROS images

docker pull ros:noetic-robot

Running ROS containers

docker run -it ros

In a new terminal on the host machine, find the name of your new container, last container started using:

docker ps -l

Using the name of the container as the ID, in writing this tutorial docker happened to assign the string "nostalgic_morse", we can start additional bash session in the same container by running:

docker exec -it nostalgic_morse bash

Once inside, we'll need to setup our environment. The best way to do this is to using the entrypoint script included in the docker image:

source ros_entrypoint.sh

ROS + Docker Network

Using GUI's with Docker

First create a directory with a Dockerfile and entrypoint script inside.

$ mkdir my_noetic_image & cd my_melodic_image
$ touch Dockerfile

Paste the following content into the Dockerfile.

FROM osrf/ros:noeitc-desktop-full

# nvidia-container-runtime
ENV NVIDIA_VISIBLE_DEVICES \
    ${NVIDIA_VISIBLE_DEVICES:-all}
ENV NVIDIA_DRIVER_CAPABILITIES \
    ${NVIDIA_DRIVER_CAPABILITIES:+$NVIDIA_DRIVER_CAPABILITIES,}graphics

Build the image. Don't forget the period at the end of that command.

$ cd my_noetic_image/
$ docker build -t my_noetic_image .

Now create a script to run the image called run_my_image.bash

XAUTH=/tmp/.docker.xauth
if [ ! -f $XAUTH ]
then
    xauth_list=$(xauth nlist :0 | sed -e 's/^..../ffff/')
    if [ ! -z "$xauth_list" ]
    then
        echo $xauth_list | xauth -f $XAUTH nmerge -
    else
        touch $XAUTH
    fi
    chmod a+r $XAUTH
fi

docker run -it \
    --env="DISPLAY=$DISPLAY" \
    --env="QT_X11_NO_MITSHM=1" \
    --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
    --env="XAUTHORITY=$XAUTH" \
    --volume="$XAUTH:$XAUTH" \
    --runtime=nvidia \
    my_noeitc_image \
    bash

Make the script executable

$ chmod a+x run_my_image.bash

Execute the script

$ ./run_my_image.bash

Then inside the container launch RViz

$ roscore > /dev/null & rosrun rviz rviz

Using GUI's with Docker

Using X server

X server is a windowing system for bitmap displays, common on linux operating systems. There are several ways one can connect a container to a host's X server for display. A brief description and tradeoffs for each method below:

  • The first listed is simple, but unsecure

  • The second is safer, but non-isolated

  • The third is isolated, but not as portable

  • The fourth is isolated, works remotely, but is slow.

The simple way

docker run -it \
    --env="DISPLAY" \
    --env="QT_X11_NO_MITSHM=1" \
    --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
    osrf/ros:indigo-desktop-full \
    rqt
export containerId=$(docker ps -l -q)

Troubleshoot

Authorization required, but no authorization protocol specified.

xhost +

Build without cache

docker build --no-cache -t my_noetic_image .

Copy File From Container To Host

The docker cp command allows you to copy files and directories from a container's file system to your local machine, whether the container is running or stopped.

docker cp container:src_path dest_path 

Where:

  • src_path is the path on the container of the file you want to copy.

  • container is the name or the ID of the container you want to copy files from.

  • dest_path is the path on your local machine of the directory you want to copy files to.

To get the name or ID of the container you want to copy files from, you can use the docker ps -a command, which will output the list of all the running and stopped containers.

Last updated