199

As the title reads, I'm trying to assign more memory to my container. I'm using an image from docker hub called "aallam/tomcat-mysql" in case that's relevant.

When I start it normally without any special flags, there's a memory limit of 2GB (even though I read that memory is unbounded if not set)

Here are my docker stats

CONTAINER           CPU %               MEM USAGE / LIMIT       MEM %               NET I/O             BLOCK I/O           PIDS
ba57d6c9e9d2        0.22%               145.6 MiB / 1.952 GiB   7.29%               508 B / 508 B       0 B / 6.91 MB       68

I tried setting memory explicitly like so but with same results

docker run -d --memory=10g --memory-swap=-1 -e MYSQL_PASSWORD=password -p 3307:3306 -p 8081:8080 aallam/tomcat-mysql

I've read that perhaps the VM is what's restricting it. But then why does docker stats show that container size limit is 2GB?

mohan08p
  • 5,002
  • 1
  • 28
  • 36
kane
  • 5,465
  • 6
  • 44
  • 72

4 Answers4

309

That 2GB limit you see is the total memory of the VM (virtual machine) on which docker runs.

If you are using Docker Desktop you can easily increase it from the Whale icon in the task bar, then go to Preferences -> Advanced:

Docker Preferences

But if you are using VirtualBox behind, open VirtualBox, Select and configure the docker-machine assigned memory.

See this for Mac:

https://docs.docker.com/desktop/settings/mac/#advanced

MEMORY By default, Docker for Mac is set to use 2 GB runtime memory, allocated from the total available memory on your Mac. You can increase the RAM on the app to get faster performance by setting this number higher (for example to 3) or lower (to 1) if you want Docker for Mac to use less memory.

For Windows:

https://docs.docker.com/desktop/settings/windows/#advanced

Memory - Change the amount of memory the Docker for Windows' Linux VM uses

Robert
  • 33,429
  • 8
  • 90
  • 94
  • 143
    How do you do it without the gui? – Philippe Oct 04 '17 at 12:08
  • 18
    @Philippe, what is your use case? Docker for Windows or docker for Mac comes with guli. I think that your case is got native docker over Linux (without any virtual machine, which is the case for production systems/cloud). In such cases you don't need to set up the VM memory because docker has the hole machine for its uses. – Robert Oct 27 '17 at 01:39
  • 11
    Yeah, I figured that out later. So if I want my docker container to have >4GB of RAM i don't need to do anything if I develop on Linux? – Philippe Oct 27 '17 at 09:48
  • 1
    Can you assign to a Docker image more memory than what you have physically available? – kgf3JfUtW Dec 04 '18 at 17:41
  • 7
    It's not possible, mainly because you don't assign memory to docker containers, instead you *limit* how much memory a container can use. Note that the present answer is not about assigning memory to containers, it is about assigning memory to the virtual machine in which docker runs. – Robert Dec 04 '18 at 17:51
  • 1
    This solved my problem. But now on my ubuntu server I ran into the same memory problem and need to find a way to increase memory on linux docker. – Siddharth Pant Jan 13 '19 at 11:21
  • 5
    @SiddharthPant , docker running directly on a Linux box will use as much memory is available, hence there is no limit other than the phisical one. – Robert Jan 13 '19 at 21:05
  • @Robert Yeah actually there were many processes running on the system which were already consuming lots of RAM so I then had to decide to move my service to another ubuntu server. – Siddharth Pant Jan 15 '19 at 05:28
  • A similar process for those who use docker shipped with minikube (on Windows). Need to open Virtual Box -> select minikube machine -> Settings -> System -> increase your Base memory – Yury Kozlov Jan 12 '20 at 03:22
31

Allocate maximum memory to your docker machine from (docker preference -> advance )

Screenshot of advance settings: Screenshot of advance settings.

This will set the maximum limit docker consume while running containers. Now run your image in new container with -m=4g flag for 4 gigs ram or more. e.g.

docker run -m=4g {imageID}

Remember to apply the ram limit increase changes. Restart the docker and double check that ram limit did increased. This can be one of the factor you not see the ram limit increase in docker containers.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Market Queue
  • 581
  • 5
  • 7
24

If you want to change the default container and you are using Virtualbox, you can do it via the commandline / CLI:

docker-machine stop
VBoxManage modifyvm default --cpus 2
VBoxManage modifyvm default --memory 4096
docker-machine start
Brent
  • 1,324
  • 1
  • 15
  • 22
6

Screen shots for Docker Desktop V3.3.3 (Mac)

Docker Desktop Menu Dropdown

Docker Desktop Preferences

Derek Soike
  • 11,238
  • 3
  • 79
  • 74
  • Thank you for your answer! It works for me on `bigSur` – Maxim Kitsenko Jun 18 '21 at 10:09
  • At this window, I have the following message "You are using the WSL 2 backend, so resource limits are managed by Windows. You can configure limits on the memory, CPU, and swap size allocated to WSL 2 in a .wslconfig file." – Ilias Machairas Jun 16 '23 at 07:09