mirror of https://github.com/docker/docker-py.git
Methods that previously supported multi-args are now single-argument. Doc and tests updated accordingly.
This commit is contained in:
parent
58bc2be5a5
commit
c8e5a6dab1
45
README.md
45
README.md
|
@ -21,6 +21,9 @@ Identical to the `docker commit` command.
|
||||||
* `c.containers(quiet=False, all=False, trunc=True, latest=False, since=None, before=None, limit=-1)`
|
* `c.containers(quiet=False, all=False, trunc=True, latest=False, since=None, before=None, limit=-1)`
|
||||||
Identical to the `docker ps` command.
|
Identical to the `docker ps` command.
|
||||||
|
|
||||||
|
* `c.copy(container, resource)`
|
||||||
|
Identical to the `docker cp` command.
|
||||||
|
|
||||||
* `c.create_container(image, command, hostname=None, user=None, detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None, environment=None, dns=None, volumes=None, volumes_from=None, privileged=False)`
|
* `c.create_container(image, command, hostname=None, user=None, detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None, environment=None, dns=None, volumes=None, volumes_from=None, privileged=False)`
|
||||||
Creates a container that can then be `start`ed. Parameters are similar to those
|
Creates a container that can then be `start`ed. Parameters are similar to those
|
||||||
for the `docker run` command except it doesn't support the attach options
|
for the `docker run` command except it doesn't support the attach options
|
||||||
|
@ -51,13 +54,15 @@ Identical to the `docker info` command.
|
||||||
Identical to the `docker insert` command.
|
Identical to the `docker insert` command.
|
||||||
|
|
||||||
* `c.inspect_container(container_id)`
|
* `c.inspect_container(container_id)`
|
||||||
Identical to the `docker inspect` command, but can only be used with a container ID.
|
Identical to the `docker inspect` command, but can only be used with a
|
||||||
|
container ID.
|
||||||
|
|
||||||
* `c.inspect_image(container_id)`
|
* `c.inspect_image(container_id)`
|
||||||
Identical to the `docker inspect` command, but can only be used with an image ID.
|
Identical to the `docker inspect` command, but can only be used with an
|
||||||
|
image ID.
|
||||||
|
|
||||||
* `c.kill(containers...)`
|
* `c.kill(container)`
|
||||||
Identical to the `docker kill` command.
|
Kill a container. Similar to the `docker kill` command.
|
||||||
|
|
||||||
* `c.login(username, password=None, email=None)`
|
* `c.login(username, password=None, email=None)`
|
||||||
Identical to the `docker login` command (but non-interactive, obviously).
|
Identical to the `docker login` command (but non-interactive, obviously).
|
||||||
|
@ -74,37 +79,39 @@ Identical to the `docker pull` command.
|
||||||
* `c.push(repository)`
|
* `c.push(repository)`
|
||||||
Identical to the `docker push` command.
|
Identical to the `docker push` command.
|
||||||
|
|
||||||
* `c.remove_container(containers..., v=False)`
|
* `c.remove_container(container, v=False)`
|
||||||
Identical to the `docker rm` command.
|
Remove a container. Similar to the `docker rm` command.
|
||||||
|
|
||||||
* `c.remove_image(images...)`
|
* `c.remove_image(image)`
|
||||||
Identical to the `docker rmi` command.
|
Remove an image. Similar to the `docker rmi` command.
|
||||||
|
|
||||||
* `c.restart(containers..., t=10)`
|
* `c.restart(container, timeout=10)`
|
||||||
Identical to the `docker restart` command.
|
Restart a container. Similar to the `docker restart` command.
|
||||||
|
|
||||||
* `c.search(term)`
|
* `c.search(term)`
|
||||||
Identical to the `docker search` command.
|
Identical to the `docker search` command.
|
||||||
|
|
||||||
* `c.start(container)`
|
* `c.start(container, binds=None)`
|
||||||
Identical to the `docker start` command, but doesn't support attach options.
|
Identical to the `docker start` command, but doesn't support attach options.
|
||||||
Use `docker logs` to recover `stdout`/`stderr`
|
Use `docker logs` to recover `stdout`/`stderr`
|
||||||
|
`binds` Allows to bind a directory in the host to the container.
|
||||||
* `c.start(container, binds={'/host': '/mnt'})`
|
Similar to the `docker run` command with option `-b="/host:/mnt"`.
|
||||||
Allows to bind a directory in the host to the container.
|
|
||||||
Similar to the `docker run` command with the `-b="/host:/mnt"`.
|
|
||||||
Requires the container to be created with the volumes argument:
|
Requires the container to be created with the volumes argument:
|
||||||
`c.create_container(..., volumes={'/mnt': {}})`
|
`c.create_container(..., volumes={'/mnt': {}})`
|
||||||
|
|
||||||
* `c.stop(containers..., t=10)`
|
* `c.stop(containers, timeout=10)`
|
||||||
Identical to the `docker stop` command.
|
Stops a container. Similar to the `docker stop` command.
|
||||||
|
|
||||||
* `c.tag(image, repository, tag=None, force=False)`
|
* `c.tag(image, repository, tag=None, force=False)`
|
||||||
Identical to the `docker tag` command.
|
Identical to the `docker tag` command.
|
||||||
|
|
||||||
|
* `c.top(container_id)`
|
||||||
|
Identical to the `docker top` command.
|
||||||
|
|
||||||
* `c.version()`
|
* `c.version()`
|
||||||
Identical to the `docker version` command.
|
Identical to the `docker version` command.
|
||||||
|
|
||||||
* `c.wait(containers...)`
|
* `c.wait(containers)`
|
||||||
Identical to the `docker wait` command.
|
Wait for a container and return its exit code. Similar to the `docker wait`
|
||||||
|
command.
|
||||||
|
|
||||||
|
|
100
docker/client.py
100
docker/client.py
|
@ -200,6 +200,13 @@ class Client(requests.Session):
|
||||||
u = self._url("/containers/ps")
|
u = self._url("/containers/ps")
|
||||||
return self._result(self.get(u, params=params), True)
|
return self._result(self.get(u, params=params), True)
|
||||||
|
|
||||||
|
def copy(self, container, resource):
|
||||||
|
res = self._post_json(self._url("/containers/{0}/copy".format(container)),
|
||||||
|
{"Resource": resource},
|
||||||
|
stream=True)
|
||||||
|
self._raise_for_status(res)
|
||||||
|
return res.raw
|
||||||
|
|
||||||
def create_container(self, image, command, hostname=None, user=None,
|
def create_container(self, image, command, hostname=None, user=None,
|
||||||
detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None,
|
detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None,
|
||||||
environment=None, dns=None, volumes=None, volumes_from=None,
|
environment=None, dns=None, volumes=None, volumes_from=None,
|
||||||
|
@ -217,13 +224,6 @@ class Client(requests.Session):
|
||||||
"image first.".format(config['Image']))
|
"image first.".format(config['Image']))
|
||||||
return self._result(res, True)
|
return self._result(res, True)
|
||||||
|
|
||||||
def copy(self, container, resource):
|
|
||||||
res = self._post_json(self._url("/containers/{0}/copy".format(container)),
|
|
||||||
{"Resource": resource},
|
|
||||||
stream=True)
|
|
||||||
self._raise_for_status(res)
|
|
||||||
return res.raw
|
|
||||||
|
|
||||||
def diff(self, container):
|
def diff(self, container):
|
||||||
return self._result(self.get(self._url("/containers/{0}/changes".
|
return self._result(self.get(self._url("/containers/{0}/changes".
|
||||||
format(container))), True)
|
format(container))), True)
|
||||||
|
@ -284,11 +284,10 @@ class Client(requests.Session):
|
||||||
return self._result(self.get(self._url("/images/{0}/json".
|
return self._result(self.get(self._url("/images/{0}/json".
|
||||||
format(image_id))), True)
|
format(image_id))), True)
|
||||||
|
|
||||||
def kill(self, *args):
|
def kill(self, container):
|
||||||
for name in args:
|
url = self._url("/containers/{0}/kill".format(container))
|
||||||
url = self._url("/containers/{0}/kill".format(name))
|
res = self.post(url, None)
|
||||||
res = self.post(url, None)
|
self._raise_for_status(res)
|
||||||
self._raise_for_status(res)
|
|
||||||
|
|
||||||
def login(self, username, password=None, email=None):
|
def login(self, username, password=None, email=None):
|
||||||
url = self._url("/auth")
|
url = self._url("/auth")
|
||||||
|
@ -360,54 +359,42 @@ class Client(requests.Session):
|
||||||
return self._result(self._post_json(u, None, headers=headers))
|
return self._result(self._post_json(u, None, headers=headers))
|
||||||
return self._result(self._post_json(u, authcfg))
|
return self._result(self._post_json(u, authcfg))
|
||||||
|
|
||||||
def remove_container(self, *args, **kwargs):
|
def remove_container(self, container, v=False):
|
||||||
params = {
|
params = { 'v': v }
|
||||||
'v': 1 if kwargs.get('v', False) else 0
|
res = self.delete(self._url("/containers/" + container), params=params)
|
||||||
}
|
self._raise_for_status(res)
|
||||||
for container in args:
|
|
||||||
res = self.delete(self._url("/containers/" + container), params=params)
|
|
||||||
self._raise_for_status(res)
|
|
||||||
|
|
||||||
def remove_image(self, *args):
|
def remove_image(self, image):
|
||||||
for image in args:
|
res = self.delete(self._url("/images/" + image))
|
||||||
res = self.delete(self._url("/images/" + image))
|
self._raise_for_status(res)
|
||||||
self._raise_for_status(res)
|
|
||||||
|
|
||||||
def restart(self, *args, **kwargs):
|
def restart(self, container, timeout=10):
|
||||||
params = {
|
params = { 't': timeout }
|
||||||
't': kwargs.get('timeout', 10)
|
url = self._url("/containers/{0}/restart".format(container))
|
||||||
}
|
res = self.post(url, None, params=params)
|
||||||
for name in args:
|
self._raise_for_status(res)
|
||||||
url = self._url("/containers/{0}/restart".format(name))
|
|
||||||
res = self.post(url, None, params=params)
|
|
||||||
self._raise_for_status(res)
|
|
||||||
|
|
||||||
def search(self, term):
|
def search(self, term):
|
||||||
return self._result(self.get(self._url("/images/search"),
|
return self._result(self.get(self._url("/images/search"),
|
||||||
params={'term': term}), True)
|
params={'term': term}), True)
|
||||||
|
|
||||||
def start(self, *args, **kwargs):
|
def start(self, container, binds=None):
|
||||||
start_config = {}
|
start_config = {}
|
||||||
binds = kwargs.pop('binds', '')
|
|
||||||
if binds:
|
if binds:
|
||||||
bind_pairs = ['{0}:{1}'.format(host, dest) for host, dest in binds.items()]
|
bind_pairs = ['{0}:{1}'.format(host, dest) for host, dest in binds.items()]
|
||||||
start_config = {
|
start_config = {
|
||||||
'Binds': bind_pairs,
|
'Binds': bind_pairs,
|
||||||
}
|
}
|
||||||
|
|
||||||
for name in args:
|
url = self._url("/containers/{0}/start".format(container))
|
||||||
url = self._url("/containers/{0}/start".format(name))
|
res = self._post_json(url, start_config)
|
||||||
res = self._post_json(url, start_config)
|
self._raise_for_status(res)
|
||||||
self._raise_for_status(res)
|
|
||||||
|
|
||||||
def stop(self, *args, **kwargs):
|
def stop(self, container, timeout=10):
|
||||||
params = {
|
params = { 't': timeout }
|
||||||
't': kwargs.get('timeout', 10)
|
url = self._url("/containers/{0}/stop".format(container))
|
||||||
}
|
res = self.post(url, None, params=params)
|
||||||
for name in args:
|
self._raise_for_status(res)
|
||||||
url = self._url("/containers/{0}/stop".format(name))
|
|
||||||
res = self.post(url, None, params=params)
|
|
||||||
self._raise_for_status(res)
|
|
||||||
|
|
||||||
def tag(self, image, repository, tag=None, force=False):
|
def tag(self, image, repository, tag=None, force=False):
|
||||||
params = {
|
params = {
|
||||||
|
@ -421,20 +408,17 @@ class Client(requests.Session):
|
||||||
return res.status_code == 201
|
return res.status_code == 201
|
||||||
|
|
||||||
def top(self, container):
|
def top(self, container):
|
||||||
return self._result(self.get(self._url("/containers/{0}/top".format(container))), True)
|
u = self._url("/containers/{0}/top".format(container)
|
||||||
|
return self._result(self.get(u)), True)
|
||||||
|
|
||||||
def version(self):
|
def version(self):
|
||||||
return self._result(self.get(self._url("/version")), True)
|
return self._result(self.get(self._url("/version")), True)
|
||||||
|
|
||||||
def wait(self, *args):
|
def wait(self, container):
|
||||||
result = []
|
url = self._url("/containers/{0}/wait".format(container))
|
||||||
for name in args:
|
res = self.post(url, None, timeout=None)
|
||||||
url = self._url("/containers/{0}/wait".format(name))
|
self._raise_for_status(res)
|
||||||
res = self.post(url, None, timeout=None)
|
json_ = res.json()
|
||||||
self._raise_for_status(res)
|
if 'StatusCode' in json_:
|
||||||
json_ = res.json()
|
return json_['StatusCode']
|
||||||
if 'StatusCode' in json_:
|
return -1
|
||||||
result.append(json_['StatusCode'])
|
|
||||||
if len(result) == 1:
|
|
||||||
return result[0]
|
|
||||||
return result
|
|
||||||
|
|
|
@ -23,8 +23,7 @@ import docker
|
||||||
import six
|
import six
|
||||||
|
|
||||||
# FIXME: missing tests for
|
# FIXME: missing tests for
|
||||||
# export; history; import_image; insert; port; push;
|
# export; history; import_image; insert; port; push; tag
|
||||||
# tag; kill/stop/start/wait/restart (multi)
|
|
||||||
|
|
||||||
class BaseTestCase(unittest.TestCase):
|
class BaseTestCase(unittest.TestCase):
|
||||||
tmp_imgs = []
|
tmp_imgs = []
|
||||||
|
@ -37,15 +36,15 @@ class BaseTestCase(unittest.TestCase):
|
||||||
self.tmp_containers = []
|
self.tmp_containers = []
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
if len(self.tmp_imgs) > 0:
|
for img in self.tmp_imgs:
|
||||||
try:
|
try:
|
||||||
self.client.remove_image(*self.tmp_imgs)
|
self.client.remove_image(img)
|
||||||
except docker.APIError:
|
except docker.APIError:
|
||||||
pass
|
pass
|
||||||
if len(self.tmp_containers) > 0:
|
for container in self.tmp_containers:
|
||||||
try:
|
try:
|
||||||
self.client.stop(*self.tmp_containers, t=1)
|
self.client.stop(container, timeout=1)
|
||||||
self.client.remove_container(*self.tmp_containers)
|
self.client.remove_container(container)
|
||||||
except docker.APIError:
|
except docker.APIError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -214,8 +213,6 @@ class TestDiff(BaseTestCase):
|
||||||
self.assertEqual(len(test_diff), 1)
|
self.assertEqual(len(test_diff), 1)
|
||||||
self.assertIn('Kind', test_diff[0])
|
self.assertIn('Kind', test_diff[0])
|
||||||
self.assertEqual(test_diff[0]['Kind'], 1)
|
self.assertEqual(test_diff[0]['Kind'], 1)
|
||||||
# FIXME also test remove/modify
|
|
||||||
# (need testcommit first)
|
|
||||||
|
|
||||||
class TestStop(BaseTestCase):
|
class TestStop(BaseTestCase):
|
||||||
def runTest(self):
|
def runTest(self):
|
||||||
|
|
Loading…
Reference in New Issue