Creating a Docker web page to run docker commands

Garvit Garg
2 min readAug 11, 2021

To create a web application that can run users all docker command we need to integrate javascript with docker for which go through these steps :-

  1. Creating a front end
  2. Creating backend.

Let’s start by installing httpd by

CMD : dnf install httpd

CMD : systemctl diable firewalled

CMD : systemctl start httpd

These Command will install and run the httpd on your linux system + disable the firewall. Once you have started httpd open the file- /var/www/html/ to write the front end code.

Creating a front end :-

Open file- /var/www/html/ to write front end code.

CMD : cd var/www/html

CMD : gedit (File_name).html

Here we will write the script that will connect our server to OS + front end html code.

HTML code varies from person to person but the common script is given below :

<script>
function (file_Name)()
{

var xhr=new XMLHttpRequest();
i=document.getElementById("in1").value;

xhr.open("GET","http://(IP_Address)/cgi-bin/(File_name)).py?x="+i,true);
xhr.send();

xhr.onload=function (){
var output=xhr.responseText;
document.getElementById("d1").innerHTML=output;
}
}

</script>

Now we have our front end and script ready.
The only work left is to write our backend common python code.

Creating backend :-

Now to write backend python code open the file- /var/www/cgi-bin

CMD : cd var/www/cgi-bin

CMD : gedit (File_Name).py

Above code will create a .py extension file at the specified location in httpd server.

Open the file and write the code.


import cgi
import subprocess
import time

print("content-type: text/html")
print()

print("Backend Work")
print()
f=cgi.FieldStorage()
cmd=f.getvalue("x")
o=subprocess.getoutput("sudo "+ cmd)
print(o)se

Now our both the front end and back end are ready.
But accessible only for the root user.

If we look at my website :-

Tapping on lets start we will get the portal to run our command :

Thank You !

--

--