Configure Webserver And Python Interpreter Inside Docker Container
Deepak Sharma
3 x RedHat Certified Engineer (EX200, EX294, EX180) || DevOps Engineer || Docker || K8s || Ansible || Linux || Git || Github || Gitlab || Terraform || Jenkins || Cloud Computing || AWS
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
- 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 .
- 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.
- 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/
We can verify yum configuration by :-
yum repolist
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
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
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
To see all downloaded docker images by:-
docker images
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
- 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 :-
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
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
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
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”.
And then "ifconfig" command is used to see container ip.
So 172.17.0.2:/lw.html put in firefox
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
After installing python interpreter , we create any python code to see our python interpreter is working or not.
I have written a small code inside my test.py file. Now I run my file by :-
python3 test.py
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