zoukankan      html  css  js  c++  java
  • python管理docker

    python管理docker——docker模块

    参考: https://docker-py.readthedocs.io/en/stable/configs.html

    docker提供API,所以直接python管理与使用docker shell命令极其相似

    一、安装docker模块

    pip install docker
    

    二、使用

    1、建立docker连接

    使用默认socket、配置

    import docker
    client = docker.from_env()
    

    2、容器管理

    • run容器

      # run(self, image, command=None, stdout=True, stderr=False, remove=False, **kwargs)
      """
                  image (str): 需要运行的镜像.
                  command (str or list): 运行镜像在容器中执行的命令.
                  auto_remove (bool): 当容器的进程退出时,在守护程序端启用容器的自动删除
                  blkio_weight_device: Block IO weight (relative device weight) in
                      the form of: ``[{"Path": "device_path", "Weight": weight}]``.
                  blkio_weight: Block IO weight (relative weight), accepts a weight value between 10 and 1000.
                  cap_add (list of str): Add kernel capabilities. For example,
                      ``["SYS_ADMIN", "MKNOD"]``.
                  cap_drop (list of str): Drop kernel capabilities.
                  cgroup_parent (str): Override the default parent cgroup.
                  cpu_period (int): The length of a CPU period in microseconds.
                  cpu_quota (int): Microseconds of CPU time that the container can get in a CPU period.
                  cpu_rt_period (int): Limit CPU real-time period in microseconds.
                  cpu_rt_runtime (int): Limit CPU real-time runtime in microseconds.
                  cpu_shares (int): CPU shares (relative weight).
                  cpuset_cpus (str): CPUs in which to allow execution (``0-3``,
                      ``0,1``).
                  cpuset_mems (str): Memory nodes (MEMs) in which to allow execution
                      (``0-3``, ``0,1``). Only effective on NUMA systems.
                  detach (bool): 后端运行容器并返回一个Container对象
                  device_cgroup_rules (:py:class:`list`): A list of cgroup rules to apply to the container.
                  device_read_bps: Limit read rate (bytes per second) from a device
                      in the form of: `[{"Path": "device_path", "Rate": rate}]`
                  device_read_iops: Limit read rate (IO per second) from a device.
                  device_write_bps: Limit write rate (bytes per second) from a device.
                  device_write_iops: Limit write rate (IO per second) from a device.
                  devices (:py:class:`list`): Expose host devices to the container,
                      as a list of strings in the form
                      ``<path_on_host>:<path_in_container>:<cgroup_permissions>``.
      
                      For example, ``/dev/sda:/dev/xvda:rwm`` allows the container
                      to have read-write access to the host's ``/dev/sda`` via a
                      node named ``/dev/xvda`` inside the container.
                  device_requests (:py:class:`list`): Expose host resources such as
                      GPUs to the container, as a list of
                      :py:class:`docker.types.DeviceRequest` instances.
                  dns (:py:class:`list`): Set custom DNS servers.
                  dns_opt (:py:class:`list`): Additional options to be added to the
                      container's ``resolv.conf`` file.
                  dns_search (:py:class:`list`): DNS search domains.
                  domainname (str or list): Set custom DNS search domains.
                  entrypoint (str or list): 容器的入口点
                  environment (dict or list): 要在容器内设置的环境变量, 一个字符串字典或字符串列表
                      例如 ``["SOMEVARIABLE=xxx"]``.
                  extra_hosts (dict): 要在容器内解析的其他主机名,作为主机名到IP地址的映射
                  group_add (:py:class:`list`): List of additional group names and/or
                      IDs that the container process will run as.
                  healthcheck (dict): Specify a test to perform to check that the
                      container is healthy.
                  hostname (str): 容器的主机名
                  init (bool): Run an init inside the container that forwards
                      signals and reaps processes
                  init_path (str): Path to the docker-init binary
                  ipc_mode (str): Set the IPC mode for the container.
                  isolation (str): Isolation technology to use. Default: `None`.
                  kernel_memory (int or str): Kernel memory limit
                  labels (dict or list): A dictionary of name-value labels (e.g.
                      ``{"label1": "value1", "label2": "value2"}``) or a list of
                      names of labels to set with empty values (e.g.
                      ``["label1", "label2"]``)
                  links (dict): Mapping of links using the
                      ``{'container': 'alias'}`` format. The alias is optional.
                      Containers declared in this dict will be linked to the new
                      container using the provided alias. Default: ``None``.
                  log_config (LogConfig): Logging configuration.
                  lxc_conf (dict): LXC config.
                  mac_address (str): MAC address to assign to the container.
                  mem_limit (int or str): Memory limit. Accepts float values
                      (which represent the memory limit of the created container in
                      bytes) or a string with a units identification char
                      (``100000b``, ``1000k``, ``128m``, ``1g``). If a string is
                      specified without a units character, bytes are assumed as an
                      intended unit.
                  mem_reservation (int or str): Memory soft limit.
                  mem_swappiness (int): Tune a container's memory swappiness
                      behavior. Accepts number between 0 and 100.
                  memswap_limit (str or int): Maximum amount of memory + swap a
                      container is allowed to consume.
                  mounts (:py:class:`list`): z. More powerful alternative to ``volumes``. Each
                      item in the list is expected to be a
                      :py:class:`docker.types.Mount` object.
                  name (str): 容器名字
                  nano_cpus (int):  CPU quota in units of 1e-9 CPUs.
                  network (str): Name of the network this container will be connected
                      to at creation time. You can connect to additional networks
                      using :py:meth:`Network.connect`. Incompatible with
                      ``network_mode``.
                  network_disabled (bool): Disable networking.
                  network_mode (str): One of:
      
                      - ``bridge`` Create a new network stack for the container on
                        on the bridge network.
                      - ``none`` No networking for this container.
                      - ``container:<name|id>`` Reuse another container's network
                        stack.
                      - ``host`` Use the host network stack.
      
                      Incompatible with ``network``.
                  oom_kill_disable (bool): Whether to disable OOM killer.
                  oom_score_adj (int): An integer value containing the score given
                      to the container in order to tune OOM killer preferences.
                  pid_mode (str): If set to ``host``, use the host PID namespace
                      inside the container.
                  pids_limit (int): Tune a container's pids limit. Set ``-1`` for
                      unlimited.
                  platform (str): Platform in the format ``os[/arch[/variant]]``.
                      Only used if the method needs to pull the requested image.
                  ports (dict): 要在容器内绑定的端口,以及宿主机的映射端口
      
                      The keys of the dictionary are the ports to bind inside the
                      container, either as an integer or a string in the form
                      ``port/protocol``, where the protocol is either ``tcp``,
                      ``udp``, or ``sctp``.
      
                      The values of the dictionary are the corresponding ports to
                      open on the host, which can be either:
      
                      - The port number, as an integer. For example,
                        ``{'2222/tcp': 3333}`` will expose port 2222 inside the
                        container as port 3333 on the host.
                      - ``None``, to assign a random host port. For example,
                        ``{'2222/tcp': None}``.
                      - A tuple of ``(address, port)`` if you want to specify the
                        host interface. For example,
                        ``{'1111/tcp': ('127.0.0.1', 1111)}``.
                      - A list of integers, if you want to bind multiple host ports
                        to a single container port. For example,
                        ``{'1111/tcp': [1234, 4567]}``.
      
                  privileged (bool): Give extended privileges to this container.
                  publish_all_ports (bool): Publish all ports to the host.
                  read_only (bool): Mount the container's root filesystem as read
                      only.
                  remove (bool): 删除容器,默认是False.
      
                  restart_policy (dict): 容器重启的策略
                      Configured as a dictionary with keys:
      
                      - ``Name`` One of ``on-failure``, or ``always``.
                      - ``MaximumRetryCount`` Number of times to restart the
                        container on failure.
      
                      For example:
                      ``{"Name": "on-failure", "MaximumRetryCount": 5}``
      
                  runtime (str): Runtime to use with this container.
                  security_opt (:py:class:`list`): A list of string values to  customize labels for MLS systems, such as SELinux.
                  shm_size (str or int): Size of /dev/shm (e.g. ``1G``).
                  stdin_open (bool): Keep ``STDIN`` open even if not attached.
                  stdout (bool): Return logs from ``STDOUT`` when ``detach=False``.
                      Default: ``True``.
                  stderr (bool): Return logs from ``STDERR`` when ``detach=False``.
                      Default: ``False``.
                  stop_signal (str): The stop signal to use to stop the container
                      (e.g. ``SIGINT``).
                  storage_opt (dict): Storage driver options per container as a
                      key-value mapping.
                  stream (bool): If true and ``detach`` is false, return a log
                      generator instead of a string. Ignored if ``detach`` is true.
                      Default: ``False``.
                  sysctls (dict): Kernel parameters to set in the container.
                  tmpfs (dict): Temporary filesystems to mount, as a dictionary
                      mapping a path inside the container to options for that path.
      
                      For example:
      
                      .. code-block:: python
      
                          {
                              '/mnt/vol2': '',
                              '/mnt/vol1': 'size=3G,uid=1000'
                          }
      
                  tty (bool): Allocate a pseudo-TTY.
                  ulimits (:py:class:`list`): Ulimits to set inside the container,
                      as a list of :py:class:`docker.types.Ulimit` instances.
                  use_config_proxy (bool): If ``True``, and if the docker client
                      configuration file (``~/.docker/config.json`` by default)
                      contains a proxy configuration, the corresponding environment
                      variables will be set in the container being built.
                  user (str or int): Username or UID to run commands as inside the
                      container.
                  userns_mode (str): Sets the user namespace mode for the container
                      when user namespace remapping option is enabled. Supported
                      values are: ``host``
                  uts_mode (str): Sets the UTS namespace mode for the container.
                      Supported values are: ``host``
                  version (str): The version of the API to use. Set to ``auto`` to
                      automatically detect the server's version. Default: ``1.35``
                  volume_driver (str): The name of a volume driver/plugin.
                  volumes (dict or list): A dictionary to configure volumes mounted
                      inside the container. The key is either the host path or a
                      volume name, and the value is a dictionary with the keys:
      
                      - ``bind`` The path to mount the volume inside the container
                      - ``mode`` Either ``rw`` to mount the volume read/write, or
                        ``ro`` to mount it read-only.
      
                      For example:
      
                      .. code-block:: python
      
                          {'/home/user1/': {'bind': '/mnt/vol2', 'mode': 'rw'},
                           '/var/www': {'bind': '/mnt/vol1', 'mode': 'ro'}}
      
                  volumes_from (:py:class:`list`): List of container names or IDs to
                      get volumes from.
                  working_dir (str): Path to the working directory.
      """
      
      client.containers.run('image', 'cmd')
      
    • 查看容器

      client.containers.list()
      
      # list(self, all=False, before=None, filters=None, limit=-1, since=None, sparse=False, ignore_removed=False)
      # 默认是返回存活容器, docker ps
      """
      			all (bool): 返回所有的存活的、退出的容器,docker ps -a
                  since (str): Show only containers created since Id or Name, include
                      non-running ones
                  before (str): Show only container created before Id or Name,
                      include non-running ones
                  limit (int): Show `limit` last created containers, include
                      non-running ones
                  filters (dict): Filters to be processed on the image list.
                      Available filters:
      
                      - `exited` (int): Only containers with specified exit code
                      - `status` (str): One of ``restarting``, ``running``,
                          ``paused``, ``exited``
                      - `label` (str|list): format either ``"key"``, ``"key=value"``
                          or a list of such.
                      - `id` (str): The id of the container.
                      - `name` (str): The name of the container.
                      - `ancestor` (str): Filter by container ancestor. Format of
                          ``<image-name>[:tag]``, ``<image-id>``, or
                          ``<image@digest>``.
                      - `before` (str): Only containers created before a particular
                          container. Give the container name or id.
                      - `since` (str): Only containers created after a particular
                          container. Give container name or id.
      
                      A comprehensive list can be found in the documentation for
                      `docker ps
                      <https://docs.docker.com/engine/reference/commandline/ps>`_.
      
                  sparse (bool): Do not inspect containers. Returns partial
                      information, but guaranteed not to block. Use
                      :py:meth:`Container.reload` on resulting objects to retrieve
                      all attributes. Default: ``False``
                  ignore_removed (bool): Ignore failures due to missing containers
                      when attempting to inspect containers from the original list.
                      Set to ``True`` if race conditions are likely. Has no effect
                      if ``sparse=True``. Default: ``False``
      """
      
    • 从list获取一个容器

      container = client.containers.get(id_or_name)
      
    • 启动、停止、重启容器

      container.start()
      container.stop()
      container.restart()
      
    • 删除容器

      container.remove()
      
  • 相关阅读:
    web通信浅析(上B/S交互)转载
    tomcat内部运行原理浅析转载
    oracle集合运算
    Oracle 游标使用全解
    oracle 一些基本概念
    1.搭建项目环境之TestDirector 8.0
    修改Win7远程桌面端口
    从第二份工作开始
    2.搭建项目环境之源代码管理SVN
    How to Get IIS Web Sites Information Programmatically
  • 原文地址:https://www.cnblogs.com/linagcheng/p/13741015.html
Copyright © 2011-2022 走看看