Search code examples
dockercgroups

Default per-container memory limit


I would like to have a default memory limit for each Docker container.

I know I can use --memory when running a container. Yet the problem is the host is shared by many developers and I can't expect everybody to remember to do it.

I want the containers run without an explicit --memory parameter to be limited to e.g. 4GB instead of being able to see the whole hosts memory.

I tried to setup CGroup limits as described in https://stackoverflow.com/a/46557336/1237617. The problem is that it's a limit on total memory used by all containers.

Can I setup a per-container memory limit?


Solution

  • I was able to realize this by adding a proxy in front of docker service. I use the proxy to inspect the JSON payload and modify the parameters to set the memory limit if it's absent.

    The final step is to modify the DOCKER_HOST environment variable to point to the proxy.

    socat might be useful if your proxy can't talk with sockets