Skip to content

Instantly share code, notes, and snippets.

@jreisinger
Last active April 6, 2018 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jreisinger/2f87098558d541cdbb7eb30b86163c39 to your computer and use it in GitHub Desktop.
Save jreisinger/2f87098558d541cdbb7eb30b86163c39 to your computer and use it in GitHub Desktop.

Limiting a Docker container's memory resource


Terminology

  • Docker server - the docker command run in daemon mode on a Linux host

  • Docker image - packaged application with all needed files (libs, configs)

      $ docker pull ...
      $ docker build ...
    
  • Docker container - a chrooted process that has been instantiated from a Docker image

      $ docker run ...
    

Docker and host resources

  • a container has no resource constraints by default
  • Docker provides a way to limit memory, CPU and block IO resources
  • your kernel must support Linux capabilities (docker info | grep WARNING)

Linux and memory resource

  • if the kernel detects that there is not enough memory, it starts killing processes
  • any process is subject to killing (including Docker)
  • a process that uses lot of memory but has not been running for long time is a most likely candidate to get killed (OOM)

Docker and memory resource

  • Docker adjusts OOM priority in the Docker daemon so it's less likely to get killed
  • the OOM priority on containers is not adjusted so they are more likely to be killed than the Docker daemon
  • you should not allow a container to consume too much of the host machine’s memory

Demo - memory limiting

$ git clone git@github.com:jreisinger/mem-muncher.git
$ cd mem-muncher
$ docker build -t mem-muncher .
$ sudo swapoff -a
# in separate terminal tab/window
$ docker stats
$ docker run -it --rm --name mem-muncher mem-muncher
# in separate terminal tab/window
$ docker kill mem-muncher
# limit the memory to 500MB and forbid access to swap
$ docker run -it --rm --name mem-muncher --memory=500m mem-muncher
$ sudo swapon -a 

More

https://docs.docker.com/config/containers/resource_constraints/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment