| 529 | |
| 530 | |
| 531 | class ContainerCollection(Collection): |
| 532 | model = Container |
| 533 | |
| 534 | def run(self, image, command=None, stdout=True, stderr=False, |
| 535 | remove=False, **kwargs): |
| 536 | """ |
| 537 | Run a container. By default, it will wait for the container to finish |
| 538 | and return its logs, similar to ``docker run``. |
| 539 | |
| 540 | If the ``detach`` argument is ``True``, it will start the container |
| 541 | and immediately return a :py:class:`Container` object, similar to |
| 542 | ``docker run -d``. |
| 543 | |
| 544 | Example: |
| 545 | Run a container and get its output: |
| 546 | |
| 547 | >>> import docker |
| 548 | >>> client = docker.from_env() |
| 549 | >>> client.containers.run('alpine', 'echo hello world') |
| 550 | b'hello world\\n' |
| 551 | |
| 552 | Run a container and detach: |
| 553 | |
| 554 | >>> container = client.containers.run('bfirsh/reticulate-splines', |
| 555 | detach=True) |
| 556 | >>> container.logs() |
| 557 | 'Reticulating spline 1...\\nReticulating spline 2...\\n' |
| 558 | |
| 559 | Args: |
| 560 | image (str): The image to run. |
| 561 | command (str or list): The command to run in the container. |
| 562 | auto_remove (bool): enable auto-removal of the container on daemon |
| 563 | side when the container's process exits. |
| 564 | blkio_weight_device: Block IO weight (relative device weight) in |
| 565 | the form of: ``[{"Path": "device_path", "Weight": weight}]``. |
| 566 | blkio_weight: Block IO weight (relative weight), accepts a weight |
| 567 | value between 10 and 1000. |
| 568 | cap_add (list of str): Add kernel capabilities. For example, |
| 569 | ``["SYS_ADMIN", "MKNOD"]``. |
| 570 | cap_drop (list of str): Drop kernel capabilities. |
| 571 | cgroup_parent (str): Override the default parent cgroup. |
| 572 | cgroupns (str): Override the default cgroup namespace mode for the |
| 573 | container. One of: |
| 574 | - ``private`` the container runs in its own private cgroup |
| 575 | namespace. |
| 576 | - ``host`` use the host system's cgroup namespace. |
| 577 | cpu_count (int): Number of usable CPUs (Windows only). |
| 578 | cpu_percent (int): Usable percentage of the available CPUs |
| 579 | (Windows only). |
| 580 | cpu_period (int): The length of a CPU period in microseconds. |
| 581 | cpu_quota (int): Microseconds of CPU time that the container can |
| 582 | get in a CPU period. |
| 583 | cpu_rt_period (int): Limit CPU real-time period in microseconds. |
| 584 | cpu_rt_runtime (int): Limit CPU real-time runtime in microseconds. |
| 585 | cpu_shares (int): CPU shares (relative weight). |
| 586 | cpuset_cpus (str): CPUs in which to allow execution (``0-3``, |
| 587 | ``0,1``). |
| 588 | cpuset_mems (str): Memory nodes (MEMs) in which to allow execution |