Docker Community Forums

Share and learn in the Docker community.

Limit resources without swarm

How can I limit cpu, memory or any other resources for a container without a swarm?

By using the right version of the docker compose specs!
Version 2 is aimed for use with docker-compose and supports ALL configuration items you can use with docker run.
Version 3 is aimed for use with docker swarm and adds configuration items required for swarm deployments, while renaming and moving other configuration items AND removing their counterparts for docker-compose deployments. For compatibility reasons docker-compose can deploy v3.x compose files, but it will ignore the swarm specifc configuration items.

Use latest v2 and check whatever the specs provide. I am sure there is a mem_limit, but unsure about a cpu limit. Example value for mem_limit: 1024M or 1G.

I found it, here is a working example:
version: ‘2.4’
services:
my-nginx:
image: nginx:latest
ports:
- “80:80”
mem_limit: 300m

More about the topic an be found here: https://docs.docker.com/compose/compose-file/compose-file-v2/

Since many Docker Compose users have complained about this incompatibility of compose v3 vs v2, the team has developed compatibility mode.

You can retain the same deploy structure that you provided and it will not be ignored, simply by adding the --compatibility flag to the docker-compose command (docker-compose --compatibility up), as explained here. I tested this with version 3.5 and verified with docker stats and can confirm that it works.

That is a good thing, thank you!