This Docker error occurs when you specify an invalid value for the --cpu-shares flag. CPU shares must be an integer value of 2 or higher (or 0 to use the default of 1024). The fix involves using a valid integer within the acceptable range.
The "invalid CPU shares" error indicates that Docker cannot accept the CPU share value you provided. CPU shares in Docker control how much CPU time a container gets relative to other containers when CPU resources are constrained. The --cpu-shares (or -c) flag sets a relative weight for CPU scheduling. Valid values are integers from 2 to 1024 (or higher), with 1024 being the default. A value of 0 is also accepted and defaults to 1024. Values of 1 or non-integer values (like floats or strings) are considered invalid. CPU shares only come into effect when multiple containers are competing for CPU resources. For example, if Container A has 1024 shares and Container B has 512 shares, Container A gets twice as much CPU time as Container B when both are CPU-bound. When there's no contention, containers can use all available CPU regardless of their share value.
Check that your CPU shares value is an integer of 2 or higher. The default value is 1024.
# Valid values
docker run --cpu-shares 512 myimage # Valid: integer >= 2
docker run --cpu-shares 1024 myimage # Valid: default value
docker run --cpu-shares 2048 myimage # Valid: higher than default
docker run -c 0 myimage # Valid: 0 defaults to 1024
# Invalid values that cause the error
docker run --cpu-shares 1 myimage # Invalid: must be >= 2
docker run --cpu-shares 0.5 myimage # Invalid: float not allowed
docker run --cpu-shares "high" myimage # Invalid: must be numericTo check your current command, review the exact value being passed to --cpu-shares or -c flag.
Update your docker run command to use a valid integer value:
# Change from invalid value
docker run --cpu-shares 1 -d nginx
# To valid value (minimum is 2)
docker run --cpu-shares 2 -d nginx
# Or use a standard proportion value
docker run --cpu-shares 512 -d nginx # Half of default
docker run --cpu-shares 1024 -d nginx # Default (same as omitting)
docker run --cpu-shares 2048 -d nginx # Double the defaultCommon CPU share values:
- 256: 1/4 of default priority
- 512: Half of default priority
- 1024: Default priority (same as not specifying)
- 2048: Double the default priority
When using Docker Compose, ensure cpu_shares is defined as an integer:
# docker-compose.yml
version: "3.9"
services:
web:
image: nginx
cpu_shares: 512 # Valid: integer value
worker:
image: myworker
cpu_shares: 1024 # Valid: default weightCommon mistake with .env files:
# This can cause issues if CPU_SHARES is read as a string
services:
web:
cpu_shares: ${CPU_SHARES} # May be parsed as stringFix by ensuring proper type conversion:
services:
web:
cpu_shares: ${CPU_SHARES:-1024} # Default to 1024 if not setMake sure your .env file has a plain integer:
# .env
CPU_SHARES=512If you need to update the CPU shares of a running container:
# Check current container settings
docker inspect --format '{{.HostConfig.CpuShares}}' container_name
# Update to a valid value
docker update --cpu-shares 512 container_name
# Verify the change
docker inspect --format '{{.HostConfig.CpuShares}}' container_nameNote: The docker update command requires the container to be using the appropriate cgroup driver. If you see errors, the container may need to be recreated with the new value.
When CPU shares values come from environment variables, ensure proper handling:
In shell scripts:
#!/bin/bash
# Ensure the value is an integer
CPU_SHARES=${CPU_SHARES:-1024}
# Validate before using
if [[ ! "$CPU_SHARES" =~ ^[0-9]+$ ]] || [ "$CPU_SHARES" -eq 1 ]; then
echo "Invalid CPU_SHARES value, using default 1024"
CPU_SHARES=1024
fi
docker run --cpu-shares "$CPU_SHARES" -d myimageIn CI/CD pipelines:
# GitHub Actions example
- name: Run container
run: |
docker run --cpu-shares ${{ vars.CPU_SHARES || '1024' }} -d myimageIn Python with docker-py:
import docker
client = docker.from_env()
# Ensure integer type
cpu_shares = int(os.environ.get('CPU_SHARES', 1024))
if cpu_shares < 2:
cpu_shares = 1024
container = client.containers.run(
'myimage',
cpu_shares=cpu_shares,
detach=True
)CPU shares require proper cgroup support. Check your Docker daemon configuration:
# Check cgroup driver
docker info | grep -i cgroup
# Expected output for cgroup v1 or v2
Cgroup Driver: cgroupfs
# or
Cgroup Driver: systemdIf you see "none" as the cgroup driver:
This can happen in rootless Docker or WSL environments. In these cases, CPU shares may not be supported:
# WSL users may see warnings like:
# WARNING: No cpu shares supportSolutions:
1. For WSL2: Ensure you're using the WSL2 backend in Docker Desktop
2. For rootless Docker: Ensure systemd and cgroup v2 are properly configured
3. Consider using --cpus flag instead for hard CPU limits:
# Alternative: use --cpus for hard limits
docker run --cpus 0.5 -d myimage # Limit to half a CPUUnderstanding CPU Shares vs Other CPU Controls:
Docker offers multiple ways to control CPU resources:
| Option | Purpose | Example |
|--------|---------|---------|
| --cpu-shares | Relative weight (soft limit) | --cpu-shares 512 |
| --cpus | Limit number of CPUs (hard limit) | --cpus 1.5 |
| --cpuset-cpus | Pin to specific CPUs | --cpuset-cpus "0,1" |
| --cpu-period/quota | Fine-grained control | --cpu-quota 50000 |
CPU Shares Calculation Example:
If you have three containers:
- Container A: 1024 shares (default)
- Container B: 512 shares
- Container C: 512 shares
Total shares = 2048
When all containers are CPU-bound:
- Container A gets: 1024/2048 = 50% CPU
- Container B gets: 512/2048 = 25% CPU
- Container C gets: 512/2048 = 25% CPU
Multi-Core Behavior:
On multi-core systems, CPU shares are distributed across all cores. A container with fewer shares can still use 100% of available CPU if other containers aren't competing.
Docker Swarm Considerations:
In Docker Swarm mode, cpu_shares may be interpreted differently. Swarm has been known to interpret shares as "number of CPUs" rather than relative weight, which can cause scheduling failures if the value exceeds available CPUs.
Windows Containers:
CPU shares work differently on Windows containers. The --cpu-shares flag maps to Windows job object CPU rate control. Some CPU options like --cpuset-cpus are not supported on Windows and will produce "invalid option" errors.
Debugging CPU Share Issues:
# Check container's CPU shares
docker inspect --format '{{.HostConfig.CpuShares}}' container_name
# Monitor CPU usage in real-time
docker stats --format "table {{.Name}} {{.CPUPerc}} {{.MemUsage}}"
# View cgroup settings directly (Linux)
cat /sys/fs/cgroup/cpu/docker/<container_id>/cpu.sharesdockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec