File size: 1,196 Bytes
65d3b67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse, HTMLResponse
import docker
from docker import client

router = APIRouter()

docker_client = docker.from_env()

@router.get("/start/{container_name}")
def start_container(container_name: str):
    try:
        container = client.containers.get(container_name)
        container.unpause()
        return JSONResponse({"status": "started"})
    except Exception as e:
        return JSONResponse({"error": str(e)}, status_code=500)

@router.get("/pause/{container_name}")
def pause_container(container_name: str):
    try:
        container = client.containers.get(container_name)
        container.pause()
        return JSONResponse({"status": "paused"})
    except Exception as e:
        return JSONResponse({"error": str(e)}, status_code=500)

@router.get("/stop/{container_name}")
def stop_container(container_name: str):
    try:
        container = client.containers.get(container_name)
        container.stop()
        container.remove(force=True)
        return JSONResponse({"status": "stopped and removed"})
    except Exception as e:
        return JSONResponse({"error": str(e)}, status_code=500)