首页 > 代码库 > 关于docker rest api接口组件docker remote api的使用

关于docker rest api接口组件docker remote api的使用

前提:

     docker remote api 是docker自带的一个rest api 管理。

     只要在配置文件里面申明下就可以了。


下面是docker remote api的官方的功能目录:

  • Docker Remote API v1.3

    • 3.1 Inside ‘docker run’

    • 3.2 Hijacking

    • 3.3 CORS Requests

    • 2.1 Containers

    • 2.2 Images

    • 2.3 Misc

    • List containers

    • Create a container

    • Inspect a container

    • List processes running inside a container

    • Inspect changes on a container’s filesystem

    • Export a container

    • Start a container

    • Stop a contaier

    • Restart a container

    • Kill a container

    • Attach to a container

    • Wait a container

    • Remove a container

    • List Images

    • Create an image

    • Insert a file in a image

    • Inspect an image

    • Get the history of an image

    • Push an image on the registry

    • Tag an image into a repository

    • Remove an image

    • Search images

    • Build an image from Dockerfile via stdin

    • Check auth configuration

    • Display system-wide information

    • Show the docker version information

    • Create a new image from a container’s changes

    • Monitor Docker’s events

    • 1. Brief introduction

    • 2. Endpoints

    • 3. Going further


wKiom1PrcYvTKgqfAAD_lg8pjg8050.jpg


通过接口获取所有的镜像 !

[root@dev-ops ~]$curl ‘http://127.0.0.1:4243/images/json?all=0‘| python -m json.tool 


原文:   
[
    {
        "Created": 1407902496,
        "Id": "57672d1b597912c7b6841c05c0e07e525890cfd64cbd37c84754e7d7e54ee766",
        "ParentId": "47386e322f5deffbc04c777632e1f1ac7e6ddd6909ae6b9ce8d1d78dc2bb6d0c",
        "RepoTags": [
            "ubuntu_redis:latest"
        ],
        "Size": 2675587,
        "VirtualSize": 257554797
    },
    {
        "Created": 1407684299,
        "Id": "636d86f228c7c6e844d1473245a9580d9c57fd0bfd1b5b9172aa5c168581978c",
        "ParentId": "e882dd610d1ca64817106dcbda29b2c78536f5fb75f780583cc9fac0efe05398",
        "RepoTags": [
            "rastasheep/ubuntu-sshd:14.04"
        ],
        "Size": 0,
        "VirtualSize": 254925704
    },
    {
        "Created": 1407342004,
        "Id": "1147568d245b369495b19b1943c0946a2b2d823d0a96b48bb7bee5f128090d29",
        "ParentId": "12175cbcbe499c80dd71e3085467d9178a1b5cfa47ecf1463302e09fe2f09e58",
        "RepoTags": [
            "rastasheep/ubuntu-sshd:12.04"
        ],
        "Size": 0,
        "VirtualSize": 144645266
    },
    {
        "Created": 1407341844,
        "Id": "8a63145eed52cf9ae6bdb064f370469d6fa3653f0da5d370ece9e270baf8d9cb",
        "ParentId": "ca60f67629d2c43012fb53f714e60672c4ad5b61520bb306e3a35614190dcf76",
        "RepoTags": [
            "rastasheep/ubuntu-sshd:13.10"
        ],
        "Size": 0,
        "VirtualSize": 231639963
    },
    {
        "Created": 1407167259,
        "Id": "47386e322f5deffbc04c777632e1f1ac7e6ddd6909ae6b9ce8d1d78dc2bb6d0c",
        "ParentId": "a8bbc54283bbc341c703970ed429e4def6c034273ede59aaf451449164c95775",
        "RepoTags": [
            "rastasheep/ubuntu-sshd:latest"
        ],
        "Size": 0,
        "VirtualSize": 254879210
    },
    {
        "Created": 1406884906,
        "Id": "b78c71c001db87eb2da7049f5f9610039afd3484f65e22a3284098792460040d",
        "ParentId": "e13b39edfcb4c5e15f876c2824fea1d738fb3825437162fad497138123f3235b",
        "RepoTags": [
            "lemonbar/centos6-ssh:latest"
        ],
        "Size": 0,
        "VirtualSize": 296869021
    },
    {
        "Created": 1405333140,
        "Id": "c9c48c3cb34c4b8305d7bae4f0c3c753fbca3ac96195e73e9fdab4a8fa24708a",
        "ParentId": "30ffc511bbe11f6205cdc0f2749cadcc0c2eca4f7296285b1af867afcb6ef842",
        "RepoTags": [
            "rastasheep/ubuntu-sshd:12.10"
        ],
        "Size": 0,
        "VirtualSize": 188947523
    },
    {
        "Created": 1405333120,
        "Id": "bf49ae3d55344648b354bddf22b2f15c48e2d74e52933404757a9eb470ac35e3",
        "ParentId": "561b2d67fd0a80367e9ab98f9893ce6d36006dbb452e7c8693f028fcaa5f6c7f",
        "RepoTags": [
            "rastasheep/ubuntu-sshd:13.04"
        ],
        "Size": 0,
        "VirtualSize": 213124480
    }]


