What is Docker?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

What is a container?

A container is a runnable instance of an image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.
By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine.

Apace Webserver:- Apache HTTP Server is a free and open-source web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web.

Let’s get started…………………………….

There are many ways to create a webserver in Docker but we follow then a simple one.

Step 1:- Create a directory webserver

sudo mkdir webserver

Step 2:- Create an index.html in webserver directory.

sudo vi index.html

Paste the below code in index.html

Welcome To Bangmetric

Welcome To Bangmetric

At Bangmetric we help you to get the most out of your investments in the latest cutting-edge platforms that allows our customers to harness the power of automation and drive true digital transformation. Bangmetric is founded by a group of experts with years of experience of implementing, optimizing and managing several cloud platforms to a wide variety of clients, ranging from SME’s to global co-operations. Bangmetric are proud of our unique customer-centric approach and deliver exceptional ROI with rapid time to value. Bangmetric’s technical consultants can help you make your IT a proactive business partner.

save and exit

Step 3:- Create a file name Dockerfile in webserver directory with below instructions (The name Dockerfile is hardcoded don’t change it)

sudo vi Dockerfile
FROM centos:7
MAINTAINER Bangmetric pvt. ltd.
RUN yum -y install httpd
COPY index.html /var/www/html/
CMD [“/usr/sbin/httpd”, “-D”, “FOREGROUND”]
EXPOSE 80

Dockerfile Commands

  • Maintainer:- This command is used to give information about the author or manager who is managing this image MAINTAINER Bangmetric pvt. ltd.
  • From:- FROM instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile must start with FROM instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public Repositories.
  • Run:- Before building an image if want some configuration that needs to be present in the image. Inside the image we need to install Apache web server image the command to install that image is “”RUN yum -y install httpd“.
  • Copy:- This command is used to copy a file from host os to docker container
    “”COPY index.html /var/www/html“.
  • EXPOSE:– This command is used to specify the port number in which the container is running its process. Anybody can come from outside and connect to this port. Apache webserver is launched at port 80 by default that is why we need to expose container at port 80. “EXPOSE 80
  • CMD:- To run a command as soon as container is launched. CMD command is different from RUN because RUN is used at the time of building an image and CMD used to run command when container is started.
  • /usr/sbin/httpd:- This command is used to start the web server
  • -D FOREGROUND:- This is not a docker command this is http server argument which is used to run webserver in background. If we do not use this argument the server will start and then it will stop.
  • CMD [“/usr/sbin/httpd”,” -D”,” FOREGROUND”]

Step 4:- Build the Docker image with docker build

docker build -t webserver:v1 .

Note:- -t option is to tag or name the image and . is used because you are in a directory where Dockerfile is present

Step 5:- Now check the Image with docker images

docker images

Step 6:- Now Run the Docker image

docker run -dit -p 8000:80 webserver:v1

-dit means run container in background -p define the port

Step 7:- check the result on webbrowser <ip:port>

Congratulations you follow all the steps and setup apache webserver in docker

Your email address will not be published. Required fields are marked *