Docker Remore API的使用范例:
为了可以远程使用docker API,需要设置以下docker的运行模式,默认监听在sock上,修改/etc/default/docker文件,DOCKER_OPTS="-H tcp://0.0.0.0:4243 -H unix://var/run/docker.sock"
docker容器信息
containers
1 列出容器
GET /containers/json 参数 all:1表示真,0表示假,显示所有的容器,默认显示只在运行的容器 limit:显示最后创建的哪些容器,包括未运行的容器 since:显示容器的ID,包括未运行的容器 before:显示在哪个容器的ID创建之前的容器,包括未运行的容器 size:1表示真,0表示假,显示容器的大小 curl -s -XGET 'http://0.0.0.0:4243/containers/json?all=1'| python -m json.tool2 检查容器
GET /containers/(container id)/json curl -s -XGET 'http://0.0.0.0:4243/containers/7101b7cbb261/json'| python -m json.tool3 创建容器
POST /containers/create 参数 config:容器的配置文件 或者使用json格式的POST数据 curl -s -XGET 'http://0.0.0.0:4243/containers/create HTTP/1.1 Content-Type: application/json { }
例如:
curl -XPOST -H "Content-Type: application/json" http://0.0.0.0: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":"vieux/elasticsearch", "Volumes":{}, "VolumesFrom":"", "WorkingDir":"" }'
4 列出正在运行的容器里面的进程
GET /containers/(container id)/top 参数 ps_args:传递给ps的参数,例如使用aux参数 curl -s -XGET 'http://0.0.0.0:4243/containers/7101b7cbb261/top'| python -m json.tool5 获取一个容器的logs
GET /containers/(container id)/logs 参数 follow:1代表真,0代表假,返回stream流日志,默认是假 stdout:1代表真,0代表假,如果logs=true,则返回stdout log,默认是假 stderr:1代表真,0代表假,如果logs=true,则返回stderr log,默认是假 timestamps:1代表真,0代表假,如果logs=true,则在每一行日志前打印一个timestamps,默认是假 curl -s -XGET 'http://0.0.0.0:4243/containers/7101b7cbb261/logs?stdout=1&follow=1×tamps=1'6 检查一个容器的改变
GET /containers/(container id)/changes curl -s -XGET 'http://0.0.0.0:4243/containers/7101b7cbb261/changes'|python -m json.tool7 导出容器内容
GET /containers/(container id)/export #导出一个容器,千万不要随便尝试 curl -s -XGET 'http://0.0.0.0:4243/containers/7101b7cbb261/export'8 启动一个容器
POST /containers/(container id)/start curl -s -XPOST 'http://0.0.0.0:4243/containers/a9c07396bd0b/start'9 停止一个容器
POST /containers/(container id)/stop 参数 t:等待多少秒之后再停止容器 curl -s -XPOST 'http://0.0.0.0:4243/containers/a9c07396bd0b/stop'10 重启一个容器
POST /containers/(container id)/restart 参数 t:等待多少秒之后再重启容器 curl -s -XPOST 'http://0.0.0.0:4243/containers/a9c07396bd0b/restart'11 杀死一个容器
POST /containers/(container id)/kill 参数 signal:发送一个什么信号给容器,可以是数字也可以是“SIGINT”12 附加到一个容器上
POST /containers/(container id)/attach 参数 logs:1代表真,0代表假,返回logs,默认是假 stream:代表真,0代表假,返回stream,默认是假 stdout:1代表真,0代表假,如果logs=true,则返回stdout log,如果stream=true,则attach到stdout上,默认是假 stderr:1代表真,0代表假,如果logs=true,则返回stderr log,如果stream=true,则attach到stderr上,默认是假 stdin:1代表真,0代表假,如果stream=true,则attach到stdin上,默认是假 curl -s -XPOST 'http://0.0.0.0:4243/containers/7101b7cbb261/attach?logs=1&stream=1&stdout=1'13 阻止一个容器直到该容器退出
POST /containers/(container id)/wait curl -s -XPOST 'http://0.0.0.0:4243/containers/7101b7cbb261/wait'14 删除一个容器
DELETE /comtainers/(container id) 参数 v:1代表真,0代表假,删除volumes和容器的联系,默认是假 force:1代表真,0代表假,即使容器在运行,也删除容器 curl -s -XDELETE 'http://0.0.0.0:4243/containers/3457e4cd8398'
15 从容器里面拷贝文件或者目录
POST /containers/(container id)/copy
images
1 列出镜像
GET /images/json 参数 all:1代表真,0代表假,默认是假 filters:一个json格式的键值对 curl -s -XGET 'http://0.0.0.0:4243/images/json?all=0'| python -m json.tool2 创建一个镜像
POST /images/create 参数 fromImage: fromSrc: repo:仓库 tag:tag标记 registry:3 给镜像中插入一个文件
POST /images/(image name)/insert #从url中取出文件插入到镜像的path里面 curl -s -XGET 'http://0.0.0.0:4243/images/insert?url=http://127.0.0.1/index.php&path=/opt'4 检查一个镜像
GET /images/(image name)/json curl -s -XGET 'http://0.0.0.0:4243/images/ubuntu14/json'| python -m json.tool5 获取镜像的历史
GET /images/(image name)/history curl -s -XGET 'http://0.0.0.0:4243/images/ubuntu14/history'| python -m json.tool6 push镜像到仓库
POST /images/(image name)/push 参数 registry:哪个registry你想push7 给仓库的镜像打tag
POST /images/(image name)/tag 参数 repo:The repository to tag in force:1代表真,0代表假,默认是假8 删除一个镜像
DELETE /images/(image name) 参数 force:1代表真,0代表假,默认是假 noprune:1代表真,0代表假,默认是假 curl -s -XDELETE 'http://0.0.0.0:4243/images/ubuntu14'9 搜索镜像
GET /images/search 参数 term:搜索哪个镜像 curl -s -XGET http://0.0.0.0:4243/images/search?term=nginx10 利用Dockfile构建镜像
POST /build 参数 t:repository名称 q:安静模式 nocache:构建镜像时不使用cache rm:容器成功构建成功后删除中间层容器 forcerm:删除中间层容器11 检查认证
POST /auth12 显示系统信息
GET /info curl -s -XGET 'http://0.0.0.0:4243/info'| python -m json.tool13 显示docker版本信息
GET /version curl -s -XGET 'http://0.0.0.0:4243/version'| python -m json.tool14 ping docker server是否存活
GET /_ping curl -s -XGET 'http://0.0.0.0:4243/_ping'15 利用现有的容器创建镜像,也就是commit
POST /commit16 监控docker事件
GET /events 参数 since:timestamp until:timestamp