Setting Up Web Server On Docker Container
Tags: docker
Published: 2021-03-16 17:04:14

Today, I’ll be showing you how to configure HTTPD Web Server on the top of Docker Image.
So, I have one newly provisioned RHEL8 OS, first of all we need to install docker software in that OS. For that we need to configure yum in that OS for Docker Repository.

**Before start the above configurations first let me know you “What is Docker” ?**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0n00326ttb9deuzjv827.png)
* Docker is known as Containerization Technology. It is a powerful tool designed to make it easier to create, deploy, and run applications by using containers.
* Docker enables you to ship isolated services as often as needed.
* The most important thing Docker is an open source project.
* Rather than creating a whole virtual operating system, Docker allows applications to use the same Linux kernel as the system that they’re running on and only requires applications be shipped with things not already running on the host computer which gives a significant performance boost and reduces the size of the application.
* We can create “multiple” containers.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gglm4s4yt6y3mcwfi2c0.png)

**How to use Docker ?**

To use Docker is not a big deal because it is so simple to use only few steps to do. But the major thing is you have to aware about your requirement that what you exactly want to do with this technology.
So let’s I’m going to show you the Docker set-up in RedHat Linux.
1. First we have to download the Docker package repository from browser using “wget” command because in RHEL8 the Docker software is not available.
*Command : wget https://download.docker.com/linux/centos/docker-ce.repo*
2. Now install the Docker software using yum command.
*Command : yum install docker-ce — nobest*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rehoyqedqbchmq14nyv.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dar11ddn2l7jwbcfc0ad.png)
**Installing and Setting Up Docker-ce**

Configuring YUM repository for Docker-ce
Create a repo file in /etc/yum.repos.d/ directory for the docker repository.
* vim /etc/yum.repos.d/yum.repo
And in this yum.repo file add this below code.
* [docker] name=docker baseurl=https://download.docker.com/linux/centos/7/x86_64/stable/ gpgcheck=0*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0o435nq0fziqdkz26wt.png)
3. Check the version.
*Command : docker — version*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/76q179mkzgjvsgmivdmr.png)
**Starting the Container Services**

For starting the container services
*systemctl start docker*
**Checking the Status of Service**

For checking the status of the services
*systemctl status docker*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/koebrp3asoo52iywpeld.png)
**How to pull images on Docker ?**
Docker images are are the starting point for anyone using Docker for the first time. A Docker image is made up of a collection of files that bundle together all the essentials, such as installations, application code and dependencies, required to configure a fully operational container environment. Each of the files that make up a Docker image is known as a layer. We can find the images from DOCKER HUB It contains many pre-build images so we can pull from it.
*Command : docker pull image:version*
We can pull any images like ubuntu, centos, redis many more as per our requirement and we also have to write the version of image, if we didn’t write the version, it will not occur any problem it’s just take latest version as default tag.
How to launch and run the Container on Docker ?
To launch the container we have required to run the container and give us one interface(-i) and one terminal(-t) to write our commands. We can also give our own name to the container. So we have to follow the simplest command which is —
*Command : docker run -it — name <give_ur_cont_nm> <give_image_nm>:<version>*
Our set-up is done Successfully. Now let’s jump to our Task :
**🔰 TO CONFIGURE WEB SERVER ON DOCKER CONTAINER🔰**

First we have to pull the image here I’m taking centos image as latest version :
For pulling the docker image use the command below:
*docker pull centos:latest*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chsdmwy1c2xmxpemsh3v.png)
**Now we have to run and launch the container :**

For running the container and exposing the port write below command
*docker run -it — name <any-name> <image>:<version>*
For running the container and exposing the port command is
*docker run -it — name webserver -p 8080:80 centos:latest*
Here -p is used to expose any port to the host machine. 8080:80 means we are exposing port 80 of Docker Container to port 8080 of the Host Machine.
We are exposing port 80 only because httpd server works only on port 80 by default.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bh2cokirgw3h8fw36inh.png)
**Checking the ports on the Host Machine**

For checking used ports
*netstat -tnlp*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qlwz0fe4zzlaz7olyol3.png)
Here you can show the port on which container is running.
**Configuring and Starting up the HTTPD Server on the Container**

Note: You have to run the command on Container prompt
**Installing the httpd server**

For installing httpd software run below command
*yum install httpd*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fm63vds1e5bscvwi76cl.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vub7wz4luqi089jn3iyc.png)
**Copying/Creating Webpages**

Now we have to create an webpage.html file inside the document root(/var/www/html) folder and deploy the web page on the Web Server
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j39xgpd7gxtk3aqrhqvn.png)
**Starting the WebServices**

Since we don’t have the systemctl command on docker so we have to directly run the program and for that we need to write command:
*/usr/sbin/httpd*
So Now we have to check whether services are running or not, we need to check the occupied ports.
For that we have netstat-tnlp command but to run this command we need net-tools
In order to get net-tools we have to run below command:
*yum install net-tools*
Now run the command below to display network status and protocol statistics.
*netstat -tnlp*

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kc1ixy4o8dnp3l6bdtii.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/258o9lii8m14yoe3xyg3.png)
And we can clearly see that our webservices running at port 80
**Accessing the WebPage**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owmyri2f9s957mnqpyg7.png)

*Here we can see that web page is running Successfully !!*

**!! Thank You for Reading the Article !!**