How to configure webserver in docker container

Devesh Bhardwaj
4 min readMar 14, 2021

what are container’s ?

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.

Docker containers that run on Docker Engine:

  • Standard: Docker created the industry standard for containers, so they could be portable anywhere
  • Lightweight: Containers share the machine’s OS system kernel and therefore do not require an OS per application, driving higher server efficiencies and reducing server and licensing costs
  • Secure: Applications are safer in containers and Docker provides the strongest default isolation capabilities in the industry

steps-:

  1. You need docker in your system to install docker you need docker repo in your repos.d folder

Go to this location

/etc/yum.repos.d/

add this in you repos file

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

2. install docker using yum command

yum install docker-ce — nobest

3. after installing docker now enable the service using

systemctl enable docker

4.Now pull the image ie centos

docker pull <image_name>:<version>

5.Now run the new container using the image you downloaded in step 4

docker run -it centos:latest

6.Now install apache sever in this new os (container) using yum command

yum is already configured in this image so just we need to enter the the command.

yum install httpd

7.In apache webserver all the files that shows on server should be saved at

/var/www/html

so go to this folder and paste your php/java/python/html or any code file that you want to save in server and to show in web.

8.After that start the service of httpd and all set now your webserver was ready to use.

systemctl start httpd

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

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 install net-tools

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

Final output-:

--

--