compose icon indicating copy to clipboard operation
compose copied to clipboard

CPU limit (`cpus`) field is not respected in Mac M1

Open sumerc opened this issue 4 years ago • 13 comments

I have following for my clickhouse deployment:

version: "2"
services:

...

clickhouse:
    container_name: 'clickhouse'
    image: clickhouse/clickhouse-server
    cpus: 1
    mem_limit: 3G
    platform: linux/amd64
    ...

I can set my memory successfully but cannot set cpu limit with using cpus setting as above. I run the above via docker compose up clickhouse and then run docker inspect <container_id> | grep NanoCpus and always get 0 as result.

When I ran the same application via docker run as following:

docker run --cpus=0.25 --platform linux/amd64 -ti .... clickhouse/clickhouse-server

I see correct NanoCpus value:

> docker inspect 3d1 | grep NanoCpus
"NanoCpus": 250000000

This is why I thought it might be a docker compose issue.

I have tried installing the latest version of docker-compose for Mac M1:

> docker-compose version
Docker Compose version v2.5.1

sumerc avatar May 20 '22 13:05 sumerc

@sumerc

Please find related documentation of resource limitation here

https://docs.docker.com/compose/compose-file/compose-file-v3/#resources

Also, please take a look at the following issue about current state of setting resources in non-swarm mode

https://github.com/docker/compose/issues/4513

maxcleme avatar May 23 '22 07:05 maxcleme

Hey @maxcleme ,

Please correct me if I am wrong but IIUC, to set CPU limits on docker compose:

  • we need to use compose v2 in non-swarm mode, (it is not supported anymore)
  • we can use v3 and swarm mode.

But in my example I already used v2. Am I missing something? I was using the reference here: https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources

sumerc avatar May 23 '22 07:05 sumerc

@sumerc,

Sorry for misunderstanding, you should be able to limit cpu using cpu_count instead of cpus, cpus has been deprecated in compose spec.

Moreover, you should be able to do this in v3 as well

maxcleme avatar May 23 '22 07:05 maxcleme

In fact Compose V2 is using the Compose Specification so you don't need the version property anymore 😉

glours avatar May 23 '22 07:05 glours

Thanks for the pointers. Actually, setting cpu_count seems to change the CpuCount field in the docker inspect output.(I saw NanoCpus being changed when I ran same command with docker run which runs on a single core without any issues) However, I still see more than 1 cpu utilization in docker stats output. It reaches to ~%250 when running the load tests.

I also read cpu_count is a Windows only option here -> https://stackoverflow.com/questions/56682105/whats-the-difference-between-cpus-and-cpu-count-in-docker . Is this information incorrect?

sumerc avatar May 23 '22 07:05 sumerc

A small reproducer that might help us understand if there is really an issue or not:

I am using following application:

package main

import (
	"crypto/md5"
	"encoding/hex"
	"io"
	"time"
)

func myhash() {
	for {
		h := md5.New()
		io.WriteString(h, "abababbabababbabaaosdfhjkloasdfjopsdjfopisdjfsjdfjsdofj")
		hex.EncodeToString(h.Sum(nil))
	}
}

func main() {
	go myhash()
	go myhash()
	time.Sleep(100 * time.Second)
}

The relevant compose file:

services:
  app:
    container_name: "mycomposecpulimit"
    cpu_count: 1
    build:
      dockerfile: Dockerfile
      context: .
    volumes:
      - .:/app

And Dockerfile

FROM golang:1.18-alpine
RUN apk add git
RUN apk add gcc musl-dev
WORKDIR /app
CMD ["go", "run", "main.go"]

and when I run the app I see following in docker stats: (you can see it is well over %100 so cpu_count seems not to be respected?)

f41a41661961   mycomposecpulimit   214.66%   18.61MiB / 5.8GiB   0.31%     876B / 0B         45.1kB / 979kB    19

and here is the inspect output:

> d inspect f41 | grep "Cpu"
            "CpuShares": 0,
            "NanoCpus": 0,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "CpuCount": 1,
            "CpuPercent": 0,

I said this before but when I ran this app with docker run --cpus=1 CPU does not get beyond %100 just as expected.

sumerc avatar May 24 '22 14:05 sumerc

Hi @sumerc Can you give a try to Compose v2.6.1 and use the deploy.limits.cpu property which is the one intended to limit the usage of the cpu in the container? If everything works as expected, can you close the issue?

glours avatar Jun 24 '22 08:06 glours

@sumerc I'm seeing the same thing, the "cpu_count" is being passed as I see from Docker inspect: ...."CpuCount": 4,...

But the actual container will exceed these values.

I saw a lot about how this is no longer supported on Linux (and only applies to Windows), the weird thing is that if you run Docker Compose 1.x (before it was packaged up as an RPM with Docker 20.x), this is passed and honored in Compose format 2.2 through 2.4. So I don't think this is a Docker CE daemon/runtime limitation but Compose is not longer passing it in the format Docker's API picks up and honors judging by the runtime behavior.

I'm still trying to find a reliable way to set cpu limits in Docker Compose 2.x format (not all of us run Swarm and 3.x is not backwards compatible, tons of other links about this issue), using Docker Compose 2.6.x, and on Linux.

solzun avatar Jul 13 '22 18:07 solzun

@glours ,

Just to be clear (and I can test with your fix in Compose 2.6.1), for Docker CE/Docker Compose on Linux, using Compose version 2.4 syntax reference, what do you explicitly set in the Compose file for the service to test your CPU limit fix? I'm unfamiliar with deploy.limits.cpu property, is this different than the 3.x syntax usually recommended (but as per quite a few GitHub issues, does not work in non-Docker Swarm deployments?)

Usually as:

deploy:
  resources:
    limits:
      cpus: '0.50'

@sumerc If you drop to Docker Compose 1.x versions (I'm testing with 1.26.2, on Linux with Docker CE 20.10.17 and using Compose syntax reference version 2.4, the cpus: 4 does work, and translates correctly when you run Docker inspect: "NanoCpus": 4000000000,

When I flip to Docker Compose version 2.6.0 on the same exact host, that doesn't get translated to NanoCpus and is effectively ignored, which I believe may be @glours PR fix (but I'm not familiar with this deploy.limits.cpu.

solzun avatar Jul 14 '22 13:07 solzun

Compose V2 doesn't referer to any specific version now, the Compose specification merged all the file versions.

We recommend to use deploy deployment attributes when you want to change the runtime behavior of your services. That's why we propose to use deployment.limits.cpu and yes my fix does the conversion to nanocpus for you

glours avatar Jul 14 '22 13:07 glours

@glours / @sumerc

Confirmed Docker Compose 2.6.1 works on Linux to restrict the CPU setting while still using Docker Compose 2.x version format using:

deploy:
  resources:
    limits:
      cpus: '4'
      memory: 16384M

In Docker Compose 2.6.0 the above deployment attributes yield: "NanoCpus": 4,

In Docker Compose 2.6.1 the above deployment attributes yield: "NanoCpus": 4000000000,

This restrict memory/CPU fine as far as my testing, thanks for the fix @glours !

solzun avatar Jul 14 '22 16:07 solzun

@sumerc can we close this issue as this PR should fixed it?

glours avatar Aug 01 '22 09:08 glours

Hi @glours. Sure. I have not had the chance to test it, but it seems good. Thank you:)

sumerc avatar Aug 01 '22 09:08 sumerc