Configuring Webserver & Python Interpreter in Docker Container over AWS

What are Containers?

What is Docker?

What is Webserver?

Configure WebServer in Docker Container:

  • 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.
  • 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:

  • 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:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store