Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set a memory limit for a docker container created by visual studio code remote devcontainer configuration

My configuration:

  • Laptop with VSCode (with VSCode remote development extension) & Docker Desktop.
  • Server with Docker Host.
  • My project locally just have: devcomposer.json, and a docker-compose.json.

My process:

  • Open SSH tunnel to server.
  • In VSCode I hit open in container (which builds the container).
  • My remote development container builds fine, and I'm able to use everything there.

My little PROBLEM:

When running a simple chown over hugely populated folders, such as node_modules, the container memory requirements go mad, crashing not just the container, but my whole server....

I've tried:

  1. Setting runArgs with:

    • ["-m","3g"]
    • ["--memory=3g"]
    • ["--memory=\"3g\""]
    • ["--memory-reservation=3g"]
  2. Setting a ulimit in my Dockerfile, for the user that later runs the chown command.

  3. Deleting all images and containers in Docker host, to force --no-cache (which I haven't found how to inject it neither :-/ )

HELP! Nothing works... Someone has any clue as to what could I do to prevent the container to consume all the memory from the server?

Repo with config: https://github.com/gsusI/vscode-remote_dev-config_test

like image 202
JesusIniesta Avatar asked Oct 15 '25 15:10

JesusIniesta


1 Answers

In case anyone find themselves in the same infinite loop. I found what's the issue!!

It appears that runArgs is not used when using docker-compose, hence configurations here have no effect.

I know!! You'll expect a warning somewhere, right?

The next best option is to do this through the docker-compose.yml file, right? Well, this is only true if you're using docker-composer version 2, as version 3 would only work with Docker swarm. In my case, I switched to version 2, and now everything works smoothly.

TL;DR

Your docker-compose.yml file should look like this:

version: '2'
services:
  <your-service-name>:

    ...

    mem_limit: 2g
    mem_reservation: 2g

Check this for syntax hints: https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources

like image 118
JesusIniesta Avatar answered Oct 18 '25 06:10

JesusIniesta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!