API for Docker
Api:application programe Interface, application access interface. By publishing the API, our programs can be invoked in other languages, platforms. If a program has an API, we can develop "client" for it without relying on a specific language. Of course, can also integrate into their own platform, to complete a number of interesting features.
The main type of API: RESTful, based on HTTP protocol, can also be based on RPC protocol.
The so-called RESTful, refers to through such as the HTTP protocol encapsulation of various requests, mainly including get, POST, UPDATE, DELETE, put, save create, modify and other operations. The General RESTful server provides the interface (address, path, parameter) of these requests, and then the client can access through a similar Linux command curl, Python standard library httplib, HTTPLIB2, Urllib, and so on, it should be noted that most parameters (data) are Json/xml packing. And most validate request permissions and security in a token manner. Other types of validation are supported, of course.
You can see the introduction of the Docker API at Docker website:
Docker Remote API
Docker Remote API Client libraries (short rcli)
What's the difference?
Original eco-RESTful standards, official maintenance, support for the latest features
Support for various languages SDK, Client Library, than API version lag.
Like OpenStack's python-*client, rcli is nothing more than a package implementation of the Docker Remote API, maintained by both the official and third parties, but does not guarantee compatibility.
The following is an official description of the introduction of RCLI:
These libraries have not been tested by the Docker for maintainers.
Please file issues with the library owners. If you find more library implementations,
Please list them in Docker Doc Bugs and we'll add the libraries here.
It is to be noted that the authorities have clearly stated that the Remote API has replaced RCLI:
Https://docs.docker.com/reference/api/docker_remote_api
The Remote API is replacing RCLI.
If you have energy, you can encapsulate some methods yourself.
the implementation method of remote Call Docker API
By default, the Docker daemon generates a socket (/var/run/docker.sock) process communication file without a listening port and can only operate Docker on the native machine. If you want to operate the Docker host elsewhere, you need to have the Docker host monitor a port number so that remote operations can be achieved through the port number.
Direct and Docker daemon communication
Using Python to invoke Api,docker provides the Python API Client:
Project Address: Https://github.com/docker/docker-py
Install Docker-py on the Docker host:
Pip Install Docker-py
First use the command line to see the output of the operation:
There are no containers in the running
# docker PS
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NA Mes
There is a container that has been closed
# Docker Ps-a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
050F47812F2C ubuntu:14.04 "/bin/bash" hours ago exited (0) hours ago Hopeful_shockley
Repeat the above command line in an API way, trying to invoke it in the Python interaction:
# Ipython
Import Module
In [1]: Import Docker
Establishing connections and communications with the Docker daemon
In [2]: c = Docker. Client (base_url= ' Unix:///var/run/docker.sock ')
Get the currently running container
In [3]: C.containers ()
OUT[3]: []
Added a parameter: all=true, which lists all the running, closed containers
In [4]: C.containers (all=true)
OUT[4]:
[{u ' Command ': U '/bin/bash ',
U ' Created ': 1410588586,
U ' Id ': U ' 050f47812f2c1925439bbdefb479efb80bc1df007fc45ced1d74a9d483036e9b ',
U ' Image ': U ' ubuntu:14.04 ',
U ' Names ': [u '/hopeful_shockley '],
U ' Ports ': [],
U ' Status ': U ' exited (0) hours ago '}]
The Docker module can implement all operations on the command line, using methods to refer to help, dir's output, or consult the documentation for the DOCKER-PY project
Let Docker listen to the port
Modify the Docker service startup parameter to add a port number that is not occupied:
# Vim/etc/default/docker
Docker_opts= '-H docker01.thstack.com:6732 '
Restarting the Docker service is in effect:
Service Docker restart
Set a docker_host environment variable, you can use the host name or IP address, with the host name when the domain name of the resolution, there is a firewall to add the following port:
# Vim/etc/profile
Export docker_host=tcp://docker01.thstack.com:6732
# Source/etc/profile
Verify
Curl can be verified on this machine or other host, or you can use the Chrome browser plugin postman to verify:
return empty
# curl-k-X ' Get ' Http://docker01.thstack.com:6732/containers/json-H ' Content-type:application/json ' | Python-mjson.tool
Add a parameter to the URL: all=1, get all container
# curl-k-X ' Get ' Http://docker01.thstack.com:6732/containers/json?all=1-H ' Content-type:application/json ' | Python-mjson.tool
% total% Received% Xferd Average Speed time
Dload Upload Total spent left Speed
218 218 0 0 14273 0--:--:----:--:----:--:--14533
[
{
"Command": "/bin/bash",
"Created": 1410588586,
"Id": "050f47812f2c1925439bbdefb479efb80bc1df007fc45ced1d74a9d483036e9b",
"Image": "ubuntu:14.04",
"Names": [
"/hopeful_shockley"
],
"Ports": [],
"Status": "Exited (0) hours ago"
}
]
Specific Docker Remote API parameters can refer to the official API documentation:
https://docs.docker.com/reference/api/docker_remote_api_v1.14/
Use Docker-py to verify:
# Ipython
In [1]: Import Docker
In [2]: c = Docker. Client (base_url= ' http://docker01.thstack.com:6732 ')
In [3]: C.containers (all=true)
OUT[3]:
[{u ' Command ': U '/bin/bash ',
U ' Created ': 1410588586,
U ' Id ': U ' 050f47812f2c1925439bbdefb479efb80bc1df007fc45ced1d74a9d483036e9b ',
U ' Image ': U ' ubuntu:14.04 ',
U ' Names ': [u '/hopeful_shockley '],
U ' Ports ': [],
U ' Status ': U ' exited (0) hours ago '}]
In [4]:
The Docker daemon now defaults to only the ports and does not open the socket entry, allowing both to work at the same time:
# Vim/etc/default/docker
Docker_opts= '-H unix:///var/run/docker.sock-h docker01.thstack.com:6732 '
# Service Docker Restart
Now we're going to connect and operate our own Docker host anywhere.