-
Notifications
You must be signed in to change notification settings - Fork 0
/
DockerContainer.py
57 lines (44 loc) · 2.16 KB
/
DockerContainer.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
__author__ = 'tlindener'
import docker
import subprocess
from OpenVSwitch import OpenVSwitch
class DockerContainer(object):
StartedContainer = []
def __init__(self, dockerfilepath, tag):
self.docker = docker.Client(base_url='unix://var/run/docker.sock',
version='1.9',
timeout=10)
self.Dockerfilepath = dockerfilepath
self.Tag = tag
self._ContainerPid = None
@property
def ContainerPid(self):
return self._ContainerPid
@ContainerPid.setter
def ContainerPid(self, value):
print(value)
self._ContainerPid = value
def build(self):
self.DockerBuildResult = self.docker.build(path=self.Dockerfilepath, tag=self.Tag, quiet=False, fileobj=None,
nocache=False,
rm=True, stream=False, timeout=None)
def create(self,initcommand,ports):
if not initcommand:
initcommand = None
self.DockerCreateResult = self.docker.create_container(self.Tag, command=initcommand, hostname=None, user=None,
detach=False, stdin_open=False, tty=False, mem_limit=0,
ports=ports, environment=None, dns=None, volumes=None,
volumes_from=None, network_disabled=False, name=None,
entrypoint=None, cpu_shares=None, working_dir=None)
return self.DockerCreateResult.get("Id")
def run(self,portbindings):
self.docker.start(self.DockerCreateResult,privileged=True ,port_bindings=portbindings)
result = self.docker.inspect_container(self.DockerCreateResult)
self.ContainerPid = result.get("State").get("Pid")
netnspath = "/var/run/netns/%d" % self.ContainerPid
procpath = "/proc/%d/ns/net" % self.ContainerPid
subprocess.call(["mkdir","/var/run/netns"])
subprocess.call(["rm","-f",netnspath])
subprocess.call(["ln","-s",procpath,netnspath])
def kill(self):
self.docker.kill(self.DockerCreateResult.get("Id"))