温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-09-25 19:00:32 来源:脚本之家 阅读:193 作者:fiverwyp 栏目:开发技术

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版本不兼容的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI