feat:
--ADDED DOCKER API
This commit is contained in:
1 parent
c77806b367
commit
c3ca0689f0
3 files changed
+77
-10
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import psutil
|
||||
import asyncio
|
||||
from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
||||
from fastapi import FastAPI, WebSocket, WebSocketDisconnect,HTTPException
|
||||
import docker
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@@ -84,6 +86,49 @@ async def hardware_status_monitor(websocket: WebSocket):
|
||||
|
||||
print("Hardware monitoring WebSocket disconnected")
|
||||
|
||||
|
||||
# GET DOCKER CONTAINERS
|
||||
@app.get("/docker/containers")
|
||||
def get_containers():
|
||||
|
||||
try:
|
||||
# Connect to the Docker daemon running on this VM
|
||||
client = docker.from_env()
|
||||
|
||||
# all=True means:
|
||||
# running + stopped + exited containers
|
||||
containers = client.containers.list(all=True)
|
||||
|
||||
result = []
|
||||
|
||||
for container in containers:
|
||||
|
||||
result.append({
|
||||
"id": container.short_id,
|
||||
"name": container.name,
|
||||
"image": (
|
||||
container.image.tags[0]
|
||||
if container.image.tags
|
||||
else container.image.short_id
|
||||
),
|
||||
"status": container.status,
|
||||
"ports": container.attrs[
|
||||
"NetworkSettings"
|
||||
]["Ports"]
|
||||
})
|
||||
|
||||
return {
|
||||
"count": len(result),
|
||||
"containers": result
|
||||
}
|
||||
|
||||
except docker.errors.DockerException as e:
|
||||
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=f"Docker error: {str(e)}"
|
||||
)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
import uvicorn
|
||||
|
||||
Reference in new issue
Block a user