How to Configure Apache Webserver inside Docker Container

Rupali Gurjar
LinuxWorld Informatics Pvt. Ltd.
4 min readNov 22, 2020

--

by Rupali Gurjar

What is Apache HTTP Server

The Apache HTTP 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.

Although we call Apache a web server, it is not a physical server, but rather a software that runs on a server. Its job is to establish a connection between a server and the browsers of website visitors (Firefox, Google Chrome, Safari, etc.) while delivering files back and forth between them (client-server structure). Apache is a cross-platform software, therefore it works on both Unix and Windows servers.

Apache HTTP Server

What is Docker Container

Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries and configuration files; they can communicate with each other through well-defined channels.

Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Docker

Task Overview

  • Configuring HTTPD Server on Docker Container
  • Setting up python Interpreter and running Python Code on Docker Container

Task Description

For this task, I am going to use RHEL8 VM . So there is a pre-requisite

  • There must be docker installed in our VM/OS.

Let’s begin the task !

It is always a good practice to make separate workspace so that we can easily manage the things

mkdir <directory_name>
cd <directory_name>
Create a workspace

There are multiple ways to launch a docker container, here I’ll use Dockerfile .

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.

We have to configure webserver, so I create a html page, also create a python page to check whether the python interpreter is installed or not.

Here is my html page

index.html

And here is my python page

test.py

Now let’s create a Dockerfile

FROM centos:latest
RUN yum install httpd -y
RUN yum install python3 -y
COPY index.html /var/www/html/
COPY test.py /
EXPOSE 80
CMD /usr/sbin/httpd -DFOREGROUND
Dockerfile

This Dockerfile will launch a docker container using centos image. In this container, it will install httpd and python3 software. It will copy both the pages and expose port no. “80".

Here we need to start the web service, for this we use this command

/usr/sbin/httpd -DFOREGROUND 

To launch the container, let’s build a customized image using this Dockerfile

docker build -t <image_name>:<version> /directory/where/the/dockerfile/is 
Build Image

Now, we can launch a container using this image

docker run -it -p 8090:80 --name <os_name> <image_name>
Launch Container

It will expose it on the port no 8090

We can verify, the webserver is correctly configured or not by checking the web page

http://<ip_of_os>:8090/<page_name>
webpage

To see python interpreter is installed or not, we go inside the container and check

To go inside the container, we can use this command

docker exec -it <os_name> bash
Run Python Code

Here we can see python interpreter is successfully installed .

Hope you like this Article !

Thanks for Reading :)

--

--