[Docker] How to limit virtual memory size?

Hello,

I deployed n8n on Docker and to my surprise, it uses 10GB of Virtual Memory. It would not usually be a problem, but I’m running n8n in a Docker-in-Docker setup (it’s complicated, short answer is it’s the easiest way to run Vanilla Docker on TrueNAS SCALE) my host OS reports the 10GB as actually used!

So I went looking for options to reduce the virtual memory:

  • setting NODE_OPTIONS=--max-old-space-size=4096 environment variable did not work
  • overriding the Docker entrypoint but I could not pass NODE_OPTIONS environment variable and --max-old-space-size is not a valid n8n option

Is there any way to reduce the virtual memory size I am not aware of?
Thanks!

Hi @axeleroy, you could limit the amount of memory used by a docker container. You might want to check out the docker documentation on this: Runtime options with Memory, CPUs, and GPUs | Docker Documentation

Something like docker run -it --rm --name n8n -p 5678:5678 -v ~/.n8n:/home/node/.n8n -m 320m n8nio/n8n:0.206.1 would limit your n8n container to 320 MB. You can verify this by running docker stats:

I’ve already tried this (though with deploy.resources.limits.memory and deploy.resources.reservations.memory as I use docker-compose 3) but the limit does not seem to be honoured.

# docker stats
CONTAINER ID   NAME                             CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O   PIDS
6eddda58f0a0   n8n                              0.00%     182MiB / 512MiB       35.54%    146kB / 38.9kB    0B / 0B     12

# top
Load average: 0.22 0.26 0.26 1/1917 984816
  PID  PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND
10042501004184 1000     S    10.4g  31%   8   0% node /usr/local/bin/n8n

Here is what worked for me in docker-compose.yml

    deploy:
        resources:
            limits:
              cpus: 1
              memory: 550M

That’s what I did and yet, docker stats shows 35% usage of my 32GB of RAM (cf. my previous reply)

    deploy:
      resources:
        limits:
          memory: 512M
        reservations:
          memory: 256M

Mine works perfect - took me a few tries to get it right and maybe a reboot.
I know it wasnt click and done - not sure why!

Good to know. I was suspecting I had to reboot, as even with the container shut down, the memory isn’t freed.

I checked again and it is not working exactly as set in limits
image
When I set mem limit to 512 - it still sets it to 650, but at least it is not using up all RAM as it was before

I just rebooted my NAS, the RAM was freed before I started the n8n container. After I started it up, it reported 10GB of virtual memory usage even though it had memory limits in place :confused:

That sucks.
I am not sure if n8n can even use 10G even if went crazy on startup - the containers are pretty lean

Hey there,

Just a quick thought, when you ran the top command was that in the container or on the host?

I am just wondering if maybe you installed n8n on the host first and you have 2 instances running one on the host and one in the container.

I’m running top on the host and no, I haven’t installed n8n on the host.

Hey @axeleroy,

As far as I understand top isn’t able to list what is happening in a container if you run it on a host unless you are using docker exec to run top within the container from the host maybe.

If you are just running top on the host though I would quickly run n8n --version to see if you get a result, At the moment it kind of feels like it is running on the host somewhere outside of docker.

Maybe a quick sudo ps -ef | grep n8n would show something.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.