Dockerize Lucas Chess : Running a GUI application inside a docker container

Few years ago I found a nice open source chess game named Lucas Chess. The program is written in Python but unfortunately the game was targeted to the Windows Platform. Since then the community around Lucas Chess has done many steps towards supporting Linux platforms. Right now the git repo provides instructions on how LucasChess can be installed on the Linux platform ( specifically on a Debian based platform i.e. Debian/Ubuntu/Mint etc ) .

Nevertheless, this issue reveals that many attempts to install LucasChess on Linux had no results. The issue made me wonder if we can create a docker container for serving GUI purposes. Furthermore I saw the challenge to provide the installation details with the infrastructure as code approach. To take it one step further I was wondering whether a local development environment can be provided by a docker container. For example the java development environment image contains your favorite IDE, your preferred jdk etc. On the other hand the Angular development environment is going to bundle with the respective tools. It seems that Docker has not only  revolutionized the DevOps landscape but also the development environment landscape.

The simple idea for running a GUI application inside a docker container is sharing the host’s X11 socket. The socket directories described here .

Based on the above idea we need to pass the parameters bellow in order to do the trick. Those params are :

-v /tmp/.X11-unix:/tmp/.X11-unix

This makes the unix domain socket available in the container

-e DISPLAY=unix$DISPLAY

This environmental variable tells the container which display to use (using the unix domain socket).

The container must belong to the host network .

The Dockerfile for the lucas chess base image :


FROM ubuntu:18.04

RUN apt-get update && apt-get install -y git sudo apt-utils

RUN export uid=1000 gid=1000 && \
mkdir -p /home/developer && \
echo "developer:x:${uid}:${gid}:Developer,,,:/home/developer:/bin/bash" >> /etc/passwd && \
echo "developer:x:${uid}:" >> /etc/group && \
echo "developer ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/developer && \
chmod 0440 /etc/sudoers.d/developer && \
chown ${uid}:${gid} -R /home/developer

RUN	apt-get install -y python-pip \
python-dev \
python-qt4 \
python-pyaudio

RUN pip install setuptools && \
pip install psutil && \
pip install chardet && \
pip install 'python-chess<0.24' && \
pip install Pillow && \
pip install PhotoHash && \
pip install Cython && \
pip install scandir

USER developer
ENV HOME /home/developer

RUN cd ${HOME} && git clone https://github.com/lukasmonk/lucaschess.git lucaschess

WORKDIR /home/developer/lucaschess

RUN chmod -R 755 *
RUN chmod -R +x *

RUN sed -i 's/LCEngine2.so/LCEngine4.so/' LCEngine/xcython_linux.sh

RUN cd LCEngine/irina && ./xmk_linux.sh

RUN cd LCEngine && ./xcython_linux.sh

ENV LD_LIBRARY_PATH /home/developer/lucaschess/Engines/Linux64/_tools

RUN chmod +x -R Engines/Linux64/

USER root
RUN apt-get install -y stockfish
User developer

RUN ln -s /usr/games/stockfish ${HOME}/stockfish.exe

ENTRYPOINT ["python2.7", "Lucas.py"]

The usual command in order to build the image


docker build -t lucaschess

The command to start the container


docker run --name lucaschess --network host -it --rm -e DISPLAY=$DISPLAY -e QT_X11_NO_MITSHM=1 -v /tmp/.X11-unix:/tmp/.X11-unix lucaschess

The command above declares that the environmental variable QT_X11_NO_MITSHM has the value 1.
If we omit this environmental variable we are going to get the following error


X Error: BadShmSeg (invalid shared segment parameter) 128
Extension:    130 (MIT-SHM)
Minor opcode: 3 (X_ShmPutImage)
Resource id:  0x2c00012

A blank window is going to pop up. The MIT-SHM is a X Window System extension for exchange of image data between client and server using shared memory.
The “QT_X11_NO_MITSHM” – stops Qt from using the MIT-SHM X11 extension. ( maybe it would be wiser to define this parameter in the dockerfile ).

Above you can see the screenshots. The board created in the container but displayed in the host.

This link contains an example also on how you can create an Java Development Environment with a docker image containing Netbeans and Openjdk.