Configure Webserver And Python Interpreter Inside Docker Container

Configure Webserver And Python Interpreter Inside Docker Container

Objective

In this article , you can learn how we can run docker container and configure apache webserver inside the container

We will also cover :-

  • Installation of Docker on redhat 8
  • How to start docker services
  • How to run docker container
  • How to go inside container and configure apache webserver
  • Also learn how we can run python code inside docker container

Introduction

  1. Docker :- Docker is a tool designed to create, deploy, and run containers and container-based applications very quickly. Docker can install and start our operating system in just 1 second. It also consume very less ram and storage. Docker can run an operating system in just 20-30 mb Ram .
  2. Apache Server :- An Apache Server is a web server application that delivers content such as HTML pages, multimedia and CSS Style sheets over the internet. Apache is a community-developed web application published by the Apache Software Foundation. It is arguably the most popular web server software available on the World Wide Web.

Let's start

Step-1 Installation of Docker

I use Redhat 8 linux as a base operating system for Docker . First we login to os by root user.

  1. First configure yum for Docker :- In redhat 8 , we use "yum" package installer. In this step , we tell yum , the internet location of our packages(docker).
https://download.docker.com/linux/centos/7/x86_64/stable/ 


No alt text provided for this image

We can verify yum configuration by :-

yum repolist

No alt text provided for this image

2. Install Docker :-

We can install docker by the yum installer of redhat 8 .But docker is not meant for the redhat 8 . So we have to use a trick (--nobest ) option to install docker . So the command is :-

yum install docker-ce  --nobest
No alt text provided for this image

So docker software has installed or not you can check with help of the below command.

rpm -q docker-ce

3. Start docker service :- Now you can see Docker has been successfully installed on the top of Redhat . So, start the docker services using the below command.

systemctl start docker

After starting the service just check the docker status that it is running or not, using the below-mentioned command.

systemctl status docker
No alt text provided for this image

Now you can see in the above screenshot that Docker has a running state now. But when you will shut down your system and restart again. At that time, you have to restart the docker services again using systemctl start docker .So if you want that you have not to do the same thing again & again then run the below command for enabling docker services permanently.

systemctl enable docker

Now Docker installation has done. So we can use all the docker services as you want.

Step-2 Download docker images :-

  • Docker require docker images to launch docker container. When we download docker image , it by default download from the docker hub.
  • To pull the docker images from the Docker-Hub:
docker pull imagename:imageversion
ex:- docker pull centos:7
No alt text provided for this image

To see all downloaded docker images by:-

docker images
No alt text provided for this image

Step-3 Launch/Run docker container :-

To launch the docker container , we using the below command.

docker run -it --name container_name imagename:imageversion
ex:- docker run -it --name webserver  centos:7
No alt text provided for this image
  • Now container has been successfully launched and also you can see in the above screenshot ,before launching the container you are seeing root@localhost but after running the above command root@dd651b28d5fd taken place.
  • Both are different operating system. The first is our base os and second is our docker container.

Now the container is ready for the configuration of the Webserver .

Step-4 Configure Apache Web-server on docker container :-

No alt text provided for this image

There are mainly three steps to configure the apache web-server :-

a. Install the Server Program

b. Configuration of Server

c. Start the server

Step-a :- Install httpd software

In docker container , yum is pre- configured . So we can directly install httpd software.

yum install httpd
No alt text provided for this image

Step-b Configure web-server :-

  • Every server has their specific document root from where they read webpages.
  • Similarly in apache web-server , the document root is /var/www/html/
  • So we put our webpage at /var/www/html/

I create a file /var/www/html/lw.html

No alt text provided for this image

Step-c Start webserver services:-

  • In docker container , if you want to start the http services by systemctl , then it will failed because container doesn’t provide the “systemctl” command inside docker container.
  • Every program call some other program behind the scene . Similarly when we start service using systemctl ,it also call some other command . So we directly run that command to start httpd service in docker container.
  • The command to start httped service inside docker container is :-
/usr/sbin/httpd
No alt text provided for this image

Now we can verify by our base os . when we put container ip and page name ,then we can see our content of page.

 To check the container IP you have install the net-tools inside container using “yum install net-tools -y”.

No alt text provided for this image

And then "ifconfig" command is used to see container ip.

No alt text provided for this image

So 172.17.0.2:/lw.html put in firefox

No alt text provided for this image

This is possible because ,by default docker container and base os has connectivity .Hence we successfully configured apache web-server on docker container.

Step-5 Run python code inside container:-

  • Let’s see how we can set up the python environment as well as how python works on the docker container. But before setting up the python environment you have to launch a docker container first .
  • To run python code inside docker container , we download python interpreter inside docker container .
yum install python3  -y
No alt text provided for this image

After installing python interpreter , we create any python code to see our python interpreter is working or not.

No alt text provided for this image

I have written a small code inside my test.py file. Now I run my file by :-

python3 test.py
No alt text provided for this image

Our code is working fine . So in this way , we can set our python environment to run any python code inside the docker container.

Thanks

要查看或添加评论,请登录

Deepak Sharma的更多文章

  • Jenkins Dynamic Provisioning

    Jenkins Dynamic Provisioning

    Objectives In this article , We will see how we can create dynamic slave on the fly when job come and attach to the…

    1 条评论
  • OSPF Routing Protocol using Dijkstra Algorithm

    OSPF Routing Protocol using Dijkstra Algorithm

    Objectives:- In this article, We will learn about Dijkstra Algorithm and Open Short Path First(OSPF) Routing Protocol .…

    1 条评论
  • MongoDB Case study: Forbes

    MongoDB Case study: Forbes

    Objective In this article , we see how MongoDB Cloud Migration Helps World's Biggest Media Brand Continue To Set…

  • Vehicle’s Number Plate Detection using CNN model using python and Flask API…

    Vehicle’s Number Plate Detection using CNN model using python and Flask API…

    In this article, I am going to show you how you can create CNN Model or Deep Learning Model for Vehicle’s Number Plate…

    8 条评论
  • K-means Clustering and its real use cases in security domain

    K-means Clustering and its real use cases in security domain

    Objectives:- In this article, we will see about the Kmean algorithm and how Kmean algorithm helps in security domain to…

  • JavaScript:- Industry Use-cases

    JavaScript:- Industry Use-cases

    Objective In this article , we will learn about the JavaScript and the use-cases of JavaScript. How Industries utilizes…

  • Confusion Matrix and Cyber Security

    Confusion Matrix and Cyber Security

    Objectives:- In this article , we will see about confusion matrix and the use of confusion matrix . Also we see how…

  • Self-Reflection of MongoDB-Workshop

    Self-Reflection of MongoDB-Workshop

    # Day1 (1st May 2021) ?? Introduction of the file system? ??The data we will stored in file and that file we basically…

  • OpenShift case study:- Cisco

    OpenShift case study:- Cisco

    Cisco’s success depends on its ability to quickly deliver innovative IT products and solutions to customers. Delays can…

  • Industry Use cases of Jenkins:- Prepl

    Industry Use cases of Jenkins:- Prepl

    In 2021, When industries are running towards automation, adopting different DevOps tools to solve their industrial…

社区洞察

其他会员也浏览了