php editor Yuzai is here to introduce to you two commands in docker: `docker system df` and `/system/df` (docker api endpoint). Both commands are used to view the docker system resource usage, but their usage and result display methods are slightly different. `docker system df` is a docker command that can be run directly in the terminal. It will display the usage of various resources in the docker system (including images, containers, data volumes, etc.), as well as the overall resource usage. And `/system/df` is a docker API endpoint, and you need to obtain relevant information by calling the API. Its return result is similar to `docker system df`, but it is more suitable for programmatically obtaining docker system resource usage.
Question content
I am writing a program in Go to get the total disk usage in GB from my docker host. For this I use func DiskUsage()
from go lib:
- https://pkg.go.dev/github.com/docker/docker/client#Client.DiskUsage.
View the code, the function is calling the docker api endpoint /system/df
:
- https://docs.docker.com/engine/api/v1.43/#tag/System/operation/SystemDataUsage
However, when I use this library with the calculation of GB using the command docker system df
, I notice a strange behavior:
-
docker system df
Output:$ docker system df TYPE TOTAL ACTIVE SIZE RECLAIMABLE Images 223 4 21.02GB 20.7GB (98%) Containers 6 0 0B 0B Local Volumes 13 1 536.4MB 340.4MB (63%) Build Cache 954 0 13.51GB 13.51GB
- My Go application output:
$ go run ./cmd/main.go Images: TOTAL (223), 17GB Build Cache: TOTAL (954), 29GB
As you can see, there is a difference between the two outputs. I need help understanding if there is something wrong with my calculations that get the data from the /system/df
endpoint. Thanks:)
Go Application:
package main import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/client" ) func main() { cli, err := client.NewClientWithOpts(client.FromEnv) if err != nil { panic(err) } diskUsg, err := cli.DiskUsage(context.Background(), types.DiskUsageOptions{}) if err != nil { panic(err) } b := float64(0) for _, ch := range diskUsg.BuildCache { b = b + float64(ch.Size) } b2 := float64(0) for _, ch := range diskUsg.Images { if ch.Size > ch.SharedSize { b2 = b2 + (float64(ch.Size) - float64(ch.SharedSize)) continue } b2 = b2 + (float64(ch.SharedSize) - float64(ch.Size)) } fmt.Printf("Images: TOTAL (%d), %2.fGB\n", len(diskUsg.Images), float64(b2)/(1<<30)) fmt.Printf("Build Cache: TOTAL (%d), %2.fGB\n", len(diskUsg.BuildCache), float64(b)/(1<<30)) }
Solution
Based on Docker source code:
-
system df
Command: https://github.com/docker/cli/blob/v24.0.5/cli/command/system/df.go - Output format: https://github.com/docker/cli/blob/v24.0.5/cli/command/formatter/disk_usage.go
You should be able to reproduce exactly what docker system df
does using the following code:
go.mod
module 76982562-docker-system-df-vs-system-df-docker-api-endpoint go 1.21.0 require ( github.com/docker/cli v24.0.5+incompatible github.com/docker/docker v24.0.5+incompatible )
main.go
<code>package main import ( "context" "fmt" "os" "github.com/docker/cli/cli/command/formatter" "github.com/docker/docker/api/types" "github.com/docker/docker/client" "github.com/docker/go-units" ) func main() { cli, err := client.NewClientWithOpts(client.FromEnv) if err != nil { panic(err) } diskUsage, err := cli.DiskUsage(context.Background(), types.DiskUsageOptions{}) if err != nil { panic(err) } var bsz int64 for _, bc := range diskUsage.BuildCache { if !bc.Shared { bsz += bc.Size } } fmt.Printf("Images: TOTAL (%d), %s\n", len(diskUsage.Images), units.HumanSize(float64(diskUsage.LayersSize))) fmt.Printf("Build Cache: TOTAL (%d), %s\n", len(diskUsage.BuildCache), units.HumanSize(float64(bsz))) } </code>
- For images, the
docker
library directly providesdiskUsage.LayersSize
to represent the total size, so you don’t have to calculate it yourself - For the build cache you need to exclude shared projects (
if !bc.Shared
)
To convert the size in the correct units, I strongly recommend using github.com/docker/go-units
(e.g. units.HumanSize(float64(diskUsage.LayersSize))
) . This will save you the unit conversion nightmare!
The above is the detailed content of `docker system df` and `/system/df` (docker api endpoint). For more information, please follow other related articles on the PHP Chinese website!

Goisidealforbuildingscalablesystemsduetoitssimplicity,efficiency,andbuilt-inconcurrencysupport.1)Go'scleansyntaxandminimalisticdesignenhanceproductivityandreduceerrors.2)Itsgoroutinesandchannelsenableefficientconcurrentprogramming,distributingworkloa

InitfunctionsinGorunautomaticallybeforemain()andareusefulforsettingupenvironmentsandinitializingvariables.Usethemforsimpletasks,avoidsideeffects,andbecautiouswithtestingandloggingtomaintaincodeclarityandtestability.

Goinitializespackagesintheordertheyareimported,thenexecutesinitfunctionswithinapackageintheirdefinitionorder,andfilenamesdeterminetheorderacrossmultiplefiles.Thisprocesscanbeinfluencedbydependenciesbetweenpackages,whichmayleadtocomplexinitializations

CustominterfacesinGoarecrucialforwritingflexible,maintainable,andtestablecode.Theyenabledeveloperstofocusonbehavioroverimplementation,enhancingmodularityandrobustness.Bydefiningmethodsignaturesthattypesmustimplement,interfacesallowforcodereusabilitya

The reason for using interfaces for simulation and testing is that the interface allows the definition of contracts without specifying implementations, making the tests more isolated and easy to maintain. 1) Implicit implementation of the interface makes it simple to create mock objects, which can replace real implementations in testing. 2) Using interfaces can easily replace the real implementation of the service in unit tests, reducing test complexity and time. 3) The flexibility provided by the interface allows for changes in simulated behavior for different test cases. 4) Interfaces help design testable code from the beginning, improving the modularity and maintainability of the code.

In Go, the init function is used for package initialization. 1) The init function is automatically called when package initialization, and is suitable for initializing global variables, setting connections and loading configuration files. 2) There can be multiple init functions that can be executed in file order. 3) When using it, the execution order, test difficulty and performance impact should be considered. 4) It is recommended to reduce side effects, use dependency injection and delay initialization to optimize the use of init functions.

Go'sselectstatementstreamlinesconcurrentprogrammingbymultiplexingoperations.1)Itallowswaitingonmultiplechanneloperations,executingthefirstreadyone.2)Thedefaultcasepreventsdeadlocksbyallowingtheprogramtoproceedifnooperationisready.3)Itcanbeusedforsend

ContextandWaitGroupsarecrucialinGoformanaginggoroutineseffectively.1)ContextallowssignalingcancellationanddeadlinesacrossAPIboundaries,ensuringgoroutinescanbestoppedgracefully.2)WaitGroupssynchronizegoroutines,ensuringallcompletebeforeproceeding,prev


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)
