moby: Memory Leak in Docker 27.0.3

Description

After I updated to Docker 27.0.3 (I do not remember what was the older version, sorry, I upgraded my dedicated servers when the RegreSSHion exploit happened, but technically the older version is a version between 10/01/2024 (when the system was installed) and 01/07/2024 (when I upgraded all packages)) the dockerd daemon is leaking memory like crazy, in two of my dedicated servers dockerd ends up eating so much memory that containers end up getting OOM’d by the system

image

If I restart Docker, the dockerd memory usage drops to ~200MB, and then keeps increasing and increasing and increasing…

Reproduce

I don’t know how to reproduce this yet

Expected behavior

That dockerd does not eating up so much memory.

docker version

Docker version 27.0.3, build 7d4bcd8

docker info

Client: Docker Engine - Community
 Version:    27.0.3
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.15.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.28.1
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 68
  Running: 68
  Paused: 0
  Stopped: 0
 Images: 41
 Server Version: 27.0.3
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: local
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: ae71819c4f5e67bb4d5ae76a6b735f29cc25774e
 runc version: v1.1.13-0-g58aa920
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.5.0-41-generic
 Operating System: Ubuntu 22.04.4 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 16
 Total Memory: 125.7GiB
 Name: saiyan-seeker
 ID: 3ae68731-57aa-49b8-bc88-c007cbcc3059
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Default Address Pools:
   Base: 172.16.0.0/12, Size: 20
   Base: 192.168.0.0/16, Size: 24

Additional Info

Yeah, this issue doesn’t really help a lot to figure out what is actually the issue… When I was looking at other comments, I found out that there is a way to create a heap dump of the dockerd process, but the link on how to do it is unavailable and it a mirror is not available on the Wayback Machine, so I wonder if someone has a working link about how to do it? https://github.com/moby/moby/issues/39916#issuecomment-532156804

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 35 (16 by maintainers)

Most upvoted comments

I don’t know why the buildkit tracing (used for the buildkit history API) is so bloated but I have an idea as to why the main otel exporter may be. I see a lot of long running requests (container attach, events), I suspect these are long-running spans that never end until the request is terminated. OTel is holding onto that data until the spans end. I’ll run some tests locally to see if that’s was causing the excessive memory usage.

we’re using docker-compose healthchecks and was also able to observe the memory leak, disabling healthchecks helps lowering the memory leak, although there is still some amount of memory leak at the end, but it is no longer causing OOMs.

image

image

Running Docker version 27.1.2, build d01f264

I did the same tests on two different VMs, one running 26.0.0 and another running 27.0.3. Both VMs are based on the VM I shared above, the 26.0.0 VM is the same as the 27.0.3 but I downgraded the Docker version to 26.0.0. I downgraded using my own snippet here, but I changed the versions from 22.04~jammy to 24.04~noble https://github.com/moby/moby/issues/48144#issuecomment-2227448290

The tests have been running for around ~30 minutes.

Docker 26.0.0:

Stays around ~350MB.

image

Docker 27.0.3:

Memory leaks!

image

I looked at the minimal repro from https://github.com/open-telemetry/opentelemetry-go-contrib/issues/5190 and looks like it also reproduces under the OTEL version we are vendoring.

Opened a PR to add the suggested workaround: https://github.com/moby/moby/pull/48690

And now a cool and useful finding that will help pin point down where the issue is…!

Docker 27.0.1, using older compose/buildx/containerd.io versions [NOT AFFECTED BY THE LEAK]

I used the same versions that I used on the Docker 26.1.0 test for the docker-buildx-plugin, docker-compose-plugin and containerd.io packages… and that stopped the memory leak!

So maybe the issue isn’t in moby (Docker Engine) itself, but in one of the other three packages. I don’t think it is an issue in containerd.io because I had tried 27.0.3 with containerd.io 1.6.33-1 when @thaJeztah asked about it, and it also had the memory leak. https://github.com/moby/moby/issues/48144#issuecomment-2230941155

If it is really an issue in the other packages, that would explain why when I downgraded Docker to 26.0.2 previously I still had the memory leak, because I didn’t downgrade the compose/buildx/containerd.io packages then, whereas when I downgraded to 26.0.0 I did attempt to match all packages.

I would guess that Docker 27.0.3 will also not be affected by the memory leak, as long as you use the older buildx/compose/containerd.io versions, maybe I should try that out later…

image

sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
sudo rm -rf /var/lib/containerd

VERSION_STRING=5:27.0.1-1~ubuntu.24.04~noble
BUILDX_VERSION_STRING=0.13.1-1~ubuntu.24.04~noble
COMPOSE_VERSION_STRING=2.26.1-1~ubuntu.24.04~noble
CONTAINERD_VERSION_STRING=1.6.33-1

sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io=$CONTAINERD_VERSION_STRING docker-buildx-plugin=$BUILDX_VERSION_STRING docker-compose-plugin=$COMPOSE_VERSION_STRING
Client: Docker Engine - Community
 Version:    27.0.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.13.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.26.1
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 2
  Running: 0
  Paused: 0
  Stopped: 2
 Images: 2
 Server Version: 27.0.1
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: local
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: d2d58213f83a351ca8f528a95fbd145f5654e957
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.8.0-38-generic
 Operating System: Ubuntu 24.04 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 3.824GiB
 Name: dockertest
 ID: 5ed5eb75-ec5a-4190-94e4-eebfb101e9c4
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false
 Default Address Pools:
   Base: 172.16.0.0/12, Size: 20
   Base: 192.168.0.0/16, Size: 24

Some findings: One of my dedicated servers was installed on 03/04/2024, so this narrows down the versions a bit. I tried downgrading from Docker 27.0.3 to Docker 26.0.2, but it seems that the issue also affects 26.0.2 too… HOWEVER I had only downgraded the docker-ce package and the docker-ce-cli package, not the containerd.io + docker-buildx-plugin + docker-compose-plugin. Maybe these three packages can also cause the memory leak? The daemon got up to 1GB of usage before I killed it.

Then I attempted to downgrade Docker to 26.0.0, which was the version that was the most recent version at the time I installed Ubuntu Server 22.04 on that dedicated server, but this time I tried matching all package versions to avoid any possible issues.

sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
sudo rm -rf /var/lib/containerd

VERSION_STRING=5:26.0.0-1~ubuntu.22.04~jammy
BUILDX_VERSION_STRING=0.13.1-1~ubuntu.22.04~jammy
COMPOSE_VERSION_STRING=2.25.0-1~ubuntu.22.04~jammy
CONTAINERD_VERSION_STRING=1.6.33-1

sudo apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io=$CONTAINERD_VERSION_STRING docker-buildx-plugin=$BUILDX_VERSION_STRING docker-compose-plugin=$COMPOSE_VERSION_STRING

And now it worked! I kept Docker running for ~2 hours and dockerd’s memory usage has stayed around ~300MB.

Yes, this is a temporary solution because no one wants to run outdated software, but this may be useful for anyone that is having issues with the memory leaks and needs a solution right now.

If any other outstanding issues happen, then I will talk about it here, but for now at least there is a “known good” Docker version that is not affected by the memory leak: Docker 26.0.0

And here’s how to hold the version after it is installed: apt-mark hold docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Figured out how to heap dump Docker!

curl -v --silent -XGET --unix-socket /run/docker.sock http://localhost/debug/pprof/heap > /srv/docker-heap-dump

Sources:

In Use Objects: docker_inuse_objects

In Use Space: docker_in_use_space

Alloc Space docker_alloc_space

Alloc Objects: docker_alloc_objects

By the way, can I share the heap dump file, or does it contain sensitive secrets?