Configuring Webserver & Python Interpreter in Docker Container over AWS

Samar Pratap Singh
8 min readMar 15, 2021

Welcome reader! Today we will see how to configure Webserver on Docker container & also run a Python code in the Docker after properly installing the Python interpreter. We need to keep in mind some concepts in order to deal with OS running inside a Container Engine like Docker. So, let’s get started…

What are Containers?

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. Container images become containers at runtime and in the case of Docker containers — images become containers when they run on Docker Engine.

What is Docker?

Docker is a software platform for building applications based on containers — small and lightweight execution environments that make shared use of the operating system kernel but otherwise run in isolation from one another. While containers as a concept have been around for some time, Docker, an open-source project launched in 2013, helped popularize the technology and has helped drive the trend towards containerization and microservices in software development that has come to be known as cloud-native development.

What is Webserver?

A web server is a computer that runs websites. It’s a computer program that distributes web pages as they are requisitioned. The basic objective of the webserver is to store, process and deliver web pages to the users. This intercommunication is done using Hypertext Transfer Protocol (HTTP). There are various Web server products available in the market, & here we are going to use one such product by Apache called Apache Webserver or Httpd.

These web pages are mostly static content that includes HTML documents, images, style sheets, test etc. Apart from HTTP, a web server also supports SMTP (Simple Mail Transfer Protocol) and FTP (File Transfer Protocol) protocol for emailing and for file transfer and storage.

Pre-requisite: The only pre-requisite I’m keeping in this blog is to have an OS already which will be the base OS for the Docker Engine in our case. In my case, I’ve installed an OS (Instance) on the AWS Cloud but you can have the OS wherever you want, maybe in Cloud or bare-metal or Virtualization, etc.

Configure WebServer in Docker Container:

There are various Web server products available in the market, and here we are using one of such webservers named Httpd or Apache Webserver by the company called Apache.

  • This is the RHEL 8 base OS I’m using here, which is running in AWS Cloud & Public IP Address is also attached to it.
  • Firstly, we need to properly configure the Docker Engine in our base OS. But for that we need to configure Yum which is a package installer in Rhel 8 OS, that will help us install Docker software.
  • It quite simple to configure Yum, just create a file with <some_name>.repo in the location /etc/yum.repos.d/
  • Now as we have configured the yum, so we can install the Docker-ce program with the command below.
# yum install docker-ce --nobest -y
  • We need to apply — nobest option here because Docker-ce software & Rhel 8 OS have some compatibility issue or package conflict while installation. So, just apply this option otherwise we won’t face any issue while using Docker Engine later.
  • Docker-ce software is installed, now we can start the Docker services in order to start using the Docker Engine. I’m enabling the Docker service so that the service will be up even after a reboot. Commands to start & enable the Docker services are:
# systemctl start docker
# systemctl enable docker
  • Now the Docker services are up, which we can check by this commad.
# systemctl status docker
  • So, the Docker is configured properly in our Base OS & is ready to use. We don’t have any Docker container running currently & no images available locally in our system.
  • Let’s now install a Docker container in which we will configure the Apache webserver.
  • I’m provisioning a Docker container from Centos image with the name myos. I’m doing patting here so that our web server in the container can be exposed to the outer world.
  • Patting needs to be done because the Container is isolated in nature, hence no one can directly connect to them. Through patting one can access any program in the container by going through port no. (a program) of the Base OS.
  • So, here patting is done in such a way that if someone comes on the port no. 8081 (you can choose any free port no.) of the Base OS & internal they will be directed to the 80 port of the Container & 80 is the port no. on which Web server runs.
  • I tried to explain patting in minimum words but patting is not our point of focus here. Basically, we are doing all this to expose Container port 80 (webserver) to the outer world so that anyone from the internet can access your webpages, provided your Base OS should have Public IP Address.
  • Now as our Docker Container is launched & also have received the container’s terminal, let’s configure our web server. For that firstly install some required packages like net-tools, vim & Httpd.
  • Now deploy your webpages with all the HTML, CSS, JS & all the sophistication you want. But here just for the demo & to save time I’ve created a very simple HTML Webpage.
  • Then whatever webpages you have, save them in the document root of the Apache Webserver: /var/www/html. It is this location which is called the Document root is from where the Apache web server reads the Webpages.
  • This is my demo Webpage :`)
  • Finally, we have to start the httpd services, so that our web server starts working, but here is an issue. In The RHEL 8 OS, in order to start or stop any services we need to use systemctl command, but it doesn’t work in the container. Following is the command which works great to start httpd services in the normal OS, but doesn’t work in a Container.
# systemctl start httpd
  • But we can do it the other way round. The above command behind the scene runs another command to start the httpd services, which we can directly run in the container & it works absolutely fine.
# /usr/sbin/httpd
  • Above is just a warning kind of thing, but our httpd services is up. This will be confirmed if we can connect to our webpages present in our web server in the Docker Container from the outer world.
  • To connect to the Webpages below is the URL specified. URL contains the IP Address of the Base OS & followed by port no. given while provisioning of the Docker container & patting. We also need to specify the webpage to which we need to connect, but in the case of a webpage named with index.html or index.php, etc we don’t need to specify it like in mine.

http://<Base_OS_IP>:<port_no>/<webpage>

  • URL in my case is: http://65.0.132.34:8081
  • Finally, we have configured our Apache webserver in the Docker container properly & it is working great.

Setting Python Interpreter & running Python code on Docker:

This part is quite simple & straight forward, we just need to install the Python interpreter on the Docker Container & run any Python Code.

  • I’m installing Python version 3 interpreter on the Docker container. For this just use the following yum command.
# yum install python3 -y
  • That’s all we had to do, and we are ready to code in Python in our Docker Container. We can either code in the REPL or create Python Program files.
  • So, here I will try to execute my Python code which I created recently to automate LVM tasks in Linux (It was quite fun :) ). So, let me clone that code from my GitHub repo, but for cloning from GitHub we require a Git program in our Docker container, so install it first.
# yum install git -y
# git clone https://github.com/Samarps/lvm_partition_auto_python.git
  • The repo is cloned, so let’s try to execute the program & see whether the Python code is working here or not.
# python3 main.py
  • You can try by executing some task from the given menu of LVM Automation program.
  • So, this Python program is working fine & hence we successfully installed & executed Python on Docker Container.

Conclusion:

Technologies are enhancing day by day & in today’s DevOps & Automation world, the industry is trying to work mostly on Containers instead of traditional ways of provisioning OS like Bare metal & Virtualization which are very slow & waste resources unnecessarily. Therefore, it is important for us to understand how things work inside Containers & some different concept available in this technology which we need to keep in our minds.

This was the purpose of this blog content & I wanted to aware you guys about all this. Hope you learned something new & would have added some value to your knowledge. We’ll meet in the next one, till then keep learning, helping & growing! :)

--

--