1.docker software comes with Docker Rest API, without installation;
Need to set the next Docker configuration file
Vi/etc/default/docker.io or Vi/etc/default/docker #追加下面一行DOCKER_OPTS = "-H tcp://0.0.0.0:5555-h unix://var/run/ Docker.sock "
Restart Docker, service Docker restart.
Usage:
#1. View all mirrors #curl ' http://127.0.0.1:5555/images/json?all=0 ' | Python-m Json.tool #2. View the running container #curl http://127.0.0.1:5555/containers/json#curl http://127.0.0.1:5555/containers/ JSON |python-m json.tool#3. Viewing a container specific information #curl-xget ' Http://127.0.0.1:4243/containers/df7b7568fa7a/json ' | Python-m json.tool#4. Close, open container #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. Installing Docker-python
#apt-get Install Python-pip#pip install docker-py
Usage:
[email protected]:~# ipythonpython 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 dockerIn [2]: docker.docker.client docker.client docker.ssladapter docker.unixconn docker.versiondocker.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.proxiesd.attach d.history d.pulld.attach_socket d.hooks d.pushd.auth d.images d.putd.base_url d.import_image d.remove_containerd.build d.info d.remove_imaged.cert d.insert d.requestd.close d.inspect_container d.resized.commit d.inspect_image d.resolve_redirectsd.containers d.kill d.restartd.cookies d.load_image d.searchd.copy d.login d.sendd.create_container d.logs d.startd.create_container_ from_config d.max_redirects d.stopd.delete d.mount d.streamd.diff d.options d.tagd.events d.params d.topd.export d.patch d.trust_envd.get d.ping d.verifyd.get_ Adapter d.port d.versiond.get_image d.post d.waitd.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 ")#关闭容器后, check the container status in [8]: d.stop (' 55bd7d4ecd0b ') again via inspect in [9]: re_info = D.inspect_container (' 55bd7d4ecd0b ') in [10]: re_info[' state ' [' Running ']out[10]: false
Installing the Docker-python and Docker rest APIs