How to Configure Web Server on the Top of Docker Container?

Shruti
4 min readMar 14, 2021

So, Let’s start and do this amazing setup

Some Prerequisites: Basic Knowledge of docker , linux and you should have docker installed on your system.

Note: I am using RHEL8 system as a base operating system for this task.

Some basic docker commands and their description:

#systemctl start docker : This will start docker service.

#systemctl status docker: It will show the status of docker service weather it is running or not.

#docker ps: It will show you all the containers running under docker.

#docker stop container_name or #docker start container_name : This will stop or start your container.

#docker attach container_name : you will get the terminal of container.

#docker logs container_name: You can see all the process going inside a particular container without attaching to it.

#docker images: Will show you the existing docker images on your system.

Step 1:

Start Docker by using systemctl start docker command.

starting docker service

Step-2:

Pull the Docker Image by using docker pull centos:7 command. this command will pull the image from docker hub.we should have image in our system to launch a container and then work on it, Here i am pulling or downloading centos:7 image from docker hub.

pulling an image

Step-3:

Launch the container on the top of Redhat 8 by using the below command , I put the name “webserver” for my container , you can take and use any random name.

launch a container

Step-4:

Here Before installing make sure to stop firewall in Base OS. In Redhat 8 to stop firewall we use systemctl stop firewalld command.

Install Apache httpd software by using yum install httpd -y command.

installing apache webserver

Step-5:

Start the HTTPD service by using /usr/sbin/httpd command.

Normally in Redhat 8, to start this service we always use systemctl start httpd command.But in docker we don’t have that command hence instead we use the command which is mentioned in the image below.

Starting httpd services.

Step-6:

Go to /var/www/html directory and create a file there with .html extension , We are creating .html file inside /var/www/html because it is a document root for apache server which means apache picks file from /var/www/html by default.

document root for apoache server

Creating a file intex.html

creating index.html file
content of index.html file

Step-7:

Use yum install net-tools command to install the net-tools software to access the ip of your container.

installing of net-tools

Access the ip of a container using ifconfig command. Here we can see 172.17.0.2 is a ip of our container having name webserver.

ip of a container

Step-8:

Access the webpage in Browser by typing ip_of_a_container/file_name on your web browser.

webpage

GREAT!! WE DID IT 🤩🥳

Some Extra Things: Running Python interpreter on the top of docker👇👇

Step 1:

Install python by using yum install python -y command.

installing python

Step 2:

Create a file , here i created show.py but you can create a file with any random same.

creating file
content of show.py

Step 4:

Run it and we are done!!

Thanks for reading😄

--

--