docker_restful_api简介

来源:互联网 发布:linux rpm怎么安装 编辑:程序博客网 时间:2024/05/22 06:49
默认情况下,Docker只允许通过unix socket通信操作Docker daemon,但有时我们想通过HTTP调用其Rest API,需单独配置启动参数

为了使配置永久生效,在Ubuntu环境下修改其配置文件/etc/default/docker,加入DOCKER_OPTS="-H=unix:///var/run/docker.sock -H=0.0.0.0:6732",重启Docker服务,可通过浏览器访问设置主机:端口号(6372)/ Docker API操作Docker.可利用docker的restful api作一个简单的管理工具。

    # Docker Upstart and SysVinit configuration file  
      
    # Customize location of Docker binary (especially for development testing).  
    #DOCKER="/usr/local/bin/docker"  
      
    # Use DOCKER_OPTS to modify the daemon startup options.  
    #DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"  
    DOCKER_OPTS="-H=unix:///var/run/docker.sock -H=0.0.0.0:6732"  
    # If you need Docker to use an HTTP proxy, it can also be specified here.  
    #export http_proxy="http://127.0.0.1:3128/"  
      
    # This is also a handy place to tweak where Docker's temporary files go.  
    #export TMPDIR="/mnt/bigdrive/docker-tmp"  

命令行窗口中用telnet测试HTTP协议

1. 使用telnet连接到docker的HTTP服务器,首先要连接到服务器的6732端口
telnet docker_host_ip 6732
2. 现在已经连接上了服务器,发送http请求消息:
(1)查询现有的containers
GET /containers/json?all=1&before=8dfafdbc3a40&size=1 HTTP/1.1

Query Parameters:

    all – 1/True/true or 0/False/false, Show all containers. Only running containers are shown by default (i.e., this defaults to false)
    limit – Show limit last created containers, include non-running ones.
    since – Show only containers created since Id, include non-running ones.
    before – Show only containers created before Id, include non-running ones.
    size – 1/True/true or 0/False/false, Show the containers sizes
    filters - a json encoded value of the filters (a map[string][]string) to process on the containers list. Available filters:
    exited=<int> -- containers with exit code of <int>
    status=(restarting|running|paused|exited)

Status Codes:

    200 – no error
    400 – bad parameter
    500 – server error

(2)创建containers
POST /containers/create HTTP/1.1
    Content-Type: application/json

    {
         "Hostname": "",
         "Domainname": "",
         "User": "",
         "AttachStdin": false,
    ......
    }

Example response:

    HTTP/1.1 201 Created
    Content-Type: application/json

    {
         "Id":"e90e34656806"
         "Warnings":[]
    }
详见https://docs.docker.com/reference/api/docker_remote_api_v1.18/#list-containers
(3) Inspect 容器

GET /containers/(id)/json
Example request:

    GET /containers/4fa6e0f0c678/json HTTP/1.1

(4)List processes running inside a container

GET /containers/(id)/top

List processes running inside the container id

Example request:

    GET /containers/4fa6e0f0c678/top HTTP/1.1
(5)Get container logs

GET /containers/(id)/logs

Get stdout and stderr logs from the container id

    Note: This endpoint works only for containers with json-file logging driver.

Example request:

   GET /containers/4fa6e0f0c678/logs?stderr=1&stdout=1&timestamps=1&follow=1&tail=10 HTTP/1.1
Query Parameters:

    follow – 1/True/true or 0/False/false, return stream. Default false
    stdout – 1/True/true or 0/False/false, show stdout log. Default false
    stderr – 1/True/true or 0/False/false, show stderr log. Default false
    timestamps – 1/True/true or 0/False/false, print timestamps for every log line. Default false
    tail – Output specified number of lines at the end of logs: all or <number>. Default all
等等。
详见https://docs.docker.com/reference/api/docker_remote_api_v1.18/#list-containers


1 0
原创粉丝点击