Launching graphical application inside the docker container
SAKSHAM TRIVEDI
Security (SOC) Analyst || Microsoft Certified Security Operation Canter Analyst
Hello guys, In this article I am going to discuss how to launch the graphical application inside the docker.
So, for this I am going to use Mozilla Firefox as a web browser that will be launched inside the fedora docker container. So, to this there are some prerequisites that have to be fulfilled to successfully run a docker container.
Prerequisites:
So, without nodding let's start and jump to practical as I also hate theory.
First open the terminal, create a directory let's say docker-gui (in my case) and enter into the directory by typing.
$ mkdir docker-gui && cd docker-gui/ && pwd
as you can see in my terminal window
Then create a file named 'Dockerfile' inside the directory using any text editor you like I have used vim for that.
Now, append the following lines into the file and save it.
Disclaimer: You can skip the line written after '#' it's all just comments but don't forget to match case
Code Explantion:
The first line, i.e.
From fedora: latest
is telling the docker to pull latest fedora image from the docker repository then the line,
RUN dnf update -y && dnf install firefox -y
is used to tell the docker to run the commands inside the docker container by image in our case
dnf update -y
command is used to update the repository metadata and update all the installed packages to the latest version available in the repository. while
dnf install firefox -y
command is issued to install firefox package and all it's dependencies and co-dependencies to run firefox browser inside the docker container.
Remember: To append '-y' at the end of each command while installing packages. otherwise docker will throw error when the container will start building and installing packages as '-y' denotes that you have already permitted all requires changes while executing the 'dnf' command
Now, the part
RUN echo "memory 4g" >> /etc/sysctl.conf
RUN echo "limit memblock unlimited" >> /etc/sysctl.conf
is used to remove the default memory limit configured by docker for each container just in case our application gets crashed while running due to low system resources inside the docker container.
So now, let's move further with Docker part.
Once, the file has been created pull the latest fedora docker image by typing below command in the terminal:
$ sudo docker pull fedora:latest
As you can also see in my terminal window:
Now after entering the command if terminal prompts for password type the password and hit enter.
Docker will start pulling the latest fedora docker image from repository into your system. If you get the similar output like below then it means docker have pulled the fedora image into your local system
So, now that we have got the docker image cached in our system for use we can start building the container simply by typing the command and name you want to give to your docker container...
$ sudo docker build -t my-gui-app .
as shown in the terminal window:
after typing hit enter, and wait for few seconds or minutes maybe the command will start generating container from the image pulled by docker. you might see the output like below...
Now, after this output once the command stops generating output you will see that the container is now successfully running.
But SK!!! Where is the GUI?
Hold on... guys I know that you might ask this question and that's totally fair too as I promised GUI inside container but whole time I we sticked with the terminal window fuzzing with fonts.
领英推荐
But there's a saying "patience is a key to success", so here we are...
only far from just one command.
So, as we know that the container has built and running in background so in order enter into the container to interact with it, In terminal enter the following command.
$ sudo docker run -it --env="DISPLAY" --net=host my-gui-app
as you can also see in my terminal,
Here, docker run command is issued to run the container along with the option '-i' and '-t' which are used to denote that we want to run the container in interactive mode as well as in terminal session while --env=DISPLAY is used to provide access to the host display outside the container while --net=host is used to share the host network for internet connectivity.
after this you will be entered into the container...
as you can see in my terminal username changed from sk to root (user inside container)
now as we know that we have already installed the firefox browser inside the docker so check that it is running or not you can try running firefox by entering
firefox
command, as you can see in my terminal I was able to successfully run the browser or graphical application better to say.
But I didn't stopped here to check everything is working fine or not I again installed a graphical file manager Nautilus inside the docker container by running
$ dnf install nautilus -y
command, and from the screenshot below you can also see that everything with the file manager is working absolutely fine.
So that's it for now, we will meet again in another article.
Till then you can go through my recent articles here
Thanks,
- SK -