Python Menu-Based Program: Managing Docker Engine

Piyush Panchariya
3 min readJul 31, 2023

--

The Python Tech Menu Program

Overview

The Python Tech Menu program is designed to provide users with a user-friendly interface to interact with different technologies and perform various operations. The menu-driven structure makes it easy for users to choose the technology they want to work with and then select the desired operation within that technology.

Docker Operations

The Docker Operations option enables users to work with Docker containers, which are lightweight and portable environments for running applications. The following operations are available under this option:

2.1 List Docker Containers

This operation lists all the Docker containers currently running on the system. Containers are instances of Docker images that execute applications in isolated environments.

2.2 Run a Docker Container

Users can run a Docker container using this operation. They need to provide the container name and the image name (the image from which the container is created).

2.3 Stop a Docker Container

This operation stops a specific Docker container based on its container name. Stopping a container halts its execution.

2.4 Remove a Docker Container

This operation removes a Docker container from the system based on its container name. Removing a container frees up system resources.

#!/usr/bin/python


print("Welcome to my Menu Program")

import os
import subprocess as sub
comd=sub.getoutput("zenity --entry --text=Enter-The-Command --title=Welcome-To-My-Menu-Program")
p=sub.getoutput(comd)
print("{}\n".format(p))


choice=int(input("Press 1. If you want to access the Docker Engine..."))


def do_while(x):
y=0
while(y<=2):
if y<=0:

if choice==1:
typ=int(input("************************************************\n1.List the Running Containers \n2.List the All container(running or stopped \n3.Create a new continer \n4.Want to get into any conainer(container must be in running state) \n5.Want to start or stop any continer \n6.List all the images we currently have \n7.Exit from the Docker Command \nEnter The Choice you want to execute: "))
if typ==1:
cmd="docker ps"
if typ==2:
cmd="docker ps -a"
if typ==3:
name=input("enter the name of continer: ")
image=input("enter the Image name of the continer: ")
d_a=input("do you want to get the terminal or not(y/n): ")
if (d_a=="y" or d_a=="Y"):
cmd="docker run -it --name={} {}".format(name,image)
if (d_a=="n" or d_a=="N"):
cmd="docker run -itd --name={} {}".format(name,image)
if typ==4:
c_n=input("Enter name of any running container you want to get into: ")
cmd="docker attach {}".format(c_n)
if typ==5:
c_ns=input("Enter name of continer you want to start/stop: ")
stp=int(input("1.start\n2.stop\nEnter the choice: "))
if stp==1:
cmd="docker start {}".format(c_ns)
if stp==2:
cmd="docker stop {}".format(c_ns)
else:
print("Wrong input...")
if typ==6:
cmd="docker images"
if typ==7:
x=2
print("^^^^^^^^^^^^^^^^^^^^^^\nThanks for you using my docker engine")
break
y=1
t=sub.getoutput(cmd)
print(t)

else:
while (x==1):
if choice==1:
typ=int(input("********************************\n1.List the Running Containers \n2.List the All container(running or stopped \n3.Create a new continer \n4.Want to get into any conainer(container must be in running state) \n5.Want to start or stop any continer \n6.List all the images we currently have \n7.Exit from the Docker Command \nEnter The Choice you want to execute: "))
if typ==1:
cmd="docker ps"
if typ==2:
cmd="docker ps -a"
if typ==3:
name=input("enter the name of continer: ")
image=input("enter the Image name of the continer: ")
d_a=input("do you want to get the terminal or not(y/n): ")
if (d_a=="y" or d_a=="Y"):
cmd="docker run -it --name={} {}".format(name,image)
if (d_a==n or d_a==N):
cmd="docker run -itd --name={} {}".format(name,image)
if typ==4:
c_n=input("Enter name of any running container you want to get into: ")
cmd="docker attach {}".format(c_n)
if typ==5:
c_ns=("Enter name of continer you want to start/stop: ")
stp=int(input("1.start\n2.stop\nEnter the choice: "))
if stp==1:
cmd="docker start {}".format(c_ns)
if stp==2:
cmd="docker stop {}".format(c_ns)
else:
print("Wrong input...")
if typ==6:
cmd="docker images"
if typ==7:
x=2
print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nThanks for using my docker engine")
return t
break
t=sub.getoutput(cmd)
print(t)

y = y + 1

if choice==1:
mail_ch=input("Do you want you last docker command at your mail id(y/n): ")
if (mail_ch=="n" or mail_ch=="N"):
t=do_while(choice)
if (mail_ch=="y" or mail_ch=="Y"):
mail_id=input("enter your mail id(gmail only): ")
t=do_while(choice)

import smtplib as s
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email import encoders

sender= "piyush007panchariya@gmail.com"
receiver=mail_id
password="************"
subject="It's output of your docker command..."
email_body="<h1>{}*Piyush_Panchariya*</h1>".format(t)

message= MIMEMultipart()
message['From']=sender
message['To']=receiver
message['Subject'] =subject
message.attach(MIMEText(email_body, 'html'))

server=s.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(sender,password)
text = message.as_string()
server.sendmail(sender, receiver, text)

print('\n\n==========================\noutput of your last docker command send to your mail id successfully....\n\n')
server.quit()

else:
print("$$$$$$$$$$$$$$$$$$$$\nthanks for using my menu program...")

--

--