For different reasons it may be useful to run dockerized GUI apps. For me, the reason was: stress testing a web app’s javascript logic by keeping 200+ browser tabs open for a long period of time.
I prefer to keep useful tools in a place I can directly find and use them.
Create a new shell script: /usr/local/bin/docker-gui
and grant all execute permissions:
echo 'xhost +si:localuser:root\ndocker run -it --rm -e DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --tmpfs /dev/shm "${@}"' | sudo tee /usr/local/bin/docker-gui
sudo chmod a+x /usr/local/bin/docker-gui
Now test your new script by running:
docker-gui jessfrez/firefox
You should see a new firefox window pop up:
This is a very basic example of running a GUI app, using X.
I encourage you to look at this nice collection of commonly used apps containerized, by jessfraz.
For my web app testing, this was not sufficient.
I wanted to run many containers, each running a web browser, hosted on a large kubernetes cluster. But our docker-gui
passes a DISPLAY
environment variable, which is used to send keyboard, mouse, and screen information to the server program (X server). Our kubernetes cluster will not have a display screen to render the browser, so this approach did not work out for me.
I discovered this interesting docker image, containing an operating system with full-fledged UI that is served at localhost:6080, and has firefox installed by default.
docker run -d --rm --name gui \
-p 6080:80 -p 5900:5900 \
dorowu/ubuntu-desktop-lxde-vnc:bionic
You can either choose to open or completely ignore the ui, and firefox will still run and render web pages in the background:
With docker it’s possible to make and save changes that have been made to a container to a new layer, as a docker image, by using using docker commit
command:
docker commit # [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
I want to run firefox, opening a specific webpage, whenever the container starts. By attaching vscode to the running container (using the docker extension for vscode), you can modify files inside the container:
The supervisord.conf
file determines which programs run at startup, so I added firefox, with my desired webpage url.
Finally, I committed the change, and tagged a new docker image, which I can now run on my kubernetes cluster to open as many browser tabs with my testing website as I want.
I still feel that there’s a huge overhead to this approach, so if you find a better solution to this problem, please let me know in the comments below!