Home > Operation and Maintenance > Docker > what is docker api

what is docker api

青灯夜游
Release: 2021-12-30 12:07:55
Original
3616 people have browsed it

Docker API refers to the application program interface of docker, which is the agreement for the connection of different components of the software system. Docker mainly has three external APIs: 1. Docker Registry API; 2. Docker Hub API; 3. Docker Remote API.

what is docker api

The operating environment of this tutorial: linux5.9.8 system, docker-1.13.1 version, Dell G3 computer.

1. What is API

1. What is API specifically?

The word API is explained in Wikipedia as follows: Application programming interface (English: application programming interface, abbreviated as API), also known as application programming interface, is a different software system Conventions for the connection of components. After reading this explanation, I guess you are still a little confused, but it doesn't matter. Below we will introduce what an API is in layman's language.

Everyone of us has a mobile phone. When the mobile phone runs out of power, we will definitely find a fixed charger and charging cable to charge it. Use Apple for Apple, use Android for Android. But you will definitely not use an Android cable to charge an Apple phone. The reason is very simple, because your Apple phone has a Lightning interface, and the Android one has a micro interface. If you want to charge or transfer data to your mobile phone, you must buy suitable charging cables and data cables. This is the simplest and easiest understanding of the interface.

Similarly, the same is true for the program interface. Each program has a fixed external standard interface. This interface is defined by the developer who developed the program. If you want to connect to them, you should follow their interface standards.

2. What is REST

# Now learning API, I often see a word called REST, and the full English name is Representational State Transfer. So what is REST? The term REST was proposed by Dr. Roy Fielding, chairman of the Apache Foundation, and its Chinese meaning is "presentation layer state transformation". Chinese is not easy to understand, but if we learn about it from the following aspects, you can probably understand what rest is.

2.1. What is the presentation layer?

The presentation layer here refers to the presentation layer of resources. The so-called "resource" is a specific information on the network. A text, a movie, and a service can be counted as a resource. So what are these resources used to identify and represent? Then you have to use URI. For example, when we download a movie, there must be a corresponding URI address. When we read an online novel, there is also a corresponding URI address. And this address is unique and unique. The resource is identified with a URI, and we can understand that the resource has been "expressed" on the network. So when it comes to this, the meaning of the presentation layer is actually the form in which "resources" are concretely presented.

2.2. What is state transition?

In common sense, if we want to change the state of an object, we definitely need some operations and means. The same is true for resources on the Internet. When you download a movie, you must download it first, and then you can open it and enjoy it. Downloads and acquisitions require the HTTP protocol. In the HTTP protocol, there are four basic operation methods: GET, POST, PUT, and DELETE (get, create, update, delete). Through these four basic methods, some state transformation operations can be performed on resources on the network.

So, REST is the state transformation of the presentation layer. You can understand the above two points separately and then combine them together. It can be understood simply and crudely as: method URI resource.

GET /movie/war/Pearl Harbor

DELETE /movie/war/Pearl Harbor

...

2. Docker API Type

Docker’s API also follows the rest style, so after we understand the above two points, we start to learn the relevant knowledge of docker’s own API.

First of all, we regard docker as a resource. We can operate docker through the api, and the operation methods are also the same as http.

Secondly, we need to understand what APIs Docker has that can be used externally. Here Docker officially has three major external APIs

  • Docker Registry API
  • Docker Hub API
  • Docker Remote API

1. Docker Registry API

This is the api of the docker image warehouse. By operating this API, you can Freely automate and programmatically manage your mirror warehouse.

2. Docker Hub API

Docker Hub API is an API for user management operations. Docker Hub uses verification and public namespaces to store account information and authentication. account and authorize the account. The API also allows operations on related user repositories and library repositories.

3. Docker Remote API

This API is used to control the host Docker server API, which is equivalent to the docker command line client. With it, you can remotely operate docker containers, and more importantly, you can automatically operate and maintain docker processes through programs.

3. Preparation before using the API

As we said before, the methods of http are used to operate rest api. So how to use these methods specifically? Here we provide several common ways to operate and call the docker API, and then experience it. Before experiencing it, we need to enable the docker rest api, otherwise you will not be able to use it if it is not enabled. Specific opening method:

$ vim /usr/lib/systemd/system/docker.service
Copy after login

Add -H tcp://0.0.0.0:8088 -H unix:///var/run/docker.sock directly after ExecStart=/usr/bin/dockerd (note that port 8088 can be defined by yourself, don’t Just conflict with the current one)

