MongoDB does not respect docker compose limits

I use MongoDB v5.0.3 via docker on a single AWS instance. The container kept crashing on long queries exactly after hitting 100% memory.
Then I noticed this:
This is the docker stat. You can notice that the memory is limited to 4 GB.

This is the MongoDB host info:

  system: {
    currentTime: 2022-11-28T10:23:25.772Z,
    hostname: 'mongo',
    cpuAddrSize: 64,
    memSizeMB: Long("31304"),
    memLimitMB: Long("31304"),
    numCores: 8,
    cpuArch: 'x86_64',
    numaEnabled: false

You can see that the memLimitMB is set to 31 GB.

I rebuilt the container several times, but it still has the same issue.

What could be the reason?

1 Like

See my answer in

1 Like