快速解决docker-py api版本不兼容的问题

yipeiwu_com6年前Python基础

docker提供了Python、Go等编程语言的api。最近打算用docker SDK for Python(以下简称docker-py)做点东西,本来以为按照官网上的步骤安装很简单,pip install docker 就行,然而出现了版本不兼容的问题。

报错如下:

docker.errors.APIError: 400 Client Error: Bad Request ("client is newer than server (client API version: 1.30, server API version: 1.19)")

可以使用指令 docker version查看版本号,果然api版本是1.19,但是docker-py的版本是1.30。在docker-py官网 里找一找解决方法,from_env()的解释如下:

 from_env()

 Return a client configured from environment variables.

 The environment variables used are the same as those used by the Docker command-line client. They are:

 DOCKER_HOST

  The URL to the Docker host.

 DOCKER_TLS_VERIFY

  Verify the host against a CA certificate.

 DOCKER_CERT_PATH

  A path to a directory containing TLS certificates to use when connecting to the Docker host.

 Parameters: 

  version (str) – The version of the API to use. Set to auto to automatically detect the server's version. Default: 1.26
  timeout (int) – Default timeout for API calls, in seconds.
  ssl_version (int) – A valid SSL version.
  assert_hostname (bool) – Verify the hostname of the server.
  environment (dict) – The environment to read environment variables from. Default: the value of os.environ

有一个参数可以指定version,于是这样写:client=docker.from_env(version='1.19') 似乎就能避免版本不兼容的问题了。然而还是报错:

ValueError: zero length field name in format

搜了一下这是因为我的python版本是2.6,在对参数解析时出现格式问题,没办法,只好升级python2.7了。

在官网的change日志中可以看到,2.0版本之后就不再支持python2.6了。

2.0.0
...
Breaking changes
 Dropped support for Python 2.6

将python2.6升级到2.7,不仅要编译安装python2.7,还要把/usr/local/bin/pip2.7软连接给/usr/bin/pip 以覆盖原来的pip2.6,然后重新pip install docker

如果是centos系统,不要忘了修改/usr/bin/yum ,将第一行#!/usr/bin/python 改为#!/usr/bin/python2.6 ,否则yum指令无法使用。

以上这篇快速解决docker-py api版本不兼容的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

研究Python的ORM框架中的SQLAlchemy库的映射关系

前面介绍了关于用户账户的User表,但是现实生活中随着问题的复杂化数据库存储的数据不可能这么简单,让我们设想有另外一张表,这张表和User有联系,也能够被映射和查询,那么这张表可以存储关...

python使用htmllib分析网页内容的方法

本文实例讲述了python使用htmllib分析网页内容的方法。分享给大家供大家参考。具体实现方法如下: import htmllib, urllib, formatter, sys...

Python实现CET查分的方法

Python CET自动查询方法需要用到的python方法模块有:sys、urllib2 本文实例讲述了Python实现CET查分的方法。分享给大家供大家参考。具体实现方法如下: 复制代...

python实现的自动发送消息功能详解

python实现的自动发送消息功能详解

本文实例讲述了python实现的自动发送消息功能。分享给大家供大家参考,具体如下: 一个简单的脚本 #-*- coding:utf-8 -*- from __future__ imp...

python使用正则搜索字符串或文件中的浮点数代码实例

用python和numpy处理数据次数比较多,写了几个小函数,可以方便地读写数据: # -*- coding: utf-8 -*- #------------------------...