SOLVED: Discrepancies in docker image sizes using the github.com/docker/docker/client package

Hi, I’m building my own docker client in GO, but for some reason I get discrepancies when displaying/calculating an image size.

Using the standard docker client, I get:

[12:59:18|jfgratton@zenika:~]: docker images |egrep "certbui|postgres"
nexus:9820/postgresql14   latest-arm64     53e6d093a323   3 months ago   87.2MB
nexus:9820/certbuilder    2.02.00-arm64    6fb52fc0da42   3 months ago   226MB

While my tool gets this:

[12:49:25|jfgratton@zenika:~]: dtools lsi |egrep "certbui|postgres"
┃ nexus:9820 ┃ certbuilder  ┃ 2.02.00-arm64  ┃ 6fb52fc0da42 ┃ 2022.09.03 15:50:41 ┃ 215MB       ┃
┃ nexus:9820 ┃ postgresql14 ┃ latest-arm64   ┃ 53e6d093a323 ┃ 2022.09.08 18:07:51 ┃ 83MB        ┃

The code I use from the docker/docker/client pkg is quite straightforward :

images, err := cli.ImageList(ctx, types.ImageListOptions{All: true})
	if err != nil {
		errmsg := fmt.Sprintf("%v", err)
		if errmsg == "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?" {
			fmt.Println(errmsg)
			os.Exit(-1)
		} else {
			panic(err)
		}
	}

	for _, image := range images {
		//imgSpec := getImageTag(image.ID, image.RepoTags, image.Created, image.Size)
		imgspecSlice = append(imgspecSlice, getImageTag(image.ID, image.RepoTags, image.Created, image.Size)...)
	}

As per types package - github.com/docker/docker/api/types - Go Packages I’d expect the image size to be in the Size (int64) struct (side note: what use is VirtualSize for ?)

I understand that it’s not a Go question per-se, but more about Docker’s API…

–JFG

Fixed it… By the way, I’m a poor questioned, a code snippet was missing, where I compute the reported size to format it in MB.

Basically I was doing:

imgspec.size = (float32)(size / 1024.0 / 1024.0)
		imgspecSlice = append(imgspecSlice, imgspec)

Instead of :

imgspec.size = (float32)(size / 1000.0 / 1000.0)
		imgspecSlice = append(imgspecSlice, imgspec)

I was dividing the value by the “real” unit size (1024) instead of the human-readable size (1000).

SOLVED.