设为首页 收藏本站
查看: 1197|回复: 0

[经验分享] 关于docker rest api接口组件docker remote api的使用

[复制链接]

尚未签到

发表于 2018-5-30 10:54:23 | 显示全部楼层 |阅读模式
  前提:
       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

  

   DSC0000.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信息:
   DSC0001.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"  

DSC0002.jpg

  

  

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

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-482996-1-1.html 上篇帖子: 使用python的docker 下篇帖子: 01 命令参数解析
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表