Docker Task: Setting Up HTTPD server and Run Python Program On Docker Container…

Kaustubh Chaudhari
4 min readMar 14, 2021

--

What is Docker ?

Docker is the Container Engine used to launch Container OS . These Os are Light weight and can be installed, configure and Boot within a second. Using Docker we can save time and resources of our company…

Prerequisites :=

  1. You must have Docker installed on your system.
  2. You may use your own image or pull image from Docker Hub.

Command to do so is: “docker pull centos:latest” . I am using Centos image for this use case.

image pulled successfully…

check images : “docker images

3. If you are using Docker you have to enable ingress and egress which is called Masquerade and enable Firewall for port Number 80 and 443.

Following are commands you can use in Base System:

firewalld-cmd — zone=public — add-masquerade — permanent

firewalld-cmd — zone=public — add-port=80/tcp

firewalld-cmd — zone=public — add-port=443/tcp

firewalld — reload

systemctl restart docker

systemctl status docker

Now, Run one Docker Container to setup our Task .

docker run -it centos:latest

Steps to Setup HTTPD server :

  1. Install HTTPD inside Container.

yum install httpd -y

2. Go to /var/www/html and create index.html file there . Inside file Write Some Html code:

cd /var/www/html

vi inex.html

3. Start HTTPD services:

Docker does not support Systemctl command. So we have to find different way to start our services. We can run “/usr/sbin/httpd” command to start HTTPD services in Docker.

4. Exit from the Container and cereate our Container Image :

exit

commit <container id/name> <image name>:<tag>

eg: commit myos1 myweb:v1

5. Expose the Container while running new container…

We have to expose the Container with PAT(Port Address Translation) so that Outside World can see the Page …

To do PAT we use option ‘p’ while running Container.

docker run -it -p 8080:80 myweb:v1

I have exposed Port Number 80 to Port Number 8080 . Now anyone who will come to port Number 8080 can see my page…

6. Get IP of the Container and Base system…

  • Here are two Possibilities :
  • If you use Base System or any System which is in LAN with Base System you can use Docker IP:Port Number to access the page like :=
Install net-tools as ifconfig not available
Getting IP of Docker Container

http://172.17.0.2:80

  • And if you use Windows or any other system which is not in LAN then, you have to use Base System IP:Exposed Port Number to access

http://192.168.1.5:8080

Final Output

This is the Final Output we get….

Steps to Setup Python Interpreter To Run Python Program :

  1. Install python in Conatiner.

yum install python3

2. Create a python code hello.py:

vi hello.py

3. Run Python Code:

python3 hello.py

Code is run successfully…

So, guys that’s it from my side…

Hope you learnt something new today …

Do tell me How’s my Content and Like Share and Follow me.

Stay tuned For more Content…

Thank you !!!

--

--

No responses yet