• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
导航菜单
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx
您的位置:首页 > 服务器 >虚拟化 > Docker-client for python详解及简单示例

Docker-client for python详解及简单示例

作者:Andy-xu 字体:[增加 减小] 来源:互联网

本文主要包含Docker,client,for,python详解,Docker,client,for,python实例,Docker,client,for,python应用等服务器相关知识,Andy-xu 希望可以进行参考

Docker-client for python使用指南:

客户端初始化的三种方法

import docker
docker.api()
docker.APIClient()
docker.client()
docker.DockerClient() 其实也是docker.client()的一个子集
docker.from_env() 其实就是docker.client()的一个子集

一、初始化客户端

1.Docker客户端的初始化工作

>>> import docker
>>> client = docker.APIClient(base_url='unix://var/run/docker.sock',version='1.21',timeout=5)
>>> client.version()
{u'ApiVersion': u'1.21',
 u'Arch': u'amd64',
 u'BuildTime': u'2016-09-27T23:38:15.810178467+00:00',
 u'Experimental': True,
 u'GitCommit': u'45bed2c',
 u'GoVersion': u'go1.6.3',
 u'KernelVersion': u'4.4.22-moby',
 u'Os': u'linux',
 u'Version': u'1.12.2-rc1'}
  Args:
   base_url (str): 指定链接路径,可以通过socket或者tcp方式链接
     ``unix:///var/run/docker.sock`` or ``tcp://127.0.0.1:1234``.
   version (str): 指定API使用的版本(docker=2.0.0默认的api版本是1.24,最低支持1.21,docker1.9+的api是1.21),因此在使用python的docker模块时一定要注意docker的api以及docker模块的api是否兼容。当然如果设置为 ``auto`` 降回去自动检测server的版本
   timeout (int): 使用API调用的默认超时时间,默认单位为秒
   tls (bool or :py:class:`~docker.tls.TLSConfig`): Enable TLS. Pass
     ``True`` to enable it with default options, or pass a
     :py:class:`~docker.tls.TLSConfig` object to use custom
     configuration.

查看docker引擎当前版本:

$ sudo docker version
Client:
 Version:   1.9.1
 API version: 1.21
 Go version:  go1.4.3
 Git commit:  a34a1d5-dirty
 Built:    Tue Mar 28 15:39:19 UTC 2017
 OS/Arch:   linux/amd64

Server:
 Version:   1.9.1
 API version: 1.21
 Go version:  go1.4.3
 Git commit:  a34a1d5-dirty
 Built:    Tue Mar 28 15:39:19 UTC 2017
 OS/Arch:   linux/amd64

The sdk of docker for python--docker==2.0.0:

1.丢弃了python2.6的支持
2.最低支持API版本为1.12(Engine version 1.9.0+)
3.`docker.Client`被替换成`docker.APIClient`
4.`docker.from_env`初始化一个docker客户端实例代替了`APIClient `实例 
5.从`APIClient.start`中移除了HostConfig参数
6.开始由之前的docker-py模块变为docker
7.`docker.ssladapter`替换为`docker.transport.ssladapter`

2.Docker客户端的具体方法

import docker
C = docker.DockerClient(base_url='unix://var/run/docker.sock',version='auto',timeout=10)

##docker相关的方法使用
使用DockerClient对象,会有以下方法:
C.api,
C.containers,
C.events,
C.from_env,
C.images,
C.info,
C.login,
C.networks,
C.nodes,
C.ping,
C.services,
C.swarm,
C.version,
C.volumes,


#输出docker的相关信息,相当于docker info
C.info()

二、api方法使用示例

1. login方法定义

C.login()
login(*args, **kwargs) method of docker.client.DockerClient instance
  Authenticate with a registry. Similar to the ``docker login`` command.

  Args:
    username (str): The registry username
    password (str): The plaintext password
    email (str): The email for the registry account
    registry (str): URL to the registry. E.g.
      ``https://index.docker.io/v1/``
    reauth (bool): Whether refresh existing authentication on the
      Docker server.
    dockercfg_path (str): Use a custom path for the ``.dockercfg`` file
  (default ``$HOME/.dockercfg``)

  Returns:返回的错误日志信息
    (dict): The response from the login request

     Raises:
    :py:class:`docker.errors.APIError`
      If the server returns an error.

##使用login方法登录
C.login('xxbandy123','nslalla')

2.images 类定义:

build方法
get方法:
get(self, name)
  Gets an image.

  Args:
    name (str): The name of the image.

  Returns:
    (:py:class:`Image`): The image.

  Raises:
    :py:class:`docker.errors.ImageNotFound` If the image does not
    exist.
    :py:class:`docker.errors.APIError`
      If the server returns an error.
