Hi everyone, today we'll learn how we can run GUI Applications inside a Docker Container. We can easily run most of the common GUI apps without getting into trouble inside a Docker Container. Docker is an Open Source project that provides an open platform to pack, ship and run any application as a lightweight container. It has no boundaries of Language support, Frameworks or packaging system and can be run anywhere, anytime from a small home computers to high-end servers. It makes them great building blocks for deploying and scaling web apps, databases, and back-end services without depending on a particular stack or provider.
Here are the quick and easy steps on how we can run a GUI App in a Docker Container. In this tutorial, we'll take Firefox for the example.
1. Installing Docker
First of all, before we start, we must ensure that we have Docker installed in our host Linux Operating System. Here, we are running CentOS 7 as host so, we'll be running yum manager to install docker using the below command.
# yum install docker
![]()
# systemctl restart docker.service
2. Creating Dockerfile
Now, as our Docker Daemon is running, we'll now prepare to create our Firefox Docker Container. We'll create a Dockerfile where we'll enter the required configuration to create a working Firefox Container. We'll fetch latest version of CentOS for our Docker Image. To do so, we'll create a file named Dockerfile using our favorite text editor.
# nano Dockerfile
Then, we'll add the following lines of configuration into Dockerfile and then save it.
#!/bin/bash
FROM centos:7
RUN yum install -y firefox
# Replace 0 with your user / group id
RUN export uid=0 gid=0
RUN mkdir -p /home/developer
RUN echo "developer:x:${uid}:${gid}:Developer,,,:/home/developer:/bin/bash" >> /etc/passwd
RUN echo "developer:x:${uid}:" >> /etc/group
RUN echo "developer ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
RUN chmod 0440 /etc/sudoers
RUN chown ${uid}:${gid} -R /home/developer
USER developer
ENV HOME /home/developer
CMD /usr/bin/firefox
Note: Please replace 0 with your user and group id in 4th line of the configuration. We can get the uid and gid of current user by running the following command in a shell or a terminal.
# id $USER
3. Building Docker Container
We'll now build the container which will work according to the above Dockerfile. It will install firefox web browser and its required packages. It will then set user permission to make it work. Here, the image name is set as firefox, you can name it as your desire.
# docker build --rm -t firefox .
4. Running Docker Container
Now, finally, if everything went cool, we'll be able to run our GUI App ie Mozilla Firefox Browser from inside our firefox Docker Container running in a CentOS 7 Image.
# docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix firefox
Conclusion
Running GUI Apps in a Docker Container is really an awesome experience which will never harm/use your host Filesystem. It is fully dependent on your Docker Container. In this tutorial, we tried Firefox in our CentOS 7 Docker Image with Firefox installed. We can use many more GUI Apps with this technology. If you have any questions, suggestions, feedback please write them in the comment box below so that we can improve or update our contents. Thank you ! Enjoy :-)
In Step 2:
The line:
RUN echo "developer ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers
should be:
RUN echo "developer ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
to append (>>) instead of being overwritten (>).
Hi there,
Greetings from Linoxide Team. Thanks very much for the comment above. We'll update the article soon.
Thanks. :-)
This looks similar to another one I read last year.
http://fabiorehm.com/blog/2014/09/11/running-gui-apps-with-docker/
[root@localhost test2]# docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix firefox
Error: cannot open display: :0
[root@localhost test2]#
It would help if you had a screenshot of running firefox.
docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix firefox
Gtk-Message: Failed to load module "pk-gtk-module"
Gtk-Message: Failed to load module "canberra-gtk-module"
process 1: D-Bus library appears to be incorrectly set up; failed to read machine uuid: UUID file '/etc/machine-id' should contain a hex string of length 32, not length 0, with no other text
See the manual page for dbus-uuidgen to correct this issue.
D-Bus not built with -rdynamic so unable to print a backtrace
Redirecting call to abort() to mozalloc_abort
Shouldn't you create 'developer' user in docker before mkdir etc? Also why should it match the host's user's UID/GID? Generally speaking what's the purpose of 'developer' user? Thanks
r$ sudo docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix centos-firefox
process 8: D-Bus library appears to be incorrectly set up; failed to read machine uuid: UUID file '/etc/machine-id' should contain a hex string of length 32, not length 0, with no other text
See the manual page for dbus-uuidgen to correct this issue.
D-Bus not built with -rdynamic so unable to print a backtrace
Running without a11y support!
No protocol specified
Error: cannot open display: :0