The Docker container of the NAS has no CPU limit, and the Family TV freezes as soon as the seeding machine is run.
It started with TV cards. The whole family was watching a movie on Friday night. Jellyfin transcoded a 4K movie. The picture paused every few seconds, but the sound was normal. I stood up and looked at the NAS. The chassis fan was whirring and the hard drive lights were flashing. The first reaction at that time was about the trays. After all, the N100 only has two trays, and the seed trays are hung all year round. I checked docker stats via ssh and was stunned on the spot.: qBittorrent is a container with 24% of the CPU occupied. Jellyfin is so hungry that only a dozen% is left, and the transcoding thread cannot grab the core at all.
As a seed machine, checksum and hashing are fully enabled, and it is fully loaded as soon as it is started. But the CPU it grabbed was exactly what Jellyfin wanted to use for transcoding. The two containers were fighting on the same N100, and the loser was me sitting on the sofa. After handling this matter, I discovered that I had limited the memory of the container, but I had never limited the CPU of the container - the memory pit was stepped on last year, and the CPU pit was only made up this year.
Make it clear first: Memory limit and CPU limit are two completely different accounts.
Last year, I wrote about container memory and added data to peak containers. -m 512m This kind of memory cage. There was a saying I remember very clearly at that time: Limits are for containers where "peaks are meaningless". But memory and CPU have completely different temperaments, and the cages must be fought separately.
When the memory is exceeded, the kernel directly kills the process, the container disappears, and the problem appears as "gone". When the CPU exceeds the limit, no one kills it. Everyone queues up and shares time slices. The problem is "slow". Therefore, the memory limit controls life and death, and the CPU limit controls speed. This determines two things: The memory limit can be set very tight, otherwise it will restart if killed. ; Instead, be careful about the CPU limit. If you set it too tightly, the container will not die, it will only be so slow that you suspect it is broken.
There is another number that must be understood. The CPU column in docker stats is not from 0 to 100, but from 0 to the percentage of the total number of cores on your machine. My N100 has four cores. If a single container is fully loaded with one core, it will show 100%, and if all four cores are used, it will be 400%. qBittorrent's 240% that night, meaning it bit two multi-cores and held on. People who see the number 200% for the first time are easily startled and think that it is an explosion, but it is actually the normal scale on a quad-core machine.
first step: Capture the scene, don’t rely on your feelings
Before taking action, spend ten minutes figuring out who is grabbing the CPU. Ssh into the NAS and run docker stats , stare at it for a minute or two, and write down the number of CPU columns. If you find it troublesome to copy by hand, just add a parameter.: docker stats --no-stream , exit after outputting only one row of results, which is convenient for archiving and comparison.
The numbers I copied down that night are as follows:: qBittorrent jumps between 80% and 240% all year round, and directly reaches 240% when seeding and verifying the hash. ; Jellyfin is usually at a fraction of a percent, but when transcoding it instantly jumps to over 300% and then drops back down again. ; Immich recognizes photos during the day and eats 150% more ; The remaining six containers add up to less than 30%. The meaning of this account is very clear: qBittorrent is a habitual offender, Jellyfin does serious business occasionally, and Immich runs during the day at different times. Each has its own temper.
There is a criterion here, which is more important than the number itself: Grab the CPU container, its work is not worth waiting for. When qBittorrent is seeding, no one cares if the verification is ten minutes slow. The fun of seeding is inherently slow. ; Jellyfin transcodes it and you can see it immediately on the TV once the card is turned on. You will understand it the first time your family calls for a card. Only containers that can afford it are allocated CPU limits. This is the same principle as the memory limit of "peak meaningless limit", but the dimension is changed.
Step 2: Cage, two ways of writing
You cannot change the limit of a container that is already running. You have to delete it and rebuild it. Just copy the original mounting directory and port. The data will be in the mounting directory and will not be lost. Add a parameter when creating a new command line:
docker run -d --cpus=1.5 qBittorrent镜像名
The following string of mounting parameters follow as they are. --cpus=1.5 means: This container uses at most one and a half cores of computing power. The speed of seed production has changed from the original "two cores to feed it" to "one and a half cores to feed it". The verification hash is about 20% slower, and the seed production machine does not matter at all. You can also use it if you have a strong family background. --cpuset-cpus 0,1 This way of writing directly pins it to the first two cores, allowing the transcoding container to use the third and fourth cores. It is physically isolated and no one can steal it from anyone else. I've used both: Daily use - cpus soft limit, temporary cpuset partition change on the night of peak transcoding, it works.
Use Compose to copy this paragraph and insert it into the corresponding service block.:
services:
qbittorrent:
cpus: 1.5
mem_limit: 512m
Yes, it’s that short. One line of cpus manages the CPU, mem_One line of limit manages memory. I write both lines in my current compose file. Changed docker compose up -d Rebuild it and it will take effect. To build a container in the Feiniu interface, go to the resource limit column in the container settings. The CPU area is usually where you can check and fill in the number. The principle is the same, but the interface name is different. If you can't find it, use SSH to go to the command line. You can reach the same goal by different methods.
Step 3: For acceptance, you will need to measure again after the limit is reached.
It's not finished until you finish loading the cage. You have to go back and retest. The next night while watching a movie at home, I ran docker stats again.: qBittorrent was pressed within 150%. I took a look at the seeding throughput and found that the speed dropped by less than 20%. Converted to a 50G calibration, it went from 8 hours to 10 hours. It is the same even if I sleep on it. Jellyfin can stably get around 280% when transcoding, and the picture on the TV that pauses every few seconds disappears. What I value most in this round of acceptance is Jellyfin’s numbers.: The essence of CPU limitation is not to slow down anyone, but to return the stolen computing power to the person who is really stuck.
| container | Can you afford to wait? | my limit | Changes after deadline |
|---|---|---|---|
| qBittorrent | You can afford to wait, sowing seeds should be done slowly. | cpus 1.5 | Verification is 20% slow, no one is harmed |
| Jellyfin | I can’t wait, I’ll be scolded immediately if I get stuck | No limit, reverse it to protect it | Transcoding is stable at 280% |
| Immich | Can't afford to wait, peak time is off during the day | cpus 2 + manual trigger recognition | No more stuck recognition during the day |
| AdGuard Home | I can’t wait, the DNS is down and the whole room is shouting | No limit | I only ate a fraction of the food |
That table is based on my family's accounts. Don't just copy the numbers. You have to measure the temper of your container by yourself. Three containers were deliberately left in the open row because their jobs could not wait. CPU limitation is never a shackles on all containers, but only for the habitual offender who "can afford to wait but still dominates the computing power".
Three overturning points, I will step on them first for you
The first one is the numerical unit. --cpus=1.5 Fill in the number of cores, not the percentage. 0.5 is half a core. If you want to fill in 50%, you have to convert it into the number of cores divided by the total number of cores. 50% of a quad-core machine is cpus 2. I confused these two units, and wrote to Immich for the first time. --cpus=50 , as a result, one person was assigned a quota of twelve cores, which was equivalent to no limit, and it was a joy in vain. The second is that the running container cannot be changed. It must be deleted and rebuilt, and the data cannot be lost. However, the port and parameters must be copied to the original ones. If a restart strategy is copied, it will not start up after the power is cut off in the middle of the night. I have written about the troubleshooting ideas for the container not starting up before. The third is not to use CPU quota as performance optimization. Its positioning is resource arbitration, which prevents anyone from starving to death, not to make the machine faster. The machine is really not enough. Adding memory and replacing the CPU is another matter. The link to that account is at the end of the article.
Checklist for different family backgrounds
The real ones who should copy this set of limits are the owners of machines with small memory and small cores. With the configuration of N100 quad-core and 8G memory, there will be a fight sooner or later if there are more than one container. On the other hand, the J4125 quad-core machine with core display transcoding and 16G memory starts with a total of three or five containers. There is no need to rush to the cage. It is not too late to come back and read this article if there is a problem.
| family property | suggestion |
|---|---|
| N100 quad-core/8G, about nine containers | There is a limit for those who can afford it, and there is no limit for transcoding DNS insurance. |
| J4125/16G, five or six containers | Measure first and then do it. For repeat offenders, only one is enough. | Machines mainly focused on nuclear display and transcoding | Transcoding and running the core display does not consume the CPU. This type of machine does not have this problem. |
The last line in the table is what I want to say most: Those who are really big on transcoding should take the path of hardware decoding with core display. Jellyfin uses QSV hard decoding. The transcoding basically does not consume the CPU, and the root cause of the problem of CPU limit does not exist. I finished limiting the CPU that night and the TV was no longer stuck. After two weeks, I still paired it with the Jellyfin hardware. From then on, the N100 core display started to work. Doing both ends will make the machine really loose and fast.
Here are some related links:: Container memory account This is the previous episode of this article, where the idea of "Two Accounts at the Peak" comes from. It is recommended to read it first. ; Docker introductory lesson Mounting the mirror port, newbies will come back after finishing the work ; How to write Compose It goes well with this article ; I can’t get up and open the container in the middle of the night That article troubleshooting ; I really need to spend more money to exchange for a machine. Selection account of N100 and J4125 Take care of that road. After the core display transcoding is configured, the CPU limit for this article is just a guarantee.