list方法:
  list(self, name=None, all=False, filters=None)
  List images on the server.

  Args:
    name (str): Only show images belonging to the repository ``name``
    all (bool): Show intermediate image layers. By default, these are
      filtered out.
    filters (dict): Filters to be processed on the image list.
      Available filters:
      - ``dangling`` (bool)
      - ``label`` (str): format either ``key`` or ``key=value``

  Returns:
    (list of :py:class:`Image`): The images.

  Raises:
    :py:class:`docker.errors.APIError`
      If the server returns an error.

示例:

查看默认所有的镜像文件,以image-id进行区分
In [34]: C.images.list()
Out[34]: 
[<Image: 'busybox:latest'>,
 <Image: '172.24.254.235:5010/rancher-server:latest', 'rancher/server:latest'>,
 <Image: '172.24.254.235:5010/jdsingleuser:latest'>,
 <Image: 'registry:2'>,
 <Image: '172.24.254.235:5010/rancher-agent:latest', 'rancher/agent:v1.0.2'>]
load方法:相当于docker load

pull方法:下载镜像文件
 pull(self, name, **kwargs)
  Pull an image of the given name and return it. Similar to the
  ``docker pull`` command.

  If you want to get the raw pull output, use the
  :py:meth:`~docker.api.image.ImageApiMixin.pull` method in the
  low-level API.

  Args:
    repository (str): The repository to pull
    tag (str): The tag to pull
    insecure_registry (bool): Use an insecure registry
    auth_config (dict): Override the credentials that
      :py:meth:`~docker.client.DockerClient.login` has set for
      this request. ``auth_config`` should contain the ``username``
      and ``password`` keys to be valid.

  Returns:
    (:py:class:`Image`): The image that has been pulled.

需要注意的是:使用pull的时候,会弱匹配所有的tag标签

push方法:上传镜像文件

push(self, repository, tag=None, **kwargs)
  Push an image or a repository to the registry. Similar to the ``docker
  push`` command.

  Args:
    repository (str): The repository to push to
    tag (str): An optional tag to push
    stream (bool): Stream the output as a blocking generator
    insecure_registry (bool): Use ``http://`` to connect to the
      registry
    auth_config (dict): Override the credentials that
      :py:meth:`~docker.api.daemon.DaemonApiMixin.login` has set for
      this request. ``auth_config`` should contain the ``username``
      and ``password`` keys to be valid.

  Returns:
    (generator or str): The output from the server.

  Raises:
    :py:class:`docker.errors.APIError`

remove方法:docker rmi 
 remove(self, *args, **kwargs)
  Remove an image. Similar to the ``docker rmi`` command.

  Args:
    image (str): The image to remove
    force (bool): Force removal of the image
    noprune (bool): Do not delete untagged parents

search方法:
search(self, *args, **kwargs)
  Search for images on Docker Hub. Similar to the ``docker search``
  command.

  Args:
    term (str): A term to search for.

  Returns:
    (list of dicts): The response of the search.

3.docker管理容器相关

C.containers类,下面有相关的方法:

client,create,get,list,model,run

列出当前存活的容器:

C.containers.list()

列出指定容器:

C.containers.get('')

创建容器:

C.containers.create
create(image, command=None, **kwargs) method of docker.models.containers.ContainerCollection instance
  Create a container without star
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Docker实践8:Compose,docker实践compose
  • Docker buildx构建多平台镜像并推送到私有仓库的方法
  • Docker三个基本概念镜像(Image)容器(Container)仓库(Repository)
  • 什么是Docker?为什么要使用Docker
  • docker 开源的应用容器引擎
  • 如何给docker设置http代理
  • docker官方mysql镜像自定义配置详解
  • 详解Docker数据管理(数据卷&数据卷容器)
  • 如何利用Docker容器实现代理转发与数据备份详解
  • 详解Docker Swarm 在持续集成测试中的应用

相关文章

  • Docker系列之使用Docker Compose编排容器
  • CoreOS配置Docker镜像加速器的方法
  • 手把手教你使用 virtualBox 让虚拟机连接网络的教程
  • Docker实践之搭建wordpress的方法
  • Docker 教程之Docker Hub详细介绍
  • Docker教程之Ubuntu 安装 Docker详细介绍
  • Docker 搭建私有仓库(registry、harbor)
  • 使用docker快速搭建Spark集群的方法教程
  • 如何用docker部署redis cluster的方法
  • 在docker中部署tomcat并且部署java应用程序的步骤详解

文章分类

  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx

最近更新的内容

    • java读写hdfs简单demo,javahdfsdemo
    • Hadoop安全性,hadoop安全机制
    • VirtualBox配置虚拟网卡(桥接)——实现主机-虚拟机网络互通
    • ubuntu vps安装docker报错:Cannot connect to the Docker daemon at unix:///var/run/docker.sock.问题解决
    • docker基础知识之挂载本地目录的方法
    • Docker 教程之仓库配置文件详解
    • 创建Web项目的Docker镜像实例讲解
    • Linux下使用docker搭建Openvpn代理的方法
    • Openstack 启动instance 'hvm'错误问题解决办法
    • 详解在docker中制作自己的JDK+tomcat镜像

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有