原文: http://rfyiamcool.blog.51cto.com/1030776/1539677     

#这里是查看已经启动了的docker容器情况 !


root@dev-ops:~# curl http://127.0.0.1:4243/containers/json | python -m json.tool

[
    {
        "Command": "/usr/sbin/sshd -D",
        "Created": 1407902166,
        "Id": "df7b7568fa7a8c65e7ce857378a01ed127109b4de5e6508123e4797a5c54bfbe",
        "Image": "rastasheep/ubuntu-sshd:latest",
        "Names": [
            "/test_sshd"
        ],
        "Ports": [
            {
                "IP": "0.0.0.0",
                "PrivatePort": 22,
                "PublicPort": 49153,
                "Type": "tcp"
            }
        ],
        "Status": "Up 3 hours"
    }
]


#查看某个容器的具体信息

root@dev-ops:~# curl -XGET ‘http://127.0.0.1:4243/containers/df7b7568fa7a/json‘ | python -m json.tool

{
    "Args": [
        "-D"
    ],
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "/usr/sbin/sshd",
            "-D"
        ],
        "CpuShares": 0,
        "Dns": null,
        "Domainname": "",
        "Entrypoint": null,
        "Env": [
            "HOME=/",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "ExposedPorts": {
            "22/tcp": {}
        },
        "Hostname": "df7b7568fa7a",
        "Image": "rastasheep/ubuntu-sshd",
        "Memory": 0,
        "MemorySwap": 0,
        "NetworkDisabled": false,
        "OnBuild": null,
        "OpenStdin": false,
        "PortSpecs": null,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": null,
        "VolumesFrom": "",
        "WorkingDir": ""
    },
    "Created": "2014-08-13T03:56:06.602313986Z",
    "Driver": "aufs",
    "ExecDriver": "native-0.1",
    "HostConfig": {
        "Binds": null,
        "ContainerIDFile": "",
        "Links": null,
        "LxcConf": [],
        "PortBindings": {
            "22/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "49153"
                }
            ]
        },
        "Privileged": false,
        "PublishAllPorts": true
    },
    "HostnamePath": "/var/lib/docker/containers/df7b7568fa7a8c65e7ce857378a01ed127109b4de5e6508123e4797a5c54bfbe/hostname",
    "HostsPath": "/var/lib/docker/containers/df7b7568fa7a8c65e7ce857378a01ed127109b4de5e6508123e4797a5c54bfbe/hosts",
    "ID": "df7b7568fa7a8c65e7ce857378a01ed127109b4de5e6508123e4797a5c54bfbe",
    "Image": "47386e322f5deffbc04c777632e1f1ac7e6ddd6909ae6b9ce8d1d78dc2bb6d0c",
    "Name": "/test_sshd",
    "NetworkSettings": {
        "Bridge": "docker0",
        "Gateway": "172.17.42.1",
        "IPAddress": "172.17.0.3",
        "IPPrefixLen": 16,
        "PortMapping": null,
        "Ports": {
            "22/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "49153"
                }
            ]
        }
    },
    "Path": "/usr/sbin/sshd",
    "ResolvConfPath": "/etc/resolv.conf",
    "State": {
        "ExitCode": 0,
        "FinishedAt": "2014-08-13T06:42:46.204526996Z",
        "Ghost": false,
        "Pid": 4222,
        "Running": true,
        "StartedAt": "2014-08-13T06:42:46.225958747Z"
    },
    "Volumes": {},
    "VolumesRW": {}
}


