Docker image with OpenCV with X11 forwarding for GUI

Hi!
Docker is very cool. You can use it to build applications in isolated environments (e.g. some Ubuntu distro you like, with some specific OpenCV version installed in it), pack the entire OS and app into an “image”, and ship it to a repository, where others can download it, and “deploy” the image into a container.

But, by default, when you run a container, you are doing it via a terminal command (e.g. run docker –rm -ti some_image_name /bin/bash), and as a result, your image don’t have a way to show GUI apps, unless you do something else.

In my case, I sometimes want to see that my code is running, for instance, some OpenCV functions, so I want to see the displayed GUI.

There is a lot of discussion about this topic, but the most simple way to do it is  to not touch the Dockerfile, but, instead, do the trick when running the container:

xhost +
sudo docker run --rm -ti --net=host --ipc=host \
   -e DISPLAY=$DISPLAY \
   -v /tmp/.X11-unix:/tmp/.X11-unix \
   opencv_gui ./sample/build/bin/opencvSample ./sample/docker.png

There is a lot of stuff going on there. Basically, the xhost + allows everybody to use your host x server; while the DISPLAY and X11 commands specify the Docker container to share the X11 socket and host display.
The opencv_gui is the name of a sample Docker image which runs the opencvSample program. You can get it from my public repository at Docker Hub:

docker pull mnieto/opencv_gui

You will see that the Dockerfile is pretty simple and without any specific X11 command:

FROM ubuntu:14.04
MAINTAINER mnieto

# General dependencies
RUN sudo apt-get update && apt-get install -y \	
	build-essential \
	cmake \
	git	

# Eigen and OpenCV
RUN apt-get install -y \
	libopencv-dev

# Project
RUN mkdir /sample
COPY main.cpp CMakeLists.txt /sample/
RUN 	cd /sample && \
	mkdir build && \
	cd build && \
	cmake .. && \
	make -j

COPY docker.png /sample/

If you run that, you should be able to see the following image:

Good luck!
Regards,

Marcos

Advertisements
This entry was posted in Computer vision, OpenCV and tagged , , , . Bookmark the permalink.

2 Responses to Docker image with OpenCV with X11 forwarding for GUI

  1. Harel says:

    Thank you for this explanation.
    As a docker newbie, will your image run on docker for Windows? Is there a way to do it?

    • Hi!
      I haven’t tried this image in Windows, but there is a Docker engine for Windows, and it works. But, yes, Docker is not really intended for Windows, as the Docker engine first makes use of a virtual layer provided by a Hypervisor (such as VMWare), so this is kind of cheating Docker to think it is under a Linux environment.
      Something I don’t know is if the nvidia-docker works well in Windows. We have experienced problems using GPUs with Linux and virtualization layers, and that probably will happen with the Windows Docker.
      Regards,
      Marcos

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s