Configuring HTTPD Server on Docker Container and Setting up Python Interpreter and running Python Code on Docker Container
Apache HTTPD is an HTTP server daemon produced by the Apache Foundation. It is a piece of software that listens for network requests (which are expressed using the Hypertext Transfer Protocol) and responds to them. It is open source and many entities use it to host their websites.
For configuring apache web-server on top of docker container running on the base Redhat OS first before launching the docker container we need to configure firewall rules.
After configuring the firewall rules, it is mandatory to restart the docker. Now we need to launch CentOS container on top of Redhat docker host. To launch a centos container using the command : docker run -it centos:7
Steps to configure APACHE WEB SERVER
- Install the httpd software using yum or dnf command.
- Create webpages. Configure the server to tell it about the webpages.
- Start your service.
Now, installing the httpd web server software using the command :
yum install httpd -y
Now we have to set-up our web-pages in /var/www/html folder, but the issue is docker doesn’t provide us with GUI, so we need vim if we want to write something in our web-page. Let’s install vim also using yum command:
yum install vim-enhanced
If we want to host our website, we have to move our webpages to this particular location (/var/www/html), only then our web server can show this webpages to the clients. We will create a test file in this directory.
The third step is to start the service, but systemctl command is not supported in docker.
As we know to any command there is a software and to that software there is program file where we can find the internal what happens when we write systemctl command and /usr/sbin/httpd is the internal file which is starting the execution.
So simply run, /usr/bin/httpd/
We can use the netstat -tnlp command to check all the running ports and hence verifying if the httpd service is running or not.
To access any web page we need the IP. In linux we can find IP by using the ifconfig command. Again the software is not installed. So, first we need to install the software and use the command .
Setting up Python Interpreter and running Python Code on Docker Container
We can simple launch python interpreter on docker container using the command:
docker run -it — name pythonInterpretor centos:7
We can also install the python interpreter using the command:
yum install python3
Now we can easily use python on top of the docker using the command:
After running this command, we notice that a docker container of version 7 is launched on top of our base OS. The next step is to create a file which contains the python code, but for creating the file we need either a graphical or command line based text editor, example — vim or gedit. But docker doesn’t come with these software pre-installed in it. So let’s install the software first using yum command. Head over to the base OS in another tab.
which vim Output: /usr/bin/vim
After then, let query rpm to find out the software which provides us vim — CLI text editor.
rpm -q -f /use/bin/vim
So we can see that the software which provide vim is “vim-enhanced”. To install it in the container, go back to the container and run the following command:
yum install vim-enhanced -y
Now it’s the time to create a file with python code (let say code.py):
to insert the code press i
x = 9
print(“python in docker container”)
to save the code and exit vim press :wq
Let’s run the python code:
python in docker container