$ systemctl daemon-reload
$ systemctl restart docker
Copy after login

After the restart is completed, we execute the curl 127.0.0.1:8088/info | python -mjson.tool command to view the status of docker (in json form, python -mjson. tool borrowed this tool to format json for easy reading)

After enabling the docker API, we still have a question, that is, where can we query the existing API of docker? Since docker provides the three major API libraries: Docker Registry API, Docker Hub API, and Docker Remote API. So where can I view specific and detailed APIs, such as what API addresses are there under the Docker Registry API? Is there an API for querying images? Have any been deleted? In fact, all of these are available. We can go directly to the official website API manual to check it. The address is: https://docs.docker.com/engine/api/v1.38/ (Which version do you want to see? Just replace the last v1.38 with the target version number)

It should be noted here that the official no longer recommends using versions before API v1.12. It is recommended to use v1.24 or higher versions. .

To check the local docker API version, you can use the docker version command:

4. How to operate the docker API

1. Simple curl method

I think everyone is familiar with the CURL command, which is installed by default under Linux. Many methods of testing http can directly use CURL.

For example, if we check the detailed information of docker images, we can directly use curl to retrieve it:

 $ curl -X GET http://127.0.0.1:8088/images/json
Copy after login

This way the display will be more confusing, we can Add python -mjson.tool after the command to format

$  curl -X GET http://127.0.0.1:8088/images/json | python -mjson.tool
Copy after login

. The result format will be more standardized and easier to read.

View all containers containers:

$ curl -X GET http://127.0.0.1:8088/containers/json | python -mjson.tool
Copy after login

Create a containers container:

Here create a container for the mariadb database, set the password to 123456, and the listening port is 3306

$ curl -X POST -H "Content-Type: application/json" -d '{
    "Image": "mariadb",
    "Env": ["MYSQL_ROOT_PASSWORD=123456"],
    "ExposedPorts": {
        "3306/tcp": {}
    },
    "HostConfig": {
        "PortBindings": {
            "3306/tcp": [{"HostIp": "","HostPort": "3306"}]
        }
    },
    "NetworkSettings": {
        "Ports": {
            "5000/tcp": [{"HostIp": "0.0.0.0","HostPort": "3306"}]
        }
    }
}' http://127.0.0.1:8088/containers/create
Copy after login

Start/stop/restart a containers container:

$ curl -X POST http://127.0.0.1:8088/containers/{id}/start   (注意这里是POST方法)
$ curl -X POST http://127.0.0.1:8088/containers/{id}/stop   (注意这里是POST方法)
$ curl -X POST http://127.0.0.1:8088/containers/{id}/restart   (注意这里是POST方法)
...
Copy after login

There are many API methods, you can log in to the link mentioned above to view

https:/ /docs.docker.com/engine/api/v1.38

2. Python program script method

Python is very powerful and everyone recognizes this. Many automation scenarios now use python to load third-party corresponding libraries, and then write business logic to automate devops operation and maintenance. Docker also provides a very powerful library for Python, called docker. We can log in to the official python sdk address to learn how python specifically operates docker.

The address is: https://docker-py.readthedocs.io/en/stable/

2.1. Install docker python library

$ pip install docker

2.2. Start using

import docker
client = docker.DockerClient(base_url='unix://var/run/docker.sock', version="auto")
client.containers.run("ubuntu", "echo hello world")
Copy after login

This is a very simple usage example, we can analyze it:

The first line indicates the introduction of the third-party library docker.

The second line is used to configure the basic information of the Docker server, including base_url (the address of the Docker server) and version (auto can automatically check the version of docker).

The third line is equivalent to running a docker run ubuntu echo hello world command.

2.3. Advanced use

import docker
client = docker.DockerClient(base_url="tcp://ip:port")
client.images.list()       # 类似docker images命令,显示image的信息列表
client.containers.list()       #  类似docker ps命令
client.containers.list(all=True)       # 类似docker ps -a命令
container = client.containers.get(container_id)           # 获取daodocker容器,这里container_id 是你要输入的具体容器id
container.start()            # 类似docker start 传入具体的容器id ,开启容器
Copy after login

Summary: Now many companies have entered the era of automated operation and maintenance, so it is very useful to master the skills and rules of using APIs necessary. Above we have roughly introduced the introduction to the docker api. In fact, you have to be very good at it. There is a lot of flexibility and complexity here, but here you need some knowledge of script programming.

Recommended learning: "docker video tutorial"

The above is the detailed content of what is docker api. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template