Harnessing the Power of Docker
Hey folks, recently I have been learning Docker and I have been writing Dockerfiles for several open source projects including some projects of mine Wise Words and
Tic Tac Toe.
I found an amazing project Plant Vs Zombie by Marblexu and I decided to contribute to it. A few days ago, I started to write Dockerfile to containerize this application and I found that the main challenge was to get the GUI of this application and surely I found a way to get it. I will be sharing all the information about it in this blog.
So let's start from the basics...
What is a Dockerfile?
A Dockerfile is a text document that contains information to build a Docker image. This image is then used to run docker containers. Here's a basic structure of Dockerfile:
FROM: It is used to build a docker image over an existing image with desired commands. It is used to set the base image for the new image.
WORKDIR: It is used to specify the working directory where all the commands will get executed.
COPY: As the name suggests it is used to copy files. It takes two parameters source(what to copy) and destination(where to copy)
RUN: It is used to specify the commands that are to be executed when the docker image is built. For example commands for installing dependencies, running or configuring services.
ENV: It is used to define an Environment Variable to be used by the application running in the container.
EXPOSE: It is used to expose a port of the docker container to the host.
CMD: It is used to define a standard or default command for the container. It is typically referred to as the entry point of the application. It can be overridden by the docker run command.
How to build images and run containers from it?
Here's a flowchart explaining how to build images and containers from a Dockerfile.
Docker Image is a read-only blueprint used to create docker containers. These images are lightweight and portable. Docker Images are usually hosted on the Docker hub. To list the docker images on your device:
docker images
While Docker Containers are instances of the docker images. These are separate environments in which an application runs. You can think of it as a virtual machine running an application that is encapsulated from the host operating system. To list docker containers on your device:
docker ps -a
My Approach
I have written the Dockerfile as usual and also made the image of that with the "docker build -t sanmargparanjpe/plantsvszombie . ". Firstly, I used Python3 docker image as the base image for my image then, I updated all the repositories on that image further, and I installed pygame library for the Python application to run. Then I have used /app/ as the working directory for my application and cloned the repository into it. At last, I have specified the entry point of my application which can be overridden by the following command:
docker run sanmargparanjpe/plantsvszombie <some_other_command>
The Dockerfile looks like this:
Building the Image...
docker build -t sanmargparanjpe/plantsvszombie .
Configuring the X11 display server...
X11 provides a framework for managing graphical output and user input in Unix-like operating systems. It provides a way for the applications to display GUI(Graphical User Interface) via a network connection.
X11 display server also known as X Server is a part of the X Windowing System, it is responsible for the graphical rendering and handling of user inputs. By default, it only accepts connections from the local machine but we want to connect our X server to the docker container in order to get the GUI. So we will need to configure our X server to accept remote connections. We can do this by running the following command:
xhost +
After executing this command, our X server will be accepting the connections from remote machines.
Running the container...
In order to run a Docker container with GUI applications, we need to perform X11 forwarding to display the graphical output of the containerized application on our local machine.
In order to achieve our goal and get the GUI the following command was used:
docker run -it --env="DISPLAY" --net=host sanmargparanjpe/plantsvszombie
Here, we have used some additional flags that are it, env and net.
it flag: It is used to allocate an interactive shell in the container in order to communicate with the application running in it.
env flag: It is used to pass the environment variable "Display" of the host to the container hence, telling the application the X11 display server address.
net flag: It allows the container to directly connect to the X11 display server on the host as by this the container and host share the same network interface.
I have hosted this docker image in the docker hub, the link is here. You can directly pull this image and then run the container.
Connect with me๐
Subscribe to my newsletter
Read articles from Sanmarg Paranjpe directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by