Docker Tip #78: Using Compatibility Mode to Set Memory and CPU Limits
Docker Compose's Compatibility mode may help you use certain API v2 properties inside of an API v3 Docker Compose file.
For example, let’s say you’re not using Docker Swarm but you still want to use resource limits on your containers.
Back with API v2, you could set
cpu_shares on your services but these properties aren’t compatible with API v3. If you try to use them with
version: 3.x then Docker Compose is going to yell at you.
There’s still cases where you may want to use
version: 3.x even if you’re not using Swarm, plus it’s beneficial to design your Compose file in such a way that makes it compatible with Compose and Swarm with the least amount of edits possible.
Setting resource limits in an API v3 compatible way:
version: "3.4" services: redis: image: "redis:alpine" deploy: resources: limits: cpus: "0.50" memory: "50M"
Now, if you ran this with
docker-compose up you would get this warning:
WARNING: Some services (redis) use the ‘deploy’ key, which will be ignored. Compose does not support ‘deploy’ configuration - use
docker stack deploy to deploy to a swarm.
You can get around that warning by running
docker-compose --compatibility up.
--compatibility mode is what lets us do this. It will attempt to convert that API v3 way of setting resource limits back into v2 compatible properties.
And you can verify it was set by running
docker-compose --compatibility config. You should see that the deploy’s resource limits were translated into
cpu_shares. You can re-run the command without
--compatibility to see the difference in output.
It also works with other properties too, but your mileage may vary. It’s not a perfect solution, so if you plan to use
--compatibility always double check to make sure Docker Compose is doing what you expect it to do. Using config is a great way to test that.