2025-01-20 00:32:25 +01:00
|
|
|
import docker
|
|
|
|
|
2025-01-20 18:12:01 +01:00
|
|
|
def launch_docker_container(image, run_command, name, ports):
|
2025-01-20 00:32:25 +01:00
|
|
|
client = docker.from_env()
|
|
|
|
try:
|
|
|
|
container = client.containers.run(
|
|
|
|
name=name,
|
2025-01-20 18:12:01 +01:00
|
|
|
image=image,
|
|
|
|
# ports=ports,
|
|
|
|
command=run_command,
|
|
|
|
detach=True,
|
|
|
|
network_mode='host'
|
2025-01-20 00:32:25 +01:00
|
|
|
)
|
|
|
|
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}"
|