skip to Main Content

I have a debian slim docker container running an ASP.Net Core 3.1 application. (mcr.microsoft.com/dotnet/aspnet:3.1.16-buster-slim)

I would like to be able to get the total memory used by the container that it is running the application.

NOTE: I am not looking for the memory used by the application/process. That is easily done with Process.GetCurrentProcess().WorkingSet64.

How can I get the memory usage for the container from the application using ASP.Net Core?

For bonus points, I would love to know CPU % utilization and available memory (again, both for the linux container, not the runing processing.

2

Answers


  1. to get the memory usage for the container, You can use docker stats command as below:

    arief@localhost:~$ docker stats
    
    CONTAINER ID   NAME                         CPU %     MEM USAGE / LIMIT     MEM %     NET I/O           BLOCK I/O         PIDS
    0781c395264e   redis                        0.11%     3.988MiB / 3.844GiB   0.10%     3.71kB / 0B       4.15MB / 111kB    6
    3cef5d564e95   focused_proskuriakova        0.92%     836.8MiB / 3.844GiB   21.26%    1.12MB / 366kB    1.22GB / 276MB    145
    c14c90811277   sharp_kilby                  0.84%     712.3MiB / 3.844GiB   18.10%    15.8MB / 6.73MB   5.36GB / 5.5GB    135
    60edadf58c45   postservice_loadbalancer_1   0.01%     18.34MiB / 3.844GiB   0.47%     434MB / 744MB     4.86GB / 0B       3
    85801301269e   postservice_webservice_1     0.04%     48.51MiB / 3.844GiB   1.23%     142MB / 94.2MB    5.61GB / 0B       15
    c3daeb9dbeee   postservice_database0_1      0.15%     415.8MiB / 3.844GiB   10.56%    44.1MB / 35.7MB   6.4GB / 796MB     46
    907eec0d78f3   postservice_webservice_2     0.00%     44.27MiB / 3.844GiB   1.12%     142MB / 93.8MB    5.09GB / 0B       16
    4ca71cf44c8c   postservice_database1_1      0.13%     369.4MiB / 3.844GiB   9.39%     18MB / 16.6MB     6.83GB / 563MB    46
    dc0e636f241e   postservice_webservice_3     0.00%     45.52MiB / 3.844GiB   1.16%     143MB / 93.9MB    18.1GB / 0B       13
    4cde19536dfe   postservice_webservice_6     0.00%     44.65MiB / 3.844GiB   1.13%     142MB / 93.7MB    6.96GB / 0B       15
    7d4af1618129   postservice_webservice_4     0.04%     46.68MiB / 3.844GiB   1.19%     145MB / 96.4MB    14.9GB / 0B       14
    0526c3be6cee   postservice_webservice_5     0.00%     43.89MiB / 3.844GiB   1.12%     143MB / 93.9MB    5.9GB / 0B        15
    eb67e60ca98c   postservice_database2_1      0.13%     283.1MiB / 3.844GiB   7.19%     34.9MB / 29.9MB   6.91GB / 699MB    46
    04104bcffa22   some-rabbit                  0.40%     88.79MiB / 3.844GiB   2.26%     77.4MB / 86.9MB   51.9GB / 90.1kB   25
    
    
    Login or Signup to reply.
  2. First thing first, you need access to the Docker Engine from within the container. Just like with vanilla VM’s, you can’t inspect the host system unless you have some allowed way to get that data (a VM might know it’s a VM, but it shouldn’t be able to know what other VMs are on the host system without permission).

    The easiest method to get access is with the Docker Socket. You can also use the Docker REST API.

    If you’re able to, it might be a safe play to add a "read only" flag to the end of the volume. That way your software can’t go rogue as much and start deleting things willy-nilly.

    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
    

    Once you have the socket setup, you can use a library like Microsoft’s own Docker.DotNet to communicate over that socket.

    You can inspect any aspect of the Docker Engine, such as the "stats" of each/any container.


    I do want to echo nyctef‘s comment though. A container is different than a VM. A VM runs an entire OS and your software. A container is just a single (usually) process, so in most cases the memory used by your container and the memory used by your software process should be nearly identical (with the Docker overhead).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search