How To Run GUI application inside a Docker container

Docker logo

Intro

They recently installed a Rocket.Chat server at my office which is a chat software solution. As I didn't want to install it directly on my computer and also never tried Docker I decided to put it in a container.

Configuration

  • OS : Debian 9
  • Docker : 18.09.5

Docker GUI Application

Installation

  • Install necessary packages to add Docker repository
root@host:~# apt install apt-transport-https ca-certificates curl software-properties-common gnupg2
  • Import Docker gpg key
root@host:~# curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
  • Add Docker repository
root@host:~# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
  • Refresh packages list
root@host:~# apt update
  • Install Docker
root@host:~# apt install docker-ce
  • Check Docker service status
root@host:~# systemctl status docker
  • Permit user to run Docker commands (then restart current session to make it works)
root@host:~# usermod -aG docker user

Debian Image Installation

  • Search Debian images
user@debian: $ docker search debian
  • Download Debian images
user@debian: $ docker image pull debian
  • /home/user/Dockerfile
FROM debian:9

RUN apt-get update && apt-get dist-upgrade -y && apt-get install -y wget gconf2 gconf-service libnotify4 libappindicator1 libxtst6 libnss3 libxss1 libasound2 sudo
RUN wget https://github.com/RocketChat/Rocket.Chat.Electron/releases/download/2.15.1/rocketchat_2.15.1_amd64.deb
RUN dpkg -i rocketchat_2.15.1_amd64.deb

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


USER user
ENV HOME /home/user
CMD rocketchat-desktop
  • Build rocket image from Dockerfile
user@debian: $ docker build -t rocket .

Launch GUI Application

user@debian: $ docker run -ti --rm -e DISPLAY  -v $HOME/.Xauthority:/home/user/.Xauthority --net=host rocket

Lauch Two Applications

Let's see what to do if we want to launch firefox and rocket.chat applications in the same container.

Applications Script

  • First we create a /home/user/applications.sh file
#! /bin/bash
firefox-esr&
rocketchat-desktop
  • We make it executable
chmod +x /home/user/applications.sh

Debian Image Installation

  • Then we modify /home/user/Dockerfile configuration file to
FROM debian:9

RUN apt-get update && apt-get dist-upgrade -y && apt-get install -y wget gconf2 gconf-service libnotify4 libappindicator1 libxtst6 libnss3 libxss1 libasound2 sudo firefox-esr
RUN wget https://github.com/RocketChat/Rocket.Chat.Electron/releases/download/2.15.1/rocketchat_2.15.1_amd64.deb
RUN dpkg -i rocketchat_2.15.1_amd64.deb

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


USER user
ENV HOME /home/user
COPY ./applications.sh /home/user/
CMD /home/user/applications.sh
  • Build fire-rocket image from Dockerfile
user@debian: $ docker build -t fire-rocket .
  • Launch GUI applications
user@debian: $ docker run -ti --rm -e DISPLAY  -v $HOME/.Xauthority:/home/user/.Xauthority --net=host fire-rocket

References

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :