1
0
mirror of https://github.com/newnius/YAO-agent.git synced 2025-06-07 22:01:55 +00:00

update, support network alias

This commit is contained in:
Newnius 2019-06-13 17:35:48 +08:00
parent f1ec22bc8e
commit 84dac98ccc
2 changed files with 53 additions and 13 deletions

View File

@ -83,23 +83,35 @@ class MyHandler(BaseHTTPRequestHandler):
docker_workspace = form.getvalue('workspace') docker_workspace = form.getvalue('workspace')
docker_gpus = form.getvalue('gpus') docker_gpus = form.getvalue('gpus')
docker_mem_limit = form.getvalue('mem_limit') docker_mem_limit = form.getvalue('mem_limit')
docker_cpu_limit = form.getvalue('cpu_limit') docker_cpu_limit = int(form.getvalue('cpu_limit'))
docker_network = form.getvalue('network') docker_network = form.getvalue('network')
try: try:
client = docker.from_env() client = docker.APIClient(base_url='unix://var/run/docker.sock')
container = client.containers.run(
name=docker_name, host_config = client.create_host_config(
hostname=docker_name, mem_limit=docker_mem_limit,
cpu_shares=docker_cpu_limit * 1024
)
networking_config = client.create_networking_config(
endpoints_config={
docker_network: client.create_endpoint_config(
aliases=[docker_name],
)
}
)
container = client.create_container(
image=docker_image, image=docker_image,
command=docker_cmd, command=docker_cmd,
mem_limit=docker_mem_limit, hostname=docker_name,
nano_cpus=docker_cpu_limit, detach=True,
network=docker_network, host_config=host_config,
environment={"repo": docker_workspace, "NVIDIA_VISIBLE_DEVICES": docker_gpus}, environment={"repo": docker_workspace, "NVIDIA_VISIBLE_DEVICES": docker_gpus},
runtime="nvidia", networking_config=networking_config,
detach=True runtime='nvidia'
) )
client.start(container)
msg = {"code": 0, "id": container.id} msg = {"code": 0, "id": container.id}
except Exception as e: except Exception as e:
msg = {"code": 1, "error": str(e)} msg = {"code": 1, "error": str(e)}

34
test.py
View File

@ -69,8 +69,36 @@ def remove_network():
client.networks.prune(filters={'name': 'yao-net-1024'}) client.networks.prune(filters={'name': 'yao-net-1024'})
#create_network() def create_container():
#list_networks() client = docker.APIClient(base_url='unix://var/run/docker.sock')
#remove_network() host_config = client.create_host_config(
mem_limit='512m',
cpu_shares=1 * 1024
)
networking_config = client.create_networking_config(
endpoints_config={
'yao-net-1201': client.create_endpoint_config(
aliases=['node1'],
)
}
)
container = client.create_container(
image='alpine',
command='pwd',
hostname='node1',
detach=True,
host_config=host_config,
environment = {"repo": docker_workspace, "NVIDIA_VISIBLE_DEVICES": docker_gpus},
networking_config=networking_config,
runtime='nvidia'
)
client.start(container)
# create_network()
# list_networks()
# remove_network()
get_status('af121babda9b') get_status('af121babda9b')