官方文檔https://docker-py.readthedocs.io/en/stable/python
1.安裝docker 模塊docker
若是不指定版本,默認安裝最新的,若不匹配會報如下錯誤(解決方法爲在定義client時可指定client的版本)ubuntu
docker.errors.APIError: 400 Client Error: Bad Request ("client is newer than server (client API version: 1.26, server API version: 1.24)") centos
pip install docker -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
2.最簡單測試bash
>>> 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