fussybeaver / bollard

Docker daemon API in Rust
Apache License 2.0
862 stars 131 forks source link

Docker CLI vs API? #118

Open smklein opened 3 years ago

smklein commented 3 years ago

Hey all, not sure if this is the right place to ask, but the maintainers of this crate seem familiar with the Docker engine API, and I figured I'd ask :)

(If there is somewhere else I should go to find the answer to this question, I'm happy to RTFM, but figured ya'll would be impacted by similar questions).

I'm finding myself frequently supplying arguments to the Docker CLI, i.e., docker run --gpus all, and wondering "what arguments to bollard would trigger the equivalent operation?" I'm aware that this is essentially a "CLI vs docker engine API" question, but other than reverse-engineering the docker CLI source, is there a quick way ya'll have seen the translation from one to the other?

I'd be happy to add some documentation about this for debugging purposes, once I have a path forward.

As an example, uncovering that

docker run --rm

Is equivalent to:

create_container with HostConfig.auto_remove = true + start_container.

Was actually non-trivial, but having a quick way to see how one maps to the other is really useful for navigating the bollard API.

smklein commented 3 years ago

Welp, I found the answer myself:

strace -fe trace=open,read,write -v -s 2048 -- docker run --rm hello-world

Has a corresponding line:

[pid 1316489] write(3, "POST /v1.40/containers/create HTTP/1.1\r\nHost: docker\r\nUser-Agent: Docker-Client/19.03.12 (linux)\r\nContent-Length: 1517\r\nContent-Type: application/json\r\n\r\n{\"Hostname\":\"\",\"
Domainname\":\"\",\"User\":\"\",\"AttachStdin\":false,\"AttachStdout\":true,\"AttachStderr\":true,\"Tty\":false,\"OpenStdin\":false,\"StdinOnce\":false,\"Env\":[],\"Cmd\":null,\"Image\":\"hello-world\",\"Volumes
\":{},\"WorkingDir\":\"\",\"Entrypoint\":null,\"OnBuild\":null,\"Labels\":{},\"HostConfig\":{\"Binds\":null,\"ContainerIDFile\":\"\",\"LogConfig\":{\"Type\":\"\",\"Config\":{}},\"NetworkMode\":\"default\",\"Port
Bindings\":{},\"RestartPolicy\":{\"Name\":\"no\",\"MaximumRetryCount\":0},\"AutoRemove\":true,\"VolumeDriver\":\"\",\"VolumesFrom\":null,\"CapAdd\":null,\"CapDrop\":null,\"Capabilities\":null,\"Dns\":[],\"DnsOpt
ions\":[],\"DnsSearch\":[],\"ExtraHosts\":null,\"GroupAdd\":null,\"IpcMode\":\"\",\"Cgroup\":\"\",\"Links\":null,\"OomScoreAdj\":0,\"PidMode\":\"\",\"Privileged\":false,\"PublishAllPorts\":false,\"ReadonlyRootfs
\":false,\"SecurityOpt\":null,\"UTSMode\":\"\",\"UsernsMode\":\"\",\"ShmSize\":0,\"ConsoleSize\":[0,0],\"Isolation\":\"\",\"CpuShares\":0,\"Memory\":0,\"NanoCpus\":0,\"CgroupParent\":\"\",\"BlkioWeight\":0,\"Blk
ioWeightDevice\":[],\"BlkioDeviceReadBps\":null,\"BlkioDeviceWriteBps\":null,\"BlkioDeviceReadIOps\":null,\"BlkioDeviceWriteIOps\":null,\"CpuPeriod\":0,\"CpuQuota\":0,\"CpuRealtimePeriod\":0,\"CpuRealtimeRuntime
\":0,\"CpusetCpus\":\"\",\"CpusetMems\":\"\",\"Devices\":[],\"DeviceCgroupRules\":null,\"DeviceRequests\":null,\"KernelMemory\":0,\"KernelMemoryTCP\":0,\"MemoryReservation\":0,\"MemorySwap\":0,\"MemorySwappiness
\":-1,\"OomKillDisable\":false,\"PidsLimit\":0,\"Ulimits\":null,\"CpuCount\":0,\"CpuPercent\":0,\"IOMaximumIOps\":0,\"IOMaximumBandwidth\":0,\"MaskedPaths\":null,\"ReadonlyPaths\":null},\"NetworkingConfig\":{\"E
ndpointsConfig\":{}}}\n", 1671) = 1671

I'll file a PR with some doc updates; I presume this would be useful for other folks too.