I would like to run firefox (or any graphical application) inside docker container.
My requirement: When I start the container, I should be creating a virtual display, starting VNC server and then the GUI application. This means after the container is successfully started, I can connect to the GUI application running inside the container via VNC client. When I close the application, the container should automatically stop.
Attempt 1: I started with the example here https://hub.docker.com/r/devopsil/vnc-firefox/~/dockerfile/ In this example the developer starts the vnc server via CMD and keeps it running forever. He puts the firefox as part of .rc file so it starts when the container starts. If you connect via VNC client, you can see firefox and as expected closing firefox or disconnecting the VNC session does not stop the container.
Attempt 2: I tuned the Dockerfile as follows. Changed only the last line (replaced CMD with ENTRYPOINT and removed the “forever” flag”)
ENTRYPOINT ["/usr/bin/x11vnc", "-usepw", "-create"]
Rebuilding and running the container “docker run --rm -p 5900 ” had a different behaviour now: container starts, connection via VNC client worked, firefox opened after a few seconds (wait after the message "extension RANDR missing on display :20") and the container stopped automatically after closing the vnc connection.
Attempt 3: Then I created a bash script to start the vncserver and firefox and changed the Dockerfile to copy this script file inside the image and made the script as the entry point. But I got the error similar to “Display variable not set”
#!/bin/bash
/usr/bin/x11vnc -forever -usepw -create &
firefox
Attempt 4: So I passed the DISPLAY variable via the docker run command but then the error is cannot open display.
Question: I don’t know how to like the virtual display created by the vncserver flag “-create” to the firefox. Please let me know what would be best way to start the GUI application when the container starts and automatically stop the container when the GUI application is closed. I believe the ENTRYPOINT in dockerfile should be set to firefox. Thanks for your help.
I have seen this https://blog.jessfraz.com/post/docker-containers-on-the-desktop/ but this is of no use to me as I want to access the application remotely via VNC.
You can also use it to run graphical programs though! You can either use an existing X Server, where the host machine is already running a graphical environment, or you can run a VNC server within the container. First it’s important to understand what Docker actually does.
Performing similar process, you can run almost any GUI application inside Docker Containers and display and interact with them on your screen. Running GUI Apps in a Docker Container is really an awesome experience which will never harm/use your host Filesystem.
Traditional Linux containers use an init system that can manage multiple processes. This means entire applications can run as one. The Docker technology encourages applications to be broken down into their separate processes and provides the tools to do that. This granular approach has its advantages.
Displaying a GUI-based application in Docker is typically a moment of stackoverflow search, for the sake of time, and for the reckless lazy ones, the easiest solution is to give xhost permission and then remove the permission. We can create a Docker Container using Dockerfile.
I managed to found the solution:
Changed the script in Attempt 3 above as follows worked
!/bin/bash
Xvfb :1 -screen 0 800x600x16 &
/usr/bin/x11vnc -display :1.0 -usepw &
DISPLAY=:1.0
export DISPLAY
firefox
Cheers.
I'm using the following Bash function:
# Configure virtual display and wine.
# Usage: set_display
set_display() {
export DISPLAY=${DISPLAY:-:0} # Select screen 0 by default.
xdpyinfo &>/dev/null && return
if command -v x11vnc &>/dev/null; then
! pgrep -a x11vnc && x11vnc -bg -forever -nopw -quiet -display WAIT$DISPLAY &
fi
! pgrep -a Xvfb && Xvfb $DISPLAY -screen 0 1024x768x16 &
sleep 1
if command -v fluxbox &>/dev/null; then
! pgrep -a fluxbox && fluxbox 2>/dev/null &
fi
echo "IP: $(hostname -I) ($(hostname))"
}
Then source
the file and call set_display
. Consider configuring the password via -usepw
.
I'm using it in the following Docker project (check .funcs.cmds.inc.sh
).
Check also: How to make Xvfb display visible?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With