Python's docker-py implementation of Docker API operations tutorial

Source: Internet
Author: User
Tags ssh docker ps

Future DBA application may be part of the other department of Docker, there seems to be engaged in a similar Docker platform management system, so, I also want to try to write a simple Doker management platform. As a start I first engage in Docker Api,docker official website has a docker-py, use it is refreshing and simple.

First, install the Docker Python-related modules.

The code is as follows Copy Code
root@dev-ops:~ # pip Install docker-py
Requirement already satisfied (use--upgrade to upgrade): Docker-py In/usr/local/lib/python2. 7/dist-packages
Requirement already satisfied (use--upgrade to upgrade): requests==2.2.1 in/usr/lib/python2.7/ Dist-packages (from docker-py)
Requirement already satisfied (use--upgrade to upgrade): six>=1.3.0 IN/USR/LIB/PYT Hon2.7/dist-packages (from docker-py)
Requirement already satisfied (use--upgrade to upgrade): websocket-client== 0.11.0 in/usr/local/lib/python2.7/dist-packages (from docker-py)
Requirement already satisfied (use--upgrade to UPG Rade): mock==1.0.1 in/usr/local/lib/python2.7/dist-packages (from docker-py)
Requirement already satisfied (use- Upgrade to upgrade): coverage==3.7.1 in/usr/local/lib/python2.7/dist-packages (from docker-py)
Cleaning up ...
root@dev-ops:~#
root@dev-ops:~#




Let's look at the downloaded modules first

The code is as follows Copy Code
[Root@dev-ops ~] $docker Images





REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE


Ubuntu_redis latest 57672d1b5979 about an hour ago 257.6 MB


Rastasheep/ubuntu-sshd 14.04 636d86f228c7 2 days ago 254.9 MB


Rastasheep/ubuntu-sshd 12.04 1147568d245b 6 days ago 144.6 MB


Rastasheep/ubuntu-sshd 13.10 8a63145eed52 6 days ago 231.6 MB


RASTASHEEP/UBUNTU-SSHD latest 47386e322f5d 8 days ago 254.9 MB


Lemonbar/centos6-ssh latest b78c71c001db days ago 296.9 MB


RASTASHEEP/UBUNTU-SSHD 12.10 c9c48c3cb34c 4 weeks ago 188.9 MB


Rastasheep/ubuntu-sshd 13.04 bf49ae3d5534 4 weeks ago 213.1 MB




View containers that have been started

The code is as follows Copy Code
[Root@dev-ops ~] $docker ps-a








CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES


c5f6d0240531 ubuntu_redis:latest/usr/sbin/sshd-d about a hour ago up about a hour 0.0.0.0:49 154->22/tcp redis_sshd


df7b7568fa7a rastasheep/ubuntu-sshd:latest/usr/sbin/sshd-d about a hour ago up about a hour 0.0.0.0:49 153->22/tcp test_sshd





View Docker-related versions

The code is as follows Copy Code
[Root@dev-ops ~] $docker version

Client version:0.9.1
Go version (client): go1.2.1
Git commit (client): 3600720
Server version:0.9.1
Git commit (server): 3600720
Go version (server): go1.2.1
Last stable version:1.1.2, please update Docker
[Root@dev-ops ~]$










OK, so far Docker image has, the container also has, docker-py This API also has ...








Here is the record of my operation, the person who has the experience of shell operation, should be very good to start.








[Root@dev-ops ~] $ipython





Python 2.7.6 (default, Mar 22 2014, 22:59:56)





Type "Copyright", "credits" or "license" for the more information.








IPython 1.2.1--an enhanced Interactive Python.





? -> Introduction and Overview of IPython ' s features.





%quickref-> Quick Reference.





Help-> the Python ' s own Help system.





Object? -> details about ' object ', use ' object?? ' for extra details.








In [1]: Import Docker








Create Docker links, which are connected by sock.





In [2]: c = Docker. Client (base_url= ' Unix://var/run/docker.sock ', version= ' 1.9 ', timeout=10)








We can see that there are a lot of functions inside.





In [3]: C.





C.adapters c.copy c.headers C.login C.proxies C.send





C.attach C.create_container c.history C.logs C.pull C.start





C.attach_socket c.create_container_from_config c.hooks c.max_redirects C.push C.stop





C.auth C.delete c.images C.mount C.put C.stream





C.base_url C.diff c.import_image c.options C.remove_container C.tag





C.build c.events C.info C.params C.remove_image C.top





C.cert C.export C.insert C.patch C.request c.trust_env





C.close C.get C.inspect_container c.ping C.resize c.verify





C.commit C.get_adapter c.inspect_image C.port C.resolve_redirects c.version





C.containers c.get_image C.kill C.post C.restart c.wait





C.cookies C.head c.load_image C.prepare_request C.search





Here is a look at the images mirrors that have been deployed





In [3]: C.images ()





OUT[3]:





