21

I do know that we can create a container with memory limitation like this

docker run -ti --memory-reservation 1G ubuntu:14.04 /bin/bash

but how to update the limitation of memory/CPU for existing container?

Yuwen Yan
  • 4,777
  • 10
  • 33
  • 63

3 Answers3

37

Docker Update Command

docker update --memory "1g" --cpuset-cpus "1" <RunningContainerNameOrID>

this will update the "RunningContainerNameOrId" to use 1g of memory and only use cpu core 1

To up date all running containers to use core 1 and 1g of memory:

docker update --cpuset-cpus "1" --memory "1g" $(docker ps | awk 'NR>1 {print $1}')
oneklc
  • 2,659
  • 1
  • 20
  • 13
  • 1
    This is really useful, although unfortunately `resource updating isn't supported on Windows` for Windows containers – sparrowt Apr 04 '19 at 16:02
2

Dynamic resource allocation is currently not possible with only Docker. You would have to update the cpu/memory shares of the cgroup (control group). You must create a new container to change the resource limitations with Docker.

There is a Docker GitHub issue for dynamic resource configuration. This pull request suggests it will be added in Docker 1.10 with a docker set or docker update command. This command will allow you to update a container's configuration at runtime.

If you are running on a systemd enabled system, you can leverage that as well to change the cpu or memory shares. An example can be found in this blog post.

Tom Verelst
  • 15,324
  • 2
  • 30
  • 40
  • 2
    thanks Tom, since the PR you mentioned has been merged and released, @oneklc 's answer should be the latest one now. – Yuwen Yan Oct 08 '16 at 08:42
1

I'd have saved hours if even one person was running docker on WSL and mentioned that you need to do this from the .wslconfig file if you're running docker with wsl integration, on windows. I'm writing it right now for if another ME comes along and now they know the trick. https://itnext.io/wsl2-tips-limit-cpu-memory-when-using-docker-c022535faf6f

It's an old question, but if you come along and need this new answer, on that one platform, at least it'll get you thinking.

Keyosuke
  • 11
  • 1