Lei Mao bio photo

Lei Mao

Machine Learning, Artificial Intelligence, Computer Science.

Twitter Facebook LinkedIn GitHub   G. Scholar E-Mail RSS

Introduction

Docker is typically used for containerized application and development. However, people hardly used it for applications that have GUIs.


It turns out that Docker is fully capable of running GUI applications. In this blog post, I would like to discuss how to run GUI applications from Docker quickly using two simple examples.

Examples

The key to run GUI applications from Docker image is to set -e DISPLAY=$DISPLAY and -v /tmp/.X11-unix:/tmp/.X11-unix for Docker container. Also make sure that the X clients can be connected from any host, including our Docker container, by running xhost +.


I have prepared the Docker containers and running instructions for two GUI applications, Firefox and Chrome.

Firefox

It would be desired if canberra-gtk libraries are installed in the Docker container. Otherwise warnings might occur. The Dockerfile for Firefox is as follows.

# firefox.Dockerfile
FROM ubuntu:20.04

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update
RUN apt-get -y install \
    firefox \
    libcanberra-gtk-module \
    libcanberra-gtk3-module
RUN apt-get clean

CMD ["firefox"]

To build the Docker image, please run the following command.

$ docker build -f firefox.Dockerfile -t firefox:0.0.1 .

To start Firefox from the Docker container, please run the following command..

$ xhost +
$ docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix firefox:0.0.1
$ xhost -

Chrome

The Dockerfile for Chrome is similar to the one for Firefox. Note that to run Chrome from Docker container, we have to use a couple of additional arguments, such as --no-sandbox, --disable-dev-shm-usage, and --disable-gpu.

# chrome.Dockerfile
FROM ubuntu:20.04

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update
RUN apt-get install -y \
    wget \
    libcanberra-gtk-module \
    libcanberra-gtk3-module
RUN apt-get clean

RUN cd /tmp && \
    wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb && \
    apt-get install -y ./google-chrome-stable_current_amd64.deb

CMD ["google-chrome", "--no-sandbox", "--disable-dev-shm-usage", "--disable-gpu"]

To build the Docker image, please run the following command.

$ docker build -f chrome.Dockerfile -t chrome:0.0.1 .

To start Chrome from the Docker container, please run the following command..

$ xhost +
$ docker run -it --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix chrome:0.0.1
$ xhost -

Display Size

Sometimes, the display size from Docker container is not ideal. We could set the display size using environment variables DISPLAY_WIDTH and DISPLAY_HEIGHT.

$ docker run -it --rm -e DISPLAY=$DISPLAY -e DISPLAY_WIDTH=3840 -e DISPLAY_HEIGHT=2160 -v /tmp/.X11-unix:/tmp/.X11-unix firefox:0.0.1

References