'services[web].mem_limit' invalid size: 'lots' means the mem_limit of a service is not a size Compose can read. Write a number with a unit, such as 256m.

cpus fails the same way with a different message, failed to cast to expected type: strconv.ParseFloat: parsing "many"; it wants a plain number such as 0.5.

The error

PS> docker compose -f compose.yaml config
decoding failed due to the following error(s):

'services[web].mem_limit' invalid size: 'lots'

The file:

name: fix10-cy-q8
services:
  web:
    image: alpine
    command: ["sleep", "1"]
    mem_limit: lots

The cpus variant, with cpus: many in place of the memory line:

PS> docker compose -f compose.cpus.yaml config
error while interpolating services.web.cpus: failed to cast to expected type: strconv.ParseFloat: parsing "many": invalid syntax

Why it happens

Compose turns mem_limit into a number of bytes while it loads the file, as the fixed run below shows. lots has no number in it, so the conversion fails and the message quotes the value. cpus is converted to a decimal number, and many is not one, which is what the ParseFloat text in the second message says.

Both messages appear on config, so nothing is created. Neither names a line; services[web].mem_limit and services.web.cpus name the service and the key, which is enough to find them.

The two messages are worded differently even though the mistake is the same kind. The memory one starts with decoding failed due to the following error(s): and a blank line, the CPU one with error while interpolating. Neither wording says "wrong unit" or "not a number" in plain words, so read the quoted value at the end: that is the text Compose could not convert.

The fix

name: fix10-cy-q8
services:
  web:
    image: alpine
    command: ["sleep", "1"]
    mem_limit: 256m
    cpus: 0.5
PS> docker compose -f compose.fixed.yaml config
...
    cpus: 0.5
    mem_limit: "268435456"
...

256m is accepted and config prints it in bytes, 268435456, which is 256 times 1024 times 1024: the m unit is mebibytes. 0.5 for cpus passes through as written, a decimal number as the ParseFloat message asked for.

How it was reproduced

Docker Desktop on Windows 11, Docker Compose v5.7.0, engine 29.9.3, Windows PowerShell 5.1. A one-service compose.yaml with mem_limit: lots, and a copy with cpus: many, gave the two messages above on docker compose -f ... config with exit code 1; with mem_limit: 256m and cpus: 0.5 the same command printed the model with exit code 0. No container was started. The project name fix10-cy-q8 is the test's own.

Frequently asked

What does mem_limit invalid size mean in docker compose?
The mem_limit value is not a size Compose can read. In the test lots was refused; 256m was accepted and config showed it as 268435456 bytes.
How do I set a memory limit in docker compose?
mem_limit: 256m under the service validated in the test. config converts it to bytes when it prints the model.
What does strconv.ParseFloat parsing invalid syntax mean for cpus?
cpus was given text that is not a number, such as many. Write a decimal number such as 0.5.

More decoded errors in the Fixes category. A bad value that config lets through and only up refuses is shown in invalid restart policy.