How to configure Web Server on Docker Container

How to configure Web Server on Docker Container

Here I'm gonna tell you how can you configure Web Server on a Docker Container. So first what is a Docker container?

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings. Container images become containers at runtime and in the case of Docker containers - images become containers when they run on Docker Engine. Available for both Linux and Windows-based applications, containerized software will always run the same, regardless of the infrastructure. Containers isolate software from its environment and ensure that it works uniformly despite differences for instance between development and staging.

To configure the Web Server in Docker first we need to install Docker in the OS. But before installation, we need to create a docker repo file in /etc/yum.repos.d directory.

So, we will create a file named docker.repo which contains:-

[docker]
baseurl=https://download.docker.com/linux/centos/7/x86_64/stable
gpgcheck=0

7.jpg

So we have configured Docker repo and now we can install docker by using command:-

yum install docker-ce --nobest

ce in the last stands for Community Edition which is free to use.

It will take some time to download and install the docker, if downloading fails try again after some time.

Now the docker is installed in our system, we can confirm by using command:-

rpm -q docker

Now Docker container uses Docker images, we can download docker image either by visiting hub.docker.com or by using command:-

docker pull <image_name>:<version>

If we don't specify the version it will by default pull the latest version of that image, here I'm downloading docker centos image by using docker pull centos.

pull centos.jpg

Now when the image is downloaded to run this image:-

docker run -i -t centos:latest

docker run.jpg

Now to configure webserver in any OS we usually have to go through 3 steps:-

  1. Downoad the software.
  2. Write a HTML code in /var/www/html folder.
  3. Start the service.

But here in the Docker container there's a little tweak.

First, to download the software we use:-

yum install httpd

6.jpg

Then second, we will put some html code in /var/www/html folder.

1.jpg

At last we start the httpd service by:-

systemctl start httpd

5.jpg But running this command will cause an error. Since there is no PID is created and docker does not have an init system.

But if we research a bit more benind the scenes systemctl calls /usr/sbin/httpd, so we can start httpd by directly using:-

/usr/sbin/httpd

4.jpg This will start the httpd service.

To check our html pages we search machine IP address in the search bar of web browser. To check IP address use command:-

ifconfig

But this command is not present in the docker container initially so we have to install it, but before installation, we need to check the package name which provides the ifconfig command.

To check the package name we use the command:-

yum whatprovides ifconfig

Here we can see the package net-tools provides ifconfig command, now we install the net-tools by yum command:-

yum install net-tools

3.jpg

This will install the net-tools package and we can see the IP address by using ifconfig command:-

2.jpg

Now if you search this IP address on web browser:-

8.jpg

Here you can see the HTML pages you've transferred to the /var/www/html directory.