skip to Main Content

So I am having the following issue:

I want to limit the resources Docker is able to use within WSL2.
The answers that I have currently found say that I can limit it with the WSL2 config file.

However, when I edit the file and limit the WSL2 RAM usage I encounter the issue that my WSL2 Ubuntu instance lacks in RAM to be able to build the Front-End project.

I have found a solution to limit Docker to run it on Hyper-V.
Now the issue is that I can’t start the Docker images of Back-End from WSL2 and that I should enable WSL2 again for Docker.

Running Back-End outside WSL2 is a lot of work (since some tools are not Windows-supported).


So I hoped to find a configuration to have specific settings for my WSL2 Ubuntu instance and have the rest limited via the global WSL2 config.

Or be able to start Hyper-V Docker images from WSL2

2

Answers


  1. Managing resource constraints and configurations between Docker, WSL2, and your Ubuntu instance might be complicated, but the required arrangement is likely achievable. When running containers, you may define resource restrictions to limit the resources that Docker containers utilize. You may, for example, use the --cpus and --memory options to limit CPU consumption and memory for particular containers. This ensures that Docker containers do not utilize all available resources.

    docker run --cpus 2 --memory 2g my-container
    

    This limits the container to two CPU cores and two gigabytes of RAM. You suggested editing the WSL2 config file to minimize RAM utilization, however this had a detrimental impact on your WSL2 Ubuntu instance. Instead of limiting resources for the whole WSL2 instance, try utilizing the previously described Docker-specific resource constraints. You may utilize the docker context capability to swap between contexts if you wish to run Docker containers under Hyper-V while still interacting with them via WSL2. This enables you to execute containers under Hyper-V before switching back to WSL2. To begin, set Docker to utilize Hyper-V as the default context.

    docker context create my-hyperv --docker "host=myswitchname"
    docker context use my-hyperv
    

    "myswitchname" should be replaced with the name of your Hyper-V virtual switch. When you’re ready to utilize Hyper-V for Docker containers, go to the Hyper-V context.

    docker context use my-hyperv
    

    When you wish to return to WSL2, type:

    docker context use default
    

    This manner, you can use Hyper-V to run Docker containers and yet access them from your WSL2 environment. If your WSL2 Ubuntu instance does not have enough RAM for frontend development, you may need to temporarily assign extra resources to it during developing. The WSL configuration allows you to change the resource allocation of the WSL2 instance. You can lower the resources again when you finish construction.

    Login or Signup to reply.
  2. Take a look of the official documentation.

    Docker containers don’t have any default limitations for the host’s resources usage.

    But, you can limit the resource of a specific contaienr with the –memory/-m --cpus/-c flags, such as:

    docker run --cpus=1 --memory=256m <image-name>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search