How to configure Httpd Webserver and Python Interpreter within Docker container

Satyam Singh
5 min readMar 16, 2021

Hello Fellow Programmers !!!

Today I would like to share step by step procedure to configure Apache Httpd Webserver and to set up Python interpreter over Docker container.

Content of this Blog

  • Basic Concepts
  • Setup Procedure

Basic Concepts

Docker

  • Docker is a open source platform for building, deploying and managing containerized applications. It enables developer to package applications into containers.
  • Containers are the executable components that combines the application source code with the Operating System(OS) libraries and dependencies required to run the code in any environment.
  • Docker makes it easier, simpler and safer to build, deploy and manage containers.

Why use Docker ?

  • Doesn’t require machine-specific configurations
  • Could be easily configured for granular update
  • Provides automated container creation
  • Makes container versioning possible
  • Enables container reuse
  • User-contributed containers could be used from open-source registry

To know more about Docker, visit the site below:

Docker Documentation:

Apache Httpd Server

  • The Apache Httpd Server, colloquially called Apache, is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0.
  • Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation.
  • The vast majority of Apache Httpd Server instances run on a Linux distribution, but current versions also run on Microsoft Windows, OpenVMS and a wide variety of Unix-like systems.

Apache Httpd Documentation:

Python

  • Python is an interpreted, high-level Object and general-purpose programming language. Python’s design philosophy emphasizes code readability with its notable use of significant indentation.
  • It is dynamically-typed and garbage-collected and supports multiple programming paradigms including structured(particularly, procedural),object-oriented and functional programming.

Python 3 Documentation:

— — — — — — — — — — — — — — — — — — — — — — — — —

Setup Procedure

Before understanding the setup procedure, let’s get a clear picture of the pre-requisites required for the same.

Prerequisites

  • As the setup is placed over AWS(Amazon Web Service), EC2 Instances needs to be launched corresponding to that.
Selected Instance i.e., TaskInstance is the one that would be used for implementation
  • Docker should be pre-installed and enabled before configuring Apache Httpd Webserver and Python interpreter.

Procedure

Apache Httpd Webserver Configuration on Docker Container

  • Create Docker container using “docker container run” command. p option in docker run command is used for PAT(Port Address Translation) to access the container from the outside i.e. from EC2 Instance. i and t option in the command is used for providing STDIN and pseudo terminal respectively. Then, install httpd in the container.

AMI Used : Amazon Linux 2 AMI (HVM), SSD Volume Type

  • List of running container could be obtained using “docker ps” command.
docker ps before launching “httpd_webserver”
docker ps after launching “httpd_webserver”
  • httpd service could be started from within the container using “/usr/sbin/httpd”.
  • The web page to be launched using Apache Httpd Webserver, needs to be placed in the Document Root(/var/www/html).
index.html is placed in the Document Root
  • httpd services could be started automatically as soon as the respective container is accessed by placing the respective commands in .bashrc file (script file that executes when user logs in ).

/usr/sbin/httpd : Starts httpd service within Docker container

rm -rf /var/run/httpd/* : Deletes the content of directory “/var/run/httpd” (Reason is mentioned in the Note below)

Note : “/usr/sbin/httpd” doesn’t update the process id in /var/run/httpd unlike “systemctl start httpd”. So when the service starts, it checks for the presence of process id i.e. httpd.pid and other associated file. If the file exists already, it would assume that the process is running already, even though it’s not and doesn’t start the service.

This results to the problem that the service doesn’t start when the respective container is accessed again after the first time. Thereby to resolve the issue, every time the container is accessed, the respective files should be deleted and then service should be started.

  • “docker inspect” command could be used to obtain detailed information about the container specified. In this case, it could be used to obtain IPv4 address of the respective container.
IPv4 address is specified in IPAddress within Networks
  • Container could be accessed from within the container could be accessed using “curl” command.
  • Container could be accessed from outside system using the format <ec2_instance_public_ip>:<port_published_using_PAT(8081 in this case)>.

Setting up Python Interpreter on Docker Container

  • Create Docker container using “docker container run” command. i and t option in the command is used for providing STDIN and pseudo terminal respectively. Then, install python3 in the container.

AMI Used : Amazon Linux 2 AMI (HVM), SSD Volume Type

  • Python code could be executed using “python3 <program_file>”.
Python Code : Factorial Program
Program Output

— — — — — — — — — — — — — — — — — — — — — — — —

Thank You !!!

--

--