Restrict docker container resource usage with docker compose

By default, resources available to containers are not limited. However, sometimes, you want to make sure a container is not going to use too much processing power or memory.

To achieve such a thing, in the docker-compose.yml file, add the following sections to the service you want to restrict:

      cpus: "1.0"
      memory: 100M
memswap_limit: 100M

This will effectively limit the container to use at most one CPU and 100 megabytes of memory.

You can specify a float for cpus. You can specify gigabytes (use G)for memory. Always use the same value for memwap_limit and memory.

Note that if the container tries to use more memory than the limit, it will get killed and will be restarted according to the policy. This can cause some issues if your app always requires this much memory (I’m looking at you clamd).

This entry was posted in Computer, Docker, Linux, Software. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.