Docker Remote API 使用详解

文章目录

  • 1.容器相关
    • 1.1 列出所有容器
    • 1.2 创建新容器
    • 1.3 检查容器
    • 1.4 启动容器
    • 1.5 停止容器
    • 1.6 重启容器
    • 1.8 删除容器
    • 1.14 根据资源使用情况获取容器统计信息
    • 1.16 获取容器日志
  • 2.镜像相关
    • 2.1 获取镜像清单
    • 2.2 构建镜像
    • 2.4 检查镜像
    • 2.5 获取镜像的历史记录
    • 2.6 标记一个镜像
    • 2.8 删除镜像
    • 2.9 删除全部镜像

Docker Remote API是一个取代远程命令行界面(rcli)的REST API。本文中,我们将使用命令行工具cURL来处理url相关操作。cURL可以发送请求、获取以及发送数据、检索信息。

本文档能够完成,主要借助于以下文档

  • docker api 官方文档
  • dockerone组织翻译文档

1.容器相关

1.1 列出所有容器

GET /containers/json

URL:

  • /containers/json

查询参数:

  • all: 获取所有容器信息,默认为false,仅显示正在运行的容器
  • limit:返回最近创建的容器的数量,包括未运行的容器。例如:limit=5 ,则返回5个最近创建的5个容器信息
  • size:将容器的大小作为字段SizeRw和SizeRootFs返回,默认为false
  • filters:过滤器处理容器列表,编码为JSON (map[string][]string)。例如,{“status”: [" stopped "]}将只返回暂停的容器

请求示例:

  • 获取正在运行中的容器列表

curl http://127.0.0.1:5678/containers/json

  • 查询全部的容器(包含不是正在运行的容器)

curl http://127.0.0.1:5678/containers/json?all=1

响应结果:

