Pratyush Desai
10592cb2d2
Connected to hld2m-server successfully. Hardcoded `network_mode='host'` Which isn't viable moving forwards. Current Status: Frontend : Active Servers show Backend: Have configured admin.py utils.py and models.py to grant us the ability to launch a docker image with a command and tested client connection. Server Online status works launch, stop, remove (with force stop) works. Signed-off-by: Pratyush Desai <pratyush.desai@liberta.casa>
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
import docker
|
|
|
|
def launch_docker_container(image, run_command, name, ports):
|
|
client = docker.from_env()
|
|
try:
|
|
container = client.containers.run(
|
|
name=name,
|
|
image=image,
|
|
# ports=ports,
|
|
command=run_command,
|
|
detach=True,
|
|
network_mode='host'
|
|
)
|
|
return f"Container launched successfully: {container.id}"
|
|
except docker.errors.APIError as e:
|
|
return f"API Error: {e}"
|
|
except Exception as e:
|
|
return f"Error: {e}"
|
|
|
|
def stop_docker_container(name):
|
|
client = docker.from_env()
|
|
try:
|
|
container = client.containers.get(name)
|
|
container.stop()
|
|
return f"Container stopped: {name}"
|
|
except docker.errors.NotFound:
|
|
return f"Container not found: {name}"
|
|
except Exception as e:
|
|
return f"Error stopping container {name}: {e}"
|
|
|
|
def remove_docker_container(name):
|
|
client = docker.from_env()
|
|
try:
|
|
container = client.containers.get(name)
|
|
container.remove(force=True) # Force removal if the container is running
|
|
return f"Container removed: {name}"
|
|
except docker.errors.NotFound:
|
|
return f"Container not found: {name}"
|
|
except Exception as e:
|
|
return f"Error removing container {name}: {e}"
|
|
|
|
def is_container_running(name):
|
|
client = docker.from_env()
|
|
try:
|
|
container = client.containers.get(name)
|
|
return container.status == "running"
|
|
except docker.errors.NotFound:
|
|
return False
|
|
except Exception as e:
|
|
return f"Error checking container {name}: {e}"
|