-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
Description
- Version: 10.16.0
- Host machine: MacOS 10.14.5
- Docker version: 18.09.2
- Docker image: node:10.16.0
- Subsystem: os
Bug
I found that inside a docker container with cpus limited, os.cpus().length still outputs the host machine's cpu amount instead of limited cpu amount.
How to reproduce
$ sysctl -n hw.physicalcpu
2
$ docker container run --rm -it --memory="3072m" --cpus="1" node:10.16.0 bash
root@38997a92e59f:/# node
> require('os').cpus().length
2
> .exit
root@38997a92e59f:/# exit
exitAs you see my host machine has 2 cpus. I start a container based on image node:10.16.0, and I set cpus of this container limit to 1. But inside the container, I can still get 2 cpus by using os.cpus().length, which supposed to be 1 as expected.
I've googled a lot and found that there's no hard distinction between inside and outside the docker container, so the os module just reads cpus' information from host machine. But java has solved this problem, as you can see here.
What the bug will cause
If we run multiple docker containers on our host machine and limit the resource of each container, we cannot limit the node application inside the container which are not max workers specified, even not to mention those node packages nesting deeply in the dependency tree. It will fork itself as many as the amount of host machine's cpus, which will exceed the container's cpu limitation, and cause the application running very slowly.
Most node packages use os.cpus().length to decide how many child process it should fork, so I think this bug should be taken a look.