Docker2X / app.py
katsukiai's picture
Update app.py
efd2c82 verified
import gradio as gr
import docker
import os
from docker.errors import DockerException, BuildError, APIError
# Docker client setup
client = docker.from_env()
# Function to build Docker image with status update and error handling
def build_docker_image(dockerfile, context_directory, api_key, status_callback):
try:
if api_key != os.getenv("DOCKER_API_KEY"):
return "Invalid API Key"
status_callback("Building image...")
image, logs = client.images.build(path=context_directory, dockerfile=dockerfile, rm=True)
status_callback(f"Image built successfully: {image.tags[0]}")
return f"Image built successfully: {image.tags[0]}"
except BuildError as e:
status_callback(f"Build error: {e}")
return f"Build error: {e}"
except APIError as e:
status_callback(f"Docker API error: {e}")
return f"Docker API error: {e}"
except Exception as e:
status_callback(f"Unexpected error: {e}")
return f"Unexpected error: {e}"
# Function to pull an image from Docker Hub with error handling
def pull_image_from_dockerhub(image_name, api_key, status_callback):
try:
if api_key != os.getenv("DOCKER_API_KEY"):
return "Invalid API Key"
status_callback(f"Pulling image: {image_name}...")
image = client.images.pull(image_name)
status_callback(f"Image {image_name} pulled successfully: {image.id}")
return f"Image {image_name} pulled successfully: {image.id}"
except APIError as e:
status_callback(f"Docker API error: {e}")
return f"Docker API error: {e}"
except Exception as e:
status_callback(f"Unexpected error: {e}")
return f"Unexpected error: {e}"
# Function to start a container with error handling
def start_container(image_name, status_callback):
try:
status_callback(f"Starting container with image: {image_name}...")
container = client.containers.run(image_name, detach=True)
status_callback(f"Container {container.id} started successfully")
return f"Container {container.id} started successfully"
except DockerException as e:
status_callback(f"Docker error: {e}")
return f"Docker error: {e}"
except Exception as e:
status_callback(f"Unexpected error: {e}")
return f"Unexpected error: {e}"
# Function to stop a container with error handling
def stop_container(container_id, status_callback):
try:
status_callback(f"Stopping container {container_id}...")
container = client.containers.get(container_id)
container.stop()
status_callback(f"Container {container.id} stopped successfully")
return f"Container {container.id} stopped successfully"
except docker.errors.NotFound as e:
status_callback(f"Error: Container {container_id} not found")
return f"Error: Container {container_id} not found"
except DockerException as e:
status_callback(f"Docker error: {e}")
return f"Docker error: {e}"
except Exception as e:
status_callback(f"Unexpected error: {e}")
return f"Unexpected error: {e}"
# Tab 1 - Build Docker Image with Status Visualization
def docker_build_tab():
with gr.Column():
dockerfile_input = gr.Textbox(label="Dockerfile Content", lines=10)
context_directory_input = gr.Textbox(label="Context Directory", placeholder="Enter directory path", lines=1)
api_key_input = gr.Textbox(label="Docker API Key", type="password")
build_button = gr.Button("Build Image")
build_output = gr.Textbox(label="Build Output", interactive=False)
status_output = gr.Textbox(label="Status", interactive=False)
build_button.click(build_docker_image, inputs=[dockerfile_input, context_directory_input, api_key_input, status_output], outputs=build_output)
return dockerfile_input, context_directory_input, api_key_input, build_button, build_output, status_output
# Tab 2 - Pull Docker Image from Docker Hub with Status Visualization
def docker_pull_tab():
with gr.Column():
image_name_input = gr.Textbox(label="Docker Image (e.g., python:3.8)", lines=1)
api_key_input = gr.Textbox(label="Docker API Key", type="password")
pull_button = gr.Button("Pull Image")
pull_output = gr.Textbox(label="Pull Output", interactive=False)
status_output = gr.Textbox(label="Status", interactive=False)
pull_button.click(pull_image_from_dockerhub, inputs=[image_name_input, api_key_input, status_output], outputs=pull_output)
return image_name_input, api_key_input, pull_button, pull_output, status_output
# Tab 3 - Container Management with Status Visualization
def container_management_tab():
with gr.Column():
start_image_input = gr.Textbox(label="Image Name", lines=1)
start_button = gr.Button("Start Container")
start_output = gr.Textbox(label="Start Output", interactive=False)
status_output = gr.Textbox(label="Status", interactive=False)
stop_container_input = gr.Textbox(label="Container ID to Stop", lines=1)
stop_button = gr.Button("Stop Container")
stop_output = gr.Textbox(label="Stop Output", interactive=False)
start_button.click(start_container, inputs=[start_image_input, status_output], outputs=start_output)
stop_button.click(stop_container, inputs=[stop_container_input, status_output], outputs=stop_output)
return start_image_input, start_button, start_output, stop_container_input, stop_button, stop_output, status_output
# Tab 4 - Code Editor
def code_editor_tab():
with gr.Column():
code_input = gr.Textbox(label="Code Editor", lines=15, interactive=True)
save_button = gr.Button("Save Code")
save_output = gr.Textbox(label="Save Output", interactive=False)
save_button.click(lambda code: "Code saved successfully" if code else "No code to save", inputs=code_input, outputs=save_output)
return code_input, save_button, save_output
# Gradio Interface Setup
def main():
with gr.Blocks() as demo:
gr.Markdown("### Docker Management Gradio App")
with gr.Tabs():
with gr.TabItem("Build Docker Image"):
docker_build_tab()
with gr.TabItem("Pull Docker Image from Docker Hub"):
docker_pull_tab()
with gr.TabItem("Container Management"):
container_management_tab()
with gr.TabItem("Code Editor"):
code_editor_tab()
demo.launch()
if __name__ == "__main__":
main()