[root@localhost ~]# curl http://10.20.29.21:6666/containers/json?all=true
[{"Id":"15ffc3340941281cd6f515c1d34d15ee0cea3ee70cd1a0e34e39f94fcf06c98b","Names":["/shipyard-swarm-agent"],"Image":"docker.io/swarm:latest","ImageID":"sha256:ff454b4a0e84e50c50e3df457ad44b80f1a67ec6ec75cff5117dd5dc46bf4965","Command":"/swarm join --addr 10.20.29.21:6666 token://a997b11fdba7ede2551f9f8a17483bee","Created":1554858486,"Ports":[{"PrivatePort":2375,"Type":"tcp"}],"Labels":{},"State":"running","Status":"Up About an hour","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"74227783d8af05a30ae0d8817d984e815144387c1b1e4b7c6b9b10915579b61e","EndpointID":"9f85ced521dd4d081954de20af5350672fa08de18882614005d10ad2b94ff8b9","Gateway":"172.17.0.1","IPAddress":"172.17.0.4","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:04","DriverOpts":null}}},"Mounts":[{"Type":"volume","Name":"534f08f77e48bbc588756c5c1113807a3a0cbe8c6e073fb781fe6fd04a1d0d1c","Source":"","Destination":"/.swarm","Driver":"local","Mode":"","RW":true,"Propagation":""}]}
......

注:可通过python -mjson.tool可以将JSON数据格式化显示

成功后返回的参数:

  • ID:这个容器的ID
  • Names:这个容器的名称
  • Image:创建此容器时使用的镜像的名称
  • ImageID::创建此容器时使用的镜像的ID
  • Command:启动容器时运行的命令
  • Created:容器创建时间
  • Ports:这个容器暴露的端口
  • State:这个容器的状态(例如:已关闭)
  • Status:容器运行时长或关闭时长
  • NetworkSettings :容器网络设置的摘要信息
  • Mounts:容器的文件或目录挂载信息

响应状态码解释

  • 204 没有错误
  • 400 参数不对
  • 500 服务器错误

1.2 创建新容器

POST /containers/create

URL:

  • /containers/create

查询参数:

  • name:即将被创建容器的名称

创建容器参数:

  • Hostname:用于容器的主机名
  • Domainname:用于容器的域名
  • User:在容器内运行命令的用户
  • AttachStdin:是否连接到Stdin,默认为false
  • AttachStdout:是否连接到Stdout,默认为false
  • AttachStderr:是否连接到Stderr,默认为false
  • ExposedPorts:端口映射(容器需要暴露的端口)
  • PortBindings:公开的容器端口和它们应该映射到的主机端口的映射
  • Tty:将标准流附加到TTY,包括stdin它是否未关闭
  • OpenStdin:打开标准输入
  • StdinOnce:连接的客户端断开后关闭标准输入
  • Env:设置容器内部环境变量
  • Cmd:需要在容器内部执行的命令,命令以字符串的形式运行
  • Healthcheck:检查容器是否健康的运行
  • Image:创建容器时使用的镜像(名称)
  • Volumes:宿主机与容器之间文件或目录的映射
  • WorkingDir:运行命令的工作目录,即容器工作目录
  • Entrypoint:指定字符串命令所在目录
  • NetworkDisabled:是否禁用容器网络
  • MacAddress:容器的MAC地址
  • Labels:添加一个标签到容器
  • StopSignal:发出停止容器的信号
  • StopTimeout:以秒为单位停止容器的超时
  • HostConfig:依赖于我们所运行的主机的容器配置
  • NetworkingConfig :这个容器的网络配置

1、请求示例:

仅映射端口:

  • curl -X POST -H “Content-Type: application/json” -d ‘{“AttachStdin”: false,“AttachStdout”: true,“AttachStderr”: true,“Tty”: false,“OpenStdin”: false,“StdinOnce”: false,“Image”: “nginx:1.14”,“Volumes”: {“/mnt/data”:{}},“PortBindings”:{ “80/tcp”:[{“HostPort”: “8888”}]} }’ http://127.0.0.1:6666/containers/create?name=nginx

响应结果:

[root@localhost wasm]# curl -X POST -H "Content-Type: application/json" -d '{"AttachStdin": false,"AttachStdout": true,"AttachStderr": true,"Tty": false,"OpenStdin": false,"StdinOnce": false,"Image": "nginx:1.14","Volumes": {"/mnt/data":{}},"PortBindings":{ "80/tcp":[{"HostPort": "8888"}]} }' http://127.0.0.1:6666/containers/create?name=nginx-test
{"Id":"5cee7175339b9cafbfa7b66e4b719aa4261a1f018f8f33dff4a50cf0ee73fc0f","Warnings":null}

2、请求示例:

映射端口及目录

  • curl -X POST -H “Content-Type: application/json” -d ‘{“AttachStdin”: false,“AttachStdout”: true,“AttachStderr”: true,“Tty”: false,“OpenStdin”: false,“StdinOnce”: false,“Image”: “zht-baseclient:v1.0”,“Binds”:[“/mnt/app/zht/baseclient/project:/data”,“/etc/localtime:/etc/localtime”,“/etc/timezone:/etc/timezone”],“ExposedPorts”: {“8081/tcp”: {}},“PortBindings”:{“8081/tcp”:[{“HostIp”:“”,“HostPort”:“18081”}]}}’ http://127.0.0.1:2375/containers/create?name=baseclient

响应结果:

[root@localhost baseclient]# curl -X POST -H "Content-Type: application/json" -d '{"AttachStdin": false,"AttachStdout": true,"AttachStderr": true,"Tty": false,"OpenStdin": false,"StdinOnce": false,"Image": "zht-baseclient:v1.0","Binds":["/mnt/app/zht/baseclient/project:/data","/etc/localtime:/etc/localtime","/etc/timezone:/etc/timezone"],"ExposedPorts": {"8081/tcp": {}},"PortBindings":{"8081/tcp":[{"HostIp":"","HostPort":"18081"}]}}' http://127.0.0.1:2375/containers/create?name=baseclient
{"Id":"8c0ddcb1921760ccf3cad0279cc8281a3d81a87f58c37d300d558685a47e8c8f","Warnings":null}

3、请求示例:
映射端口及使用host模式映射端口

  • curl -X POST -H “Content-Type: application/json” -d ‘{“AttachStdin”: false,“AttachStdout”: true,“AttachStderr”: true,“Tty”: false,“OpenStdin”: false,“StdinOnce”: false,“Image”: “zht-baseclient:v1.0”,“Binds”:[“/mnt/app/zht/baseclient/project:/data”,“/etc/localtime:/etc/localtime”,“/etc/timezone:/etc/timezone”],“NetworkMode”:“host”,“PortBindings”:{},“RestartPolicy”:{“Name”:“unless-stopped”,“MaximumRetryCount”:0}}’ http://127.0.0.1:2375/containers/create?name=baseclient

响应结果:

[root@localhost ~]# curl -X POST -H "Content-Type: application/json" -d '{"AttachStdin": false,"AttachStdout": true,"AttachStderr": true,"Tty": false,"OpenStdin": false,"StdinOnce": false,"Image": "zht-baseclient:v1.0","Binds":["/mnt/app/zht/baseclient/project:/data","/etc/localtime:/etc/localtime","/etc/timezone:/etc/timezone"],"NetworkMode":"host","PortBindings":{},"RestartPolicy":{"Name":"unless-stopped","MaximumRetryCount":0}}' http://127.0.0.1:2375/containers/create?name=baseclient
{"Id":"fda7518cd757e604d963c91f3684317c5f29e27db6a186bed74b7f94acff6a16","Warnings":null}

以上api各种使用方法灵感来源于:

hostconfig.json文件

文件路径:/var/lib/docker/containers/容器ID/hostconfig.json

[root@localhost 35a2f9491f6ad3d43a8e7c8b62acec1ccc82e90db844988b5a3c5f2ac630da5c]# cat hostconfig.json
{"Binds":["/etc/localtime:/etc/localtime","/etc/timezone:/etc/timezone","/mnt/public/mysql/data:/var/lib/mysql","/mnt/public/mysql/my.cnf:/etc/mysql/my.cnf"],"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"default","PortBindings":{"3306/tcp":[{"HostIp":"","HostPort":"13306"}]},"RestartPolicy":{"Name":"unless-stopped","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"shareable","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":[],"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DeviceCgroupRules":null,"DiskQuota":0,"KernelMemory":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":false,"PidsLimit":0,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/asound","/proc/acpi","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/timer_list","/proc/timer_stats","/proc/sched_debug","/proc/scsi","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]}

注意:此时容器为Created(创建)状态,还需要启动容器

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                         PORTS                    NAMES
5cee7175339b        nginx:1.14            "nginx -g 'daemon of…"   6 seconds ago       Created                                                 nginx

[root@localhost ~# docker start  nginx

响应状态码解释

  • 201 容器已成功创建
    成功后返回的参数:

  • ID:被创建的容器ID

  • Warnings:创建容器时遇到的警告(错误)

  • 400 参数不对

  • 404 没有这个容器

  • 409 冲突

  • 500 服务器错误

1.3 检查容器

获取关于容器的底层信息。

GET /containers/(id)/json

URL:

  • /containers/(id)/json

路径参数:

  • ID:容器的ID或名称

查询参数:

  • size:将容器的大小作为字段SizeRw和SizeRootFs返回,默认为false

请求示例:

  • curl http://10.20.29.21:6666/containers/4eccce38c57f/json

响应结果:

[root@localhost ~]# curl http://10.20.29.21:5678/containers/4eccce38c57f/json
{"Id":"4eccce38c57f183feef8ec76dfa64bbc0caa21e26b3968efa98121fe037bdad1","Created":"2018-12-07T12:56:38.189610103Z","Path":"nginx","Args":["-g","daemon off;"],"State":{"Status":"exited","Running":false,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":0,"ExitCode":0,"Error":"","StartedAt":"2019-01-12T01:32:54.412546647Z","FinishedAt":"2019-01-12T01:47:57.571820667Z"},"Image":"sha256:4037a5562b030fd80ec889bb885405587a52cfef898ffb7402649005dfda75ff"
......

注:可通过python -mjson.tool可以将JSON数据格式化显示

响应状态码解释

  • 204 没有错误
  • 404 没有这个容器
  • 500 服务器错误

1.4 启动容器

POST /containers/(id)/start

URL:

  • /containers/{id}/start

路径参数:

  • ID:容器的ID或名称

查询参数:

  • detachKeys:覆盖用于分离容器的键序列。格式是一个单独的字符[a-Z]或ctrl-其中是下列之一:a-z,@,^,[,,或_

请求示例:

curl -v --raw -X POST http://10.20.29.15:5678/containers/4eccce38c57f/start

响应结果:

[root@localhost ~]# curl -v --raw -X POST http://10.20.29.15:5678/containers/4eccce38c57f/start
* About to connect() to 10.20.29.21 port 2375 (#0)
*   Trying 10.20.29.21...
* Connected to 10.20.29.21 (10.20.29.21) port 2375 (#0)
> POST /containers/4eccce38c57f/start HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 10.20.29.21:2375
> Accept: */*
> 
< HTTP/1.1 204 No Content
< Api-Version: 1.39
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/18.09.0 (linux)
< Date: Tue, 09 Apr 2019 09:14:06 GMT
< 
* Connection #0 to host 10.20.29.21 left intact

响应状态码解释

  • 204 没有错误
  • 304 容器已经启动
  • 404 没有这个容器
  • 500 服务器错误

1.5 停止容器

POST /containers/(id)/stop

URL:

  • /containers/{id}/stop

路径参数:

  • ID:容器的ID或名称

查询参数:

  • t:在关闭容器之前等待的秒数

请求示例:

  • curl -v --raw -X POST http://10.20.29.15:5678/containers/f59103b6bfd1/stop #直接关闭容器

  • curl -v --raw -X POST http://127.0.0.1:5678/containers/容器ID/stop?t=5 #等待5秒关闭容器

响应结果:

[root@localhost ~]# curl -v --raw -X POST http://10.20.29.15:5678/containers/f59103b6bfd1/stop
* About to connect() to 10.20.29.15 port 5678 (#0)
*   Trying 10.20.29.15...
* Connected to 10.20.29.15 (10.20.29.15) port 5678 (#0)
> POST /containers/f59103b6bfd1/stop HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 10.20.29.15:5678
> Accept: */*
> 
< HTTP/1.1 204 No Content
< Api-Version: 1.39
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/18.09.3 (linux)
< Date: Tue, 09 Apr 2019 09:23:15 GMT
< 
* Connection #0 to host 10.20.29.15 left intact

响应状态码解释

  • 204 没有错误
  • 304 容器已经停止
  • 404 没有这个容器
  • 500 服务器错误

1.6 重启容器

POST /containers/{id}/restart

URL:

  • /containers/{id}/restart

路径参数:

  • ID:容器的ID或名称

查询参数:

  • t:在关闭容器之前等待的秒数

请求示例:

  • curl -v --raw -X POST http://10.20.29.15:5678/containers/f59103b6bfd1/restart

  • curl -v --raw -X POST http://10.20.29.15:5678/containers/f59103b6bfd1/restart?t=5

响应结果:

[root@localhost ~]# curl -v --raw -X POST http://10.20.29.15:5678/containers/f59103b6bfd1/restart
* About to connect() to 10.20.29.15 port 5678 (#0)
*   Trying 10.20.29.15...
* Connected to 10.20.29.15 (10.20.29.15) port 5678 (#0)
> POST /containers/f59103b6bfd1/restart HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 10.20.29.15:5678
> Accept: */*
> 
< HTTP/1.1 204 No Content
< Api-Version: 1.39
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/18.09.3 (linux)
< Date: Tue, 09 Apr 2019 09:36:29 GMT
< 
* Connection #0 to host 10.20.29.15 left intact

响应状态码解释:

  • 204 没有错误
  • 404 没有这样的容器
  • 500 服务器错误

1.8 删除容器

DELETE /containers/{id}

URL:

  • /containers/{id}

路径参数:

  • ID:容器的ID或名称

查询参数:

  • v :删除与容器关联的卷,默认为false
  • force: 如果容器正在运行,请在删除之前杀死它,默认为false
  • link:删除与容器关联的指定链接,默认为false

请求示例:

curl -v -X DELETE http://127.0.0.1:5678/containers/6c66e2135f24

响应结果:

[root@localhost ~]# curl -v -X DELETE http://127.0.0.1:5678/containers/6c66e2135f24
* About to connect() to 127.0.0.1 port 5678 (#0)
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5678 (#0)
> DELETE /containers/6c66e2135f24 HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1:5678
> Accept: */*
> 
< HTTP/1.1 204 No Content
< Api-Version: 1.39
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/18.09.3 (linux)
< Date: Tue, 09 Apr 2019 07:42:04 GMT
< 
* Connection #0 to host 127.0.0.1 left intact

注:删除正在运行的容器,如果使用以上请求命令,则会报错:

< HTTP/1.1 409 Conflict
< Api-Version: 1.39
< Content-Type: application/json
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/18.09.3 (linux)
< Date: Tue, 09 Apr 2019 07:41:33 GMT
< Content-Length: 179
< 
{"message":"You cannot remove a running container f59103b6bfd1cbf753c97cfd63b83f484bdae1b3f14ff0f5b192d4e191e22f5b. Stop the container before attempting removal or force remove"}
* Connection #0 to host 127.0.0.1 left intact

若需要删除正在运行的容器,请使用如下请求命令:

  • curl -v -X DELETE http://127.0.0.1:5678/containers/a3e7b90dd836?force=true

响应结果:

[root@localhost ~]# curl -v -X DELETE http://127.0.0.1:5678/containers/a3e7b90dd836?force=true
* About to connect() to 127.0.0.1 port 5678 (#0)
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5678 (#0)
> DELETE /containers/a3e7b90dd836?force=true HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1:5678
> Accept: */*
> 
< HTTP/1.1 204 No Content
< Api-Version: 1.39
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/18.09.3 (linux)
< Date: Tue, 09 Apr 2019 05:22:23 GMT
< 
* Connection #0 to host 127.0.0.1 left intact

响应状态码解释:

  • 204 没有错误
  • 400 坏的参数
  • 404 没有这个容器
  • 409 冲突(如:容器正在运行)
  • 500 服务器错误

1.14 根据资源使用情况获取容器统计信息

此api用来显示容器使用的系统资源,下面是输出的主要内容:

  • [CONTAINER]:以短格式显示容器的 ID。
  • [CPU %]:CPU 的使用情况。
  • [MEM USAGE / LIMIT]:当前使用的内存和最大可以使用的内存。
  • [MEM %]:以百分比的形式显示内存使用情况。
  • [NET I/O]:网络 I/O 数据。
  • [BLOCK I/O]:磁盘 I/O 数据。
  • [PIDS]:PID 号。

GET /containers/{id}/stats

URL:

  • /containers/{id}/stats

路径参数:

  • ID:容器的ID或名称

查询参数:

  • stream:流输出,如果为false,则统计信息将输出一次,然后将断开连接。默认为true

默认情况下,只返回当前的状态,但可以每隔 1 秒钟刷新一次输出的内容,如果不想持续的监控容器使用资源的情况可以通过stream=false 参数只输出当前的状态。

请求示例:

  • curl http://10.20.29.21:2375/containers/545a4863e32f/stats?stream=false

响应结果:

[root@localhost ~]# curl http://10.20.29.21:2375/containers/545a4863e32f/stats?stream=false
{"read":"2019-04-09T12:49:39.82789284Z","preread":"2019-04-09T12:49:38.825314468Z","pids_stats":{"current":25},"blkio_stats":{"io_service_bytes_recursive":[],"io_serviced_recursive":[],"io_queue_recursive":[],"io_service_time_recursive":[],"io_wait_time_recursive":[],"io_merged_recursive":[],"io_time_recursive":[],"sectors_recursive":[]},"num_procs":0,"storage_stats":{},"cpu_stats":{"cpu_usage":{"total_usage":27356238626,"percpu_usage":[3568026315,3567907546,2604128115,4723295390,5379339014,5697405323,818240931,997895992],"usage_in_kernelmode":1120000000,"usage_in_usermode":1890000000},"system_cpu_usage":835459830000000,"online_cpus":8,"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"precpu_stats":{"cpu_usage":{"total_usage":27354558405,"percpu_usage":[3568026315,3567840282,2604054202,4723295390,5377875899,5697405323,818165002,997895992],"usage_in_kernelmode":1120000000,"usage_in_usermode":1890000000},"system_cpu_usage":835451830000000,"online_cpus":8,"throttling_data":{"periods":0,"throttled_periods":0,"throttled_time":0}},"memory_stats":{"usage":99807232,"max_usage":101842944,"stats":{"active_anon":99774464,"active_file":0,"cache":32768,"dirty":0,"hierarchical_memory_limit":9223372036854771712,"hierarchical_memsw_limit":9223372036854771712,"inactive_anon":0,"inactive_file":32768,"mapped_file":32768,"pgfault":19237,"pgmajfault":0,"pgpgin":11226,"pgpgout":10876,"rss":99774464,"rss_huge":90177536,"total_active_anon":99774464,"total_active_file":0,"total_cache":32768,"total_dirty":0,"total_inactive_anon":0,"total_inactive_file":32768,"total_mapped_file":32768,"total_pgfault":19237,"total_pgmajfault":0,"total_pgpgin":11226,"total_pgpgout":10876,"total_rss":99774464,"total_rss_huge":90177536,"total_unevictable":0,"total_writeback":0,"unevictable":0,"writeback":0},"limit":8182054912},"name":"/zookeeper","id":"545a4863e32f4b2280bb8e5a469159a711fdb146266ecf0bf1133920024a8b2c","networks":{"eth0":{"rx_bytes":656,"rx_packets":8,"rx_errors":0,"rx_dropped":0,"tx_bytes":0,"tx_packets":0,"tx_errors":0,"tx_dropped":0}}}

注:可通过python -mjson.tool可以将JSON数据格式化显示

响应状态码解释:

  • 200 没有错误
  • 404 没有这样的容器
  • 500 服务器错误

1.16 获取容器日志

GET /containers/{id}/logs

URL:

  • /containers/{id}/logs

路径参数:

  • id:容器的名称或者ID

查询参数:

  • follow:以数据流的形式返回日志,将返回一个带有Connection: upgrade头的101 HTTP响应,默认为false
  • stdout:标准输出日志,默认为false
  • stderr:日志输出为标准错误,默认为false
  • since:只返回这次以后的日志,作为UNIX时间戳
  • until:只返回在此之前的日志,作为UNIX时间戳
  • timestamps:将时间戳添加到每个日志行,默认为false
  • tail:只返回日志末尾的日志行数。指定为整数或all以输出所有日志行,例:tail=100

请求示例:

curl http://10.20.29.21:6666/containers/ba5cf1607701/logs?stdout=true&stdout=true&follow=true&tail=all

[root@localhost ~]# curl http://10.20.29.21:6666/containers/ba5cf1607701/logs?stdout=true&stdout=true&follow=true&tail=all
[7] 31749
[8] 31750
[9] 31751
[8]   完成                  stdout=true
[root@localhost ~]# ±0.20.29.154 - - [11/Apr/2019:16:34:03 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
䯰.20.29.154 - - [11/Apr/2019:16:34:03 +0000] "GET /favicon.ico HTTP/1.1" 404 571 "http://10.20.29.21:8080/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
10.20.29.154 - - [11/Apr/2019:16:34:15 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
10.20.29.154 - - [11/Apr/2019:16:35:05 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
10.20.29.154 - - [11/Apr/2019:16:35:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
10.20.29.154 - - [11/Apr/2019:16:35:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
......
\10.20.29.26 - - [11/Apr/2019:17:34:19 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
`10.20.29.26 - - [11/Apr/2019:17:34:20 +0000] "GET /curl HTTP/1.1" 404 169 "-" "curl/7.29.0" "-"
\10.20.29.26 - - [11/Apr/2019:17:34:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
`10.20.29.26 - - [11/Apr/2019:17:34:20 +0000] "GET /curl HTTP/1.1" 404 169 "-" "curl/7.29.0" "-"
\10.20.29.26 - - [11/Apr/2019:17:34:20 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

[7]   完成                  curl http://10.20.29.21:6666/containers/ba5cf1607701/logs?stdout=true
[9]   完成                  follow=true

响应状态码解释:

  • 101 日志作为流数据返回
  • 200 没有错误
  • 4.4 没有这个容器
  • 500 服务器错误

2.镜像相关

2.1 获取镜像清单

返回服务器上的镜像列表

GET /images/json

URL

  • /images/json

查询参数

  • all :显示所有图片。默认为false,只显示最后一层的图像(没有子层),即最终层
  • filters:过滤器的JSON编码值(map[string][]string),用于处理镜像列表
  • digests:将摘要信息显示为每个镜像上的RepoDigests字段,默认为false

请求示例

  • curl http://127.0.0.1:6666/images/json

响应结果

[root@localhost wasm]# curl http://127.0.0.1:6666/images/json | python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3550    0  3550    0     0   525k      0 --:--:-- --:--:-- --:--:--  577k
[
    {
        "Containers": -1,
        "Created": 1554895762,
        "Id": "sha256:8bc2104b95693541c31524ba25b7819af14b45360c0efa0c7d5b69344050a96b",
        "Labels": null,
        "ParentId": "sha256:996598012bcb1435d8e801cc3bc7041cb257f958095ff9c52f181c22ffbf24f6",
        "RepoDigests": null,
        "RepoTags": [
            "basis:latest"
        ],
        "SharedSize": -1,
        "Size": 12369196,
        "VirtualSize": 12369196
    },
    {
        "Containers": -1,
        "Created": 1554895758,
        "Id": "sha256:1c5c084fb13ab7cca82b6ee226a4ac19dbeb6f2983e8a81b1d5905ff83e062e4",
        "Labels": null,
        "ParentId": "sha256:49dc3f8ad471e760b96ac88fb367c6d72e503b34ae7767a7421b64fc54430e1c",
        "RepoDigests": [
            "@"
        ],
        "RepoTags": [
            ":"
        ],
        "SharedSize": -1,
        "Size": 345580587,
        "VirtualSize": 345580587
    },

响应状态码解释

  • 200 与查询匹配的镜像的摘要镜像数据
  • 500 服务器错误

2.2 构建镜像

POST /build

查询参数

  • t:设置镜像名,要以name:tag格式应用于图像的名称和可选标记。如果省略标记,latest则假定为默认值。

请求示例

  • curl -X POST “http://10.20.29.21:6666/build?t=contract:1.1&remote=https://github.com/wuxiaoyong/apiserver_demos/raw/master/contract-1.1.tar.gz”

  • t=contract:1.1 build后的镜像名与版本号

  • remote dockerfile的tar包链接地址

响应结果

[root@localhost /]# curl -X POST "http://10.20.29.21:6666/build?t=contract:1.1&remote=https://github.com/wuxiaoyong/apiserver_demos/raw/master/contract-1.1.tar.gz"
{"status":"https://github.com/wuxiaoyong/apiserver_demos/raw/master/contract-1.1.tar.gz","progressDetail":{"current":4719},"progress":" 4.719kB","id":"Downloading context"}
{"stream":"Step 1/7 : FROM golang:1.11"}
{"stream":"\n"}
{"stream":" ---\u003e 28daaf3edbdb\n"}
{"stream":"Step 2/7 : ENV GO_PATH=/go/src/"}
{"stream":"\n"}
{"stream":" ---\u003e Running in 2ce99db89d68\n"}
{"stream":"Removing intermediate container 2ce99db89d68\n"}
{"stream":" ---\u003e 5d96a6cb3d7b\n"}
{"stream":"Step 3/7 : ENV CON_PATH=/go/src/git.phjr.com/contract-server"}
{"stream":"\n"}
......
{"stream":" ---\u003e Running in 82573697eedf\n"}
{"stream":"Removing intermediate container 82573697eedf\n"}
{"stream":" ---\u003e 3c470596b89e\n"}
{"aux":{"ID":"sha256:3c470596b89e4ee7a9a63e69fb6624f44a2a84f5974aab15af1d1964bd59ade4"}}
{"stream":"Successfully built 3c470596b89e\n"}
{"stream":"Successfully tagged contract:1.1\n"}

[root@localhost /]# docker images
REPOSITORY                    TAG                            IMAGE ID            CREATED             SIZE
contract                      1.1                            3c470596b89e        12 seconds ago      837MB

响应状态码解释

  • 200 没有错误
  • 400 参数错误
  • 500 服务器错误

2.4 检查镜像

获取关于镜像的底层信息

GET /images/{name}/json

URL:

  • /images/{name}/json

路径参数:

  • name: 镜像名称或ID

请求示例:

  • curl http://127.0.0.1:6666/images/8bc2104b9569/json

响应结果

[root@localhost ~]# curl http://127.0.0.1:6666/images/8bc2104b9569/json | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2420    0  2420    0     0   557k      0 --:--:-- --:--:-- --:--:--  590k
{
    "Architecture": "amd64",
    "Author": "",
    "Comment": "",
    "Config": {
        "ArgsEscaped": true,
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "/bin/sh",
            "-c",
            "/mnt/wasm /mnt/compare.wasm 1 1"
        ],
        "Domainname": "",
        "Entrypoint": null,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "SRC_DIR=/go/src/git.phjr.com"
        ],
        "Hostname": "",
        "Image": "sha256:996598012bcb1435d8e801cc3bc7041cb257f958095ff9c52f181c22ffbf24f6",
        "Labels": null,
        "OnBuild": null,
        "OpenStdin": false,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": null,
        "WorkingDir": ""
    },
    "Container": "1dfcec665a0479c6519ffdea5516bc619b70a154114700d46ba583f44074ee12",
    "ContainerConfig": {
        "ArgsEscaped": true,
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "/bin/sh",
            "-c",
            "#(nop) ",
            "CMD [\"/bin/sh\" \"-c\" \"/mnt/wasm /mnt/compare.wasm 1 1\"]"
        ],
        "Domainname": "",
        "Entrypoint": null,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "SRC_DIR=/go/src/git.phjr.com"
        ],
        "Hostname": "1dfcec665a04",
        "Image": "sha256:996598012bcb1435d8e801cc3bc7041cb257f958095ff9c52f181c22ffbf24f6",
        "Labels": {},
        "OnBuild": null,
        "OpenStdin": false,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": null,
        "WorkingDir": ""

响应结果解释

  • 200 没有错误
  • 404 没有这个镜像
  • 500 服务器错误

2.5 获取镜像的历史记录

每一个镜像都可能依赖于由一个或多个下层的组成的另一个镜像,这条请求就是查看最终镜像的镜像层记录

GET /images/{name}/history

URL:

  • /images/{name}/history

路径参数:

  • name: 镜像的名称或者ID

请求示例

  • curl http://10.20.29.26:6666/images/8bc2104b9569/history

响应结果

[root@localhost ~]# curl http://10.20.29.26:6666/images/8bc2104b9569/history |python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1356  100  1356    0     0   287k      0 --:--:-- --:--:-- --:--:--  331k
[
    {
        "Comment": "",
        "Created": 1554895762,
        "CreatedBy": "/bin/sh -c #(nop)  CMD [\"/bin/sh\" \"-c\" \"/mnt/wasm /mnt/compare.wasm 1 1\"]",
        "Id": "sha256:8bc2104b95693541c31524ba25b7819af14b45360c0efa0c7d5b69344050a96b",
        "Size": 0,
        "Tags": [
            "basis:latest"
        ]
    },
    {
        "Comment": "",
        "Created": 1554895762,
        "CreatedBy": "/bin/sh -c #(nop) COPY file:c4cd840b1e62e31e6717e6ae158d6533291637acab1efaa4b77f6d7185181cd5 in /mnt/ ",
        "Id": "sha256:996598012bcb1435d8e801cc3bc7041cb257f958095ff9c52f181c22ffbf24f6",
        "Size": 2444971,
        "Tags": null
    },
    {
        "Comment": "",
        "Created": 1554895761,
        "CreatedBy": "/bin/sh -c #(nop) COPY file:8cc7ec5e2f20daf71d43c8467d3ab0419c50e6d694510d02329be966e7c6f75c in /mnt/ ",
        "Id": "sha256:f766d15551a2f5e9f9450f2f7cafdc959bf80de69a6d612946b9569b42ef7961",
        "Size": 4391090,
        "Tags": null
    },
    {
        "Comment": "",
        "Created": 1554895759,
        "CreatedBy": "/bin/sh -c #(nop)  ENV SRC_DIR=/go/src/git.phjr.com",
        "Id": "sha256:20e3faf18f3500632d1496f3d1034d84cd856669f5470a49b7cf36e80938fa6f",
        "Size": 0,
        "Tags": null
    },
    {
        "Comment": "",
        "Created": 1554852018,
        "CreatedBy": "/bin/sh -c #(nop)  CMD [\"/bin/sh\"]",
        "Id": "sha256:cdf98d1859c1beb33ec70507249d34bacf888d59c24df3204057f9a6c758dddb",
        "Size": 0,
        "Tags": [
            "alpine:latest"
        ]
    },

响应状态码解释:

  • 200 镜像层列表
  • 404 没有这个镜像
  • 500 服务器错误

2.6 标记一个镜像

为一个镜像打一个标签

POST /images/{name}/tag

URL:

  • /images/{name}/tag

路径参数

  • name:镜像名称或者ID

查询参数:

  • repo:要标记的存储库的名称
  • tag:新标记的名称

请求示例:

  • curl -v -X POST http://10.20.29.26:6666/images/8bc2104b9569/tag?repo=镜像新标签名:版本号

响应结果:

[root@localhost ~]# curl -v -X POST http://10.20.29.26:6666/images/8bc2104b9569/tag?repo=10.20.29.45/myproject/basechain:1.1
* About to connect() to 10.20.29.26 port 6666 (#0)
*   Trying 10.20.29.26...
* Connected to 10.20.29.26 (10.20.29.26) port 6666 (#0)
> POST /images/8bc2104b9569/tag?repo=10.20.29.45/myproject/basechain:1.1 HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 10.20.29.26:6666
> Accept: */*
> 
< HTTP/1.1 201 Created
< Api-Version: 1.39
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/18.09.4 (linux)
< Date: Thu, 11 Apr 2019 03:39:09 GMT
< Content-Length: 0
< 
* Connection #0 to host 10.20.29.26 left intact


[root@localhost ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
10.20.29.45/myproject/basechain   1.1                 8bc2104b9569        16 hours ago        12.4MB
basis                             latest              8bc2104b9569        16 hours ago        12.4MB

响应状态码解释

  • 201 没有错误
  • 400 参数错误
  • 409 冲突(例:新标签名称冲突)
  • 500 服务器错误

2.8 删除镜像

删除一个镜像,以及该镜像引用的任何未标记的父镜像

如果镜像具有子镜像、正在运行的容器使用该镜像或正在生成镜像,则无法删除该镜像

DELETE /images/{name}

URL:

  • /images/{name}

路径参数

  • name: 镜像名称或者ID

查询参数

  • force:即使被停止的容器或其他标记使用,也要删除镜像,默认为false

  • noprune:不能删除未加标签的父镜像,默认为false

请求示例

  • curl -v -X DELETE http://127.0.0.1:5678/images/镜像ID或镜像名

响应结果

[root@localhost ~]# curl -v -X DELETE http://127.0.0.1:6666/images/nginx:1.15
* About to connect() to 127.0.0.1 port 6666 (#0)
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 6666 (#0)
> DELETE /images/nginx:1.15 HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1:6666
> Accept: */*
> 
< HTTP/1.1 200 OK
< Api-Version: 1.39
< Content-Type: application/json
< Docker-Experimental: false
< Ostype: linux
< Server: Docker/18.09.0 (linux)
< Date: Wed, 10 Apr 2019 22:59:19 GMT
< Content-Length: 379
< 
[{"Untagged":"nginx:1.15"},{"Untagged":"nginx@sha256:d14bbdf20f42375020c80315041ba252623b489966c5b5b0ce9d244fa4bf0400"},{"Deleted":"sha256:bb776ce48575796501bcc53e511563116132b789ab0552d520513da8c738cba2"},{"Deleted":"sha256:43e4bf6ebb72bc17f5f35af0ace5e5d5db31b2b631fac2cca3d4be2420ca9758"},{"Deleted":"sha256:2ceeec5cb749c4154ae2390bc3e0c2a4dee8663ca7012ca37a293734de83d498"}]
* Connection #0 to host 127.0.0.1 left intact

响应状态码解释

  • 200 镜像已成功删除
  • 404 没有这个镜像
  • 409 冲突(例如:镜像正在被某一个容器使用,没有使用强制删除)
  • 500 服务器错误

2.9 删除全部镜像

使用此方法可删除所有已停止的容器,请慎用!!!

你可能感兴趣的:(Linux,docker,docker,Docker,Remote,Remote,API)