官方文档https://docker-py.readthedocs.io/en/stable/
1.安装docker 模块
如果不指定版本,默认安装最新的,若不匹配会报以下错误(解决方法为在定义client时可指定client的版本)
docker.errors.APIError: 400 Client Error: Bad Request ("client is newer than server (client API version: 1.26, server API version: 1.24)")
pip install docker -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
2.最简单测试
>>> import tab
>>> import docker
>>> client = docker.from_env()
>>> client.containers.run("ubuntu","echo hello world")
'hello world\n'
在主机上查看
[root@centos7 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
25f94eddca69 ubuntu "echo hello world" 13 seconds ago Exited (0) 12 seconds ago infallible_raman