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:
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.
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:
# General dependencies
RUN sudo apt-get update && apt-get install -y \
# Eigen and OpenCV
RUN apt-get install -y \
RUN mkdir /sample
COPY main.cpp CMakeLists.txt /sample/
RUN cd /sample && \
mkdir build && \
cd build && \
cmake .. && \
COPY docker.png /sample/
If you run that, you should be able to see the following image: