Docker Container GUI Display

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
1
2
3
4
5
6
7
8
9
10
11
12
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.

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

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

1
2
3
$ 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.

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

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

1
2
3
$ 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.

1
$ 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

Author

Lei Mao

Posted on

12-24-2020

Updated on

12-24-2020

Licensed under


Comments