[{u ' Created ': 1407902496,





U ' Id ': U ' 57672d1b597912c7b6841c05c0e07e525890cfd64cbd37c84754e7d7e54ee766 ',





U ' parentid ': U ' 47386e322f5deffbc04c777632e1f1ac7e6ddd6909ae6b9ce8d1d78dc2bb6d0c ',





U ' repotags ': [u ' ubuntu_redis:latest '],





U ' Size ': 2675587,





U ' virtualsize ': 257554797},





{u ' Created ': 1407684299,





U ' Id ': U ' 636d86f228c7c6e844d1473245a9580d9c57fd0bfd1b5b9172aa5c168581978c ',





U ' parentid ': U ' e882dd610d1ca64817106dcbda29b2c78536f5fb75f780583cc9fac0efe05398 ',





U ' repotags ': [u ' rastasheep/ubuntu-sshd:14.04 '],





U ' Size ': 0,





U ' virtualsize ': 254925704},





{u ' Created ': 1407342004,





U ' Id ': U ' 1147568d245b369495b19b1943c0946a2b2d823d0a96b48bb7bee5f128090d29 ',





U ' parentid ': U ' 12175cbcbe499c80dd71e3085467d9178a1b5cfa47ecf1463302e09fe2f09e58 ',





U ' repotags ': [u ' rastasheep/ubuntu-sshd:12.04 '],





U ' Size ': 0,





U ' virtualsize ': 144645266},





{u ' Created ': 1407341844,





U ' Id ': U ' 8A63145EED52CF9AE6BDB064F370469D6FA3653F0DA5D370ECE9E270BAF8D9CB ',





U ' parentid ': U ' ca60f67629d2c43012fb53f714e60672c4ad5b61520bb306e3a35614190dcf76 ',





U ' repotags ': [u ' rastasheep/ubuntu-sshd:13.10 '],





U ' Size ': 0,





U ' virtualsize ': 231639963},





{u ' Created ': 1407167259,





U ' Id ': U ' 47386e322f5deffbc04c777632e1f1ac7e6ddd6909ae6b9ce8d1d78dc2bb6d0c ',





U ' parentid ': U ' a8bbc54283bbc341c703970ed429e4def6c034273ede59aaf451449164c95775 ',





U ' repotags ': [u ' rastasheep/ubuntu-sshd:latest '],





U ' Size ': 0,





U ' virtualsize ': 254879210},





{u ' Created ': 1406884906,





U ' Id ': U ' b78c71c001db87eb2da7049f5f9610039afd3484f65e22a3284098792460040d ',





U ' parentid ': U ' e13b39edfcb4c5e15f876c2824fea1d738fb3825437162fad497138123f3235b ',





U ' repotags ': [u ' lemonbar/centos6-ssh:latest '],





U ' Size ': 0,





U ' virtualsize ': 296869021},





{u ' Created ': 1405333140,





U ' Id ': U ' c9c48c3cb34c4b8305d7bae4f0c3c753fbca3ac96195e73e9fdab4a8fa24708a ',





U ' parentid ': U ' 30ffc511bbe11f6205cdc0f2749cadcc0c2eca4f7296285b1af867afcb6ef842 ',





U ' repotags ': [u ' rastasheep/ubuntu-sshd:12.10 '],





U ' Size ': 0,





U ' virtualsize ': 188947523},





{u ' Created ': 1405333120,





U ' Id ': U ' bf49ae3d55344648b354bddf22b2f15c48e2d74e52933404757a9eb470ac35e3 ',





U ' parentid ': U ' 561b2d67fd0a80367e9ab98f9893ce6d36006dbb452e7c8693f028fcaa5f6c7f ',





U ' repotags ': [u ' rastasheep/ubuntu-sshd:13.04 '],





U ' Size ': 0,





U ' virtualsize ': 213124480}]











Here is the search mirror, such as I want to search, Ubuntu Redis words, the results did not search. But the mirror of the search ubuntu_sshd will find some.








In [5]: C.search (' Ubuntu_redis ')





OUT[5]: []








In [6]: C.search (' ubuntu_sshd ')





OUT[6]:





[{u ' description ': U ',





U ' is_official ': False,





U ' is_trusted ': False,





U ' name ': U ' pungoyal/ubuntu_sshd ',





U ' Star_count ': 0}]








Here is the download image





In []: C.pull (' Core9/docker-mongodb ')








In [6]: C.create_container (image= "Ubuntu_redis", command= "/usr/sbin/sshd-d", Name= "Test_nima")





OUT[6]:





{u ' Id ': U ' 15bd8d4ecd1a50604732e1f81763eb347a1cf8937da929799e65261b004894ce ',





U ' warnings ': None}








In [7]:





Start container





In [7]: C.start (container= "15bd8d4ecd1a50604732e1f81763eb347a1cf8937da929799e65261b004894ce")








View Log





In [8]: C.logs (container= "15bd8d4ecd1a50604732e1f81763eb347a1cf8937da929799e65261b004894ce")





OUT[8]: "














We'll try to shut down this container, and then inspect look at his running state.








in [[]: C.stop (' 15bd8d4ecd1a ')





In [m]: Re_info = C.inspect_container (' 15bd8d4ecd1a ')





In [to]: re_info[' state ' [' Running ']





OUT[31]: False











OK, let's look at the results we just created through the Docker shell. When we used the Python Docker API, we created a container called Test_nima.














The question of space is not much said, the API contains almost Docker functions, at least to meet my application, and in the development and operation of the platform docking. Friends of the company has been using pure node.js to write a set of APIs for the company's internal services. In fact, the official website and the community provided several Web UI, but it was written by someone else. It's not easy to change. I'm going to introduce myself in the next article to implement a simple Docker Rest API interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.