创建一个容器 !


[root@dev-ops ~]$curl -XPOST -H "Content-Type: application/json"  http://127.0.0.1:4243/containers/create -d ‘{ 

     "Hostname":"", 

     "User":"", 

     "Memory":0, 

     "MemorySwap":0, 

     "AttachStdin":false, 

     "AttachStdout":true, 

     "AttachStderr":true, 

     "PortSpecs":null, 

     "Privileged": false, 

     "Tty":false, 

     "OpenStdin":false, 

     "StdinOnce":false, 

     "Env":null, 

     "Dns":null, 

     "Image":"rastasheep/ubuntu-sshd", 

     "Volumes":{}, 

     "VolumesFrom":"", 

     "WorkingDir":"" 

}‘ 

{"Id":"d0274fdba510f643a0398cac36ee4caf9615b74e72452ff22951c96697f22c29","Warnings":null}


可能会返回的状态 !

config – the container’s configuration

Status Codes:

201 – no error

404 – no such container

406 – impossible to attach (container not running)

500 – server error


#我们查看下结果

[root@dev-ops ~]$docker ps -a

CONTAINER ID        IMAGE                           COMMAND             CREATED              STATUS              PORTS                            NAMES
d0274fdba510        rastasheep/ubuntu-sshd:latest   /usr/sbin/sshd -D   About a minute ago   Up 7 seconds        22/tcp                           silly_turing        
2602cb4fb43b        rastasheep/ubuntu-sshd:latest   /usr/sbin/sshd -D   2 minutes ago        Up 56 seconds       22/tcp                           angry_lovelace      
1d20403be200        ubuntu_redis:latest             /usr/sbin/sshd -D   3 hours ago          Up 3 minutes        22/tcp, 0.0.0.0:6379->6379/tcp   redis_6379          
c5f6d0240531        ubuntu_redis:latest             /usr/sbin/sshd -D   6 hours ago          Exit 0                                               redis_sshd          
df7b7568fa7a        rastasheep/ubuntu-sshd:latest   /usr/sbin/sshd -D   6 hours ago          Up 3 hours          0.0.0.0:49153->22/tcp            test_sshd


查看容器内的top信息:

wKioL1PrdmSxycPkAAGX9o6qI90787.jpg


原文: http://rfyiamcool.blog.51cto.com/1030776/1539677 

关闭容器,启动容器:

root@dev-ops:~# curl -s -XPOST "http://127.0.0.1:4243/containers/d0274fdba510/stop" 
root@dev-ops:~# 
root@dev-ops:~# curl -s -XPOST "http://127.0.0.1:4243/containers/d0274fdba510/start"


wKiom1PrdDTQs0bAAAO4vClEaqk448.jpg



我这边就简单说下,详细的功能实现大家自己翻墙到官网look下吧。 说来也怪,docker时不时的抽风被墙 。我看了国外的论坛,貌似有些公司做了基于docker remote api实现的管理平台。   



本文出自 “峰云,就她了。” 博客,谢绝转载!