Sobes.tech
Middle+

We see a top process that lives in memory. On a server with 100 containers. How to determine which container this process belongs to?

sobes.tech AI

Answer from AI

To determine which container on the server a specific process belongs to, several approaches can be used:

  1. Using cgroup and namespace:

    • Each container is isolated using namespaces and cgroups.
    • In Linux, you can check the cgroup of a process, for example:
    cat /proc/<PID>/cgroup
    
    • The output will contain information related to the container (e.g., container ID or cgroup path).
  2. Using Docker CLI:

    • Get a list of all containers and their main process PIDs:

docker ps -q | xargs docker inspect --format '{{.Id}}: PID={{.State.Pid}}'


   - Match the process PID from `top` with the main process PID of the container.

3. **Using utilities like `nsenter` or `pidof`:**
   - Check which namespace a process is in.

4. **Using `docker top <container_id>`:**
   - Shows processes inside the container.

5. **Automation:**
   - Write a script that iterates over containers, retrieves their processes, and looks for a match with the PID.

Thus, the key is to match the process PID with the PIDs of processes inside containers, using information from cgroup or Docker tools.