Docker image with OpenCV with X11 forwarding for GUI

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 \
   -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

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

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

# 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!


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

Leave a Reply

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

You are commenting using your 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