安装docker-python和docker rest api

1.docker软件自带docker rest api,不用安装;

需要设置下docker配置文件

vi /etc/default/docker.io 
或者
vi /etc/default/docker #追加下面一行
DOCKER_OPTS="-H tcp://0.0.0.0:5555 -H  unix://var/run/docker.sock"

重启下docker,  service  docker  restart即可。
用法:

#1.查看所有镜像
#curl 'http://127.0.0.1:5555/images/json?all=0'| python -m json.tool 
#2.查看运行的容器
#curl http://127.0.0.1:5555/containers/json
#curl http://127.0.0.1:5555/containers/json |python -m json.tool
#3.查看某个容器具体信息
#curl -XGET 'http://127.0.0.1:4243/containers/df7b7568fa7a/json' | python -m json.tool
#4.关闭、开启容器
#curl -s -XPOST "http://127.0.0.1:5555/containers/d0274fdba510/stop"
#curl -s -XPOST "http://127.0.0.1:5555/containers/d0274fdba510/start"


2.安装docker-python

#apt-get install python-pip
#pip install docker-py
----------------------------
# wget https://github.com/docker/docker-py/archive/master.zip
# unzip master
# cd docker-py-master/
# python setup.py install

用法:

root@bogon:~# ipython
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
#倒入docker模块
In [1]: import docker

In [2]: docker.
docker.Client      docker.client      docker.ssladapter  docker.unixconn    docker.version
docker.auth        docker.errors      docker.tls         docker.utils       

#设置链接
In [2]: d = docker.Client(base_url='unix://var/run/docker.sock',version='1.9',timeout=10)

#docker模块的用法
In [3]: d.
d.adapters                      d.headers                       d.proxies
d.attach                        d.history                       d.pull
d.attach_socket                 d.hooks                         d.push
d.auth                          d.images                        d.put
d.base_url                      d.import_image                  d.remove_container
d.build                         d.info                          d.remove_image
d.cert                          d.insert                        d.request
d.close                         d.inspect_container             d.resize
d.commit                        d.inspect_image                 d.resolve_redirects
d.containers                    d.kill                          d.restart
d.cookies                       d.load_image                    d.search
d.copy                          d.login                         d.send
d.create_container              d.logs                          d.start
d.create_container_from_config  d.max_redirects                 d.stop
d.delete                        d.mount                         d.stream
d.diff                          d.options                       d.tag
d.events                        d.params                        d.top
d.export                        d.patch                         d.trust_env
d.get                           d.ping                          d.verify
d.get_adapter                   d.port                          d.version
d.get_image                     d.post                          d.wait
d.head                          d.prepare_request               

#查看所有镜像
In [3]: d.images()   
Out[3]: 
[{u'Created': 1409856139,
  u'Id': u'68eb857ffb513a3809e94c26925abdff3783dc13e2a96b01d8e74688ab8e48f7',
  u'ParentId': u'34e94e67e63a0f079d9336b3c2a52e814d138e5b3f1f614a0cfe273814ed7c0a',
  u'RepoTags': [u'centos:centos6'],
  u'Size': 212675513,
  u'VirtualSize': 212675513},
 {u'Created': 1409686431,
  u'Id': u'e5322c5256de057b73ca9d2552ecfceaed350dd6f0465d0716cad9f75f9cb374',
  u'ParentId': u'aad454798945a2d2fa67af9b99a4ef4667aa4d4eb5701ba7bc5233c381c47fed',
  u'RepoTags': [u'ubuntu1004/apache2:latest'],
  u'Size': 0,
  u'VirtualSize': 298356004}]
#搜索镜像
In [4]: d.search('ubuntu_sshd')
#下载镜像
In [5]: d.pull('ubuntu_sshd')
#启动容器
In [6]: d.start('apache') 
In [7]: d.start(container="e5322c5256de057b73ca9d2552ecfceaed350dd6f0465d0716cad9f75f9cb374")
#关闭容器
In [8]: d.stop('apache') 
#查看容器日志
In [7]: d.logs(container="e5322c5256de057b73ca9d2552ecfceaed350dd6f0465d0716cad9f75f9cb374")

#关闭容器后,通过inspect再次查看容器状态
In [8]: d.stop('55bd7d4ecd0b')
In [9]: re_info = d.inspect_container('55bd7d4ecd0b')
In [10]: re_info['State']['Running']
Out[10]: False


你可能感兴趣的:(安装docker-python和docker rest api)