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)`
|
||||
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)`
|
||||
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
|
||||
|
@ -51,13 +54,15 @@ Identical to the `docker info` command.
|
|||
Identical to the `docker insert` command.
|
||||
|
||||
* `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)`
|
||||
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...)`
|
||||
Identical to the `docker kill` command.
|
||||
* `c.kill(container)`
|
||||
Kill a container. Similar to the `docker kill` command.
|
||||
|
||||
* `c.login(username, password=None, email=None)`
|
||||
Identical to the `docker login` command (but non-interactive, obviously).
|
||||
|
@ -74,37 +79,39 @@ Identical to the `docker pull` command.
|
|||
* `c.push(repository)`
|
||||
Identical to the `docker push` command.
|
||||
|
||||
* `c.remove_container(containers..., v=False)`
|
||||
Identical to the `docker rm` command.
|
||||
* `c.remove_container(container, v=False)`
|
||||
Remove a container. Similar to the `docker rm` command.
|
||||
|
||||
* `c.remove_image(images...)`
|
||||
Identical to the `docker rmi` command.
|
||||
* `c.remove_image(image)`
|
||||
Remove an image. Similar to the `docker rmi` command.
|
||||
|
||||
* `c.restart(containers..., t=10)`
|
||||
Identical to the `docker restart` command.
|
||||
* `c.restart(container, timeout=10)`
|
||||
Restart a container. Similar to the `docker restart` command.
|
||||
|
||||
* `c.search(term)`
|
||||
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.
|
||||
Use `docker logs` to recover `stdout`/`stderr`
|
||||
|
||||
* `c.start(container, binds={'/host': '/mnt'})`
|
||||
Allows to bind a directory in the host to the container.
|
||||
Similar to the `docker run` command with the `-b="/host:/mnt"`.
|
||||
`binds` Allows to bind a directory in the host to the container.
|
||||
Similar to the `docker run` command with option `-b="/host:/mnt"`.
|
||||
Requires the container to be created with the volumes argument:
|
||||
`c.create_container(..., volumes={'/mnt': {}})`
|
||||
|
||||
* `c.stop(containers..., t=10)`
|
||||
Identical to the `docker stop` command.
|
||||
* `c.stop(containers, timeout=10)`
|
||||
Stops a container. Similar to the `docker stop` command.
|
||||
|
||||
* `c.tag(image, repository, tag=None, force=False)`
|
||||
Identical to the `docker tag` command.
|
||||
|
||||
* `c.top(container_id)`
|
||||
Identical to the `docker top` command.
|
||||
|
||||
* `c.version()`
|
||||
Identical to the `docker version` command.
|
||||
|
||||
* `c.wait(containers...)`
|
||||
Identical to the `docker wait` command.
|
||||
* `c.wait(containers)`
|
||||
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")
|
||||
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,
|
||||
detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None,
|
||||
environment=None, dns=None, volumes=None, volumes_from=None,
|
||||
|
@ -217,13 +224,6 @@ class Client(requests.Session):
|
|||
"image first.".format(config['Image']))
|
||||
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):
|
||||
return self._result(self.get(self._url("/containers/{0}/changes".
|
||||
format(container))), True)
|
||||
|
@ -284,11 +284,10 @@ class Client(requests.Session):
|
|||
return self._result(self.get(self._url("/images/{0}/json".
|
||||
format(image_id))), True)
|
||||
|
||||
def kill(self, *args):
|
||||
for name in args:
|
||||
url = self._url("/containers/{0}/kill".format(name))
|
||||
res = self.post(url, None)
|
||||
self._raise_for_status(res)
|
||||
def kill(self, container):
|
||||
url = self._url("/containers/{0}/kill".format(container))
|
||||
res = self.post(url, None)
|
||||
self._raise_for_status(res)
|
||||
|
||||
def login(self, username, password=None, email=None):
|
||||
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, authcfg))
|
||||
|
||||
def remove_container(self, *args, **kwargs):
|
||||
params = {
|
||||
'v': 1 if kwargs.get('v', False) else 0
|
||||
}
|
||||
for container in args:
|
||||
res = self.delete(self._url("/containers/" + container), params=params)
|
||||
self._raise_for_status(res)
|
||||
def remove_container(self, container, v=False):
|
||||
params = { 'v': v }
|
||||
res = self.delete(self._url("/containers/" + container), params=params)
|
||||
self._raise_for_status(res)
|
||||
|
||||
def remove_image(self, *args):
|
||||
for image in args:
|
||||
res = self.delete(self._url("/images/" + image))
|
||||
self._raise_for_status(res)
|
||||
def remove_image(self, image):
|
||||
res = self.delete(self._url("/images/" + image))
|
||||
self._raise_for_status(res)
|
||||
|
||||
def restart(self, *args, **kwargs):
|
||||
params = {
|
||||
't': kwargs.get('timeout', 10)
|
||||
}
|
||||
for name in args:
|
||||
url = self._url("/containers/{0}/restart".format(name))
|
||||
res = self.post(url, None, params=params)
|
||||
self._raise_for_status(res)
|
||||
def restart(self, container, timeout=10):
|
||||
params = { 't': timeout }
|
||||
url = self._url("/containers/{0}/restart".format(container))
|
||||
res = self.post(url, None, params=params)
|
||||
self._raise_for_status(res)
|
||||
|
||||
def search(self, term):
|
||||
return self._result(self.get(self._url("/images/search"),
|
||||
params={'term': term}), True)
|
||||
|
||||
def start(self, *args, **kwargs):
|
||||
def start(self, container, binds=None):
|
||||
start_config = {}
|
||||
binds = kwargs.pop('binds', '')
|
||||
if binds:
|
||||
bind_pairs = ['{0}:{1}'.format(host, dest) for host, dest in binds.items()]
|
||||
start_config = {
|
||||
'Binds': bind_pairs,
|
||||
}
|
||||
|
||||
for name in args:
|
||||
url = self._url("/containers/{0}/start".format(name))
|
||||
res = self._post_json(url, start_config)
|
||||
self._raise_for_status(res)
|
||||
url = self._url("/containers/{0}/start".format(container))
|
||||
res = self._post_json(url, start_config)
|
||||
self._raise_for_status(res)
|
||||
|
||||
def stop(self, *args, **kwargs):
|
||||
params = {
|
||||
't': kwargs.get('timeout', 10)
|
||||
}
|
||||
for name in args:
|
||||
url = self._url("/containers/{0}/stop".format(name))
|
||||
res = self.post(url, None, params=params)
|
||||
self._raise_for_status(res)
|
||||
def stop(self, container, timeout=10):
|
||||
params = { 't': timeout }
|
||||
url = self._url("/containers/{0}/stop".format(container))
|
||||
res = self.post(url, None, params=params)
|
||||
self._raise_for_status(res)
|
||||
|
||||
def tag(self, image, repository, tag=None, force=False):
|
||||
params = {
|
||||
|
@ -421,20 +408,17 @@ class Client(requests.Session):
|
|||
return res.status_code == 201
|
||||
|
||||
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):
|
||||
return self._result(self.get(self._url("/version")), True)
|
||||
|
||||
def wait(self, *args):
|
||||
result = []
|
||||
for name in args:
|
||||
url = self._url("/containers/{0}/wait".format(name))
|
||||
res = self.post(url, None, timeout=None)
|
||||
self._raise_for_status(res)
|
||||
json_ = res.json()
|
||||
if 'StatusCode' in json_:
|
||||
result.append(json_['StatusCode'])
|
||||
if len(result) == 1:
|
||||
return result[0]
|
||||
return result
|
||||
def wait(self, container):
|
||||
url = self._url("/containers/{0}/wait".format(container))
|
||||
res = self.post(url, None, timeout=None)
|
||||
self._raise_for_status(res)
|
||||
json_ = res.json()
|
||||
if 'StatusCode' in json_:
|
||||
return json_['StatusCode']
|
||||
return -1
|
||||
|
|
|
@ -23,8 +23,7 @@ import docker
|
|||
import six
|
||||
|
||||
# FIXME: missing tests for
|
||||
# export; history; import_image; insert; port; push;
|
||||
# tag; kill/stop/start/wait/restart (multi)
|
||||
# export; history; import_image; insert; port; push; tag
|
||||
|
||||
class BaseTestCase(unittest.TestCase):
|
||||
tmp_imgs = []
|
||||
|
@ -37,15 +36,15 @@ class BaseTestCase(unittest.TestCase):
|
|||
self.tmp_containers = []
|
||||
|
||||
def tearDown(self):
|
||||
if len(self.tmp_imgs) > 0:
|
||||
for img in self.tmp_imgs:
|
||||
try:
|
||||
self.client.remove_image(*self.tmp_imgs)
|
||||
self.client.remove_image(img)
|
||||
except docker.APIError:
|
||||
pass
|
||||
if len(self.tmp_containers) > 0:
|
||||
for container in self.tmp_containers:
|
||||
try:
|
||||
self.client.stop(*self.tmp_containers, t=1)
|
||||
self.client.remove_container(*self.tmp_containers)
|
||||
self.client.stop(container, timeout=1)
|
||||
self.client.remove_container(container)
|
||||
except docker.APIError:
|
||||
pass
|
||||
|
||||
|
@ -214,8 +213,6 @@ class TestDiff(BaseTestCase):
|
|||
self.assertEqual(len(test_diff), 1)
|
||||
self.assertIn('Kind', test_diff[0])
|
||||
self.assertEqual(test_diff[0]['Kind'], 1)
|
||||
# FIXME also test remove/modify
|
||||
# (need testcommit first)
|
||||
|
||||
class TestStop(BaseTestCase):
|
||||
def runTest(self):
|
||||
|
|
Loading…
Reference in New Issue