DEV Community

Cover image for Setting Up Web Server On Docker Container
Abhishek Dwibedy for Spectrum Club

Posted on

Setting Up Web Server On Docker Container

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

  • 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

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
  2. Now install the Docker software using yum command. Command : yum install docker-ce — nobest Alt Text Alt Text 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.

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
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 :
Our set-up is done Successfully. Now let’s jump to our Task :

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
Now we have to run and launch the container :

For running the container and exposing the port write below command
docker run -it — name :
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
Checking the ports on the Host Machine

For checking used ports
netstat -tnlp
Alt Text
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
Alt Text
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
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:
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

Alt Text
And we can clearly see that our webservices running at port 80
Accessing the WebPage
Alt Text

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

!! Thank You for Reading the Article !!

Top comments (0)