What is the PID in the host, of a process running inside a Docker container?
<p>There are several processes running in a Docker container, their PIDs are isolated in the container namespace, is there a way to figure out what are their PIDs on the Docker host?</p>

<p>For example there is an Apache web server running inside a Docker container, 
(I use Apache+PHP image from <a href="https://hub.docker.com/_/php/" rel="noreferrer" title="Docker Hub">Docker Hub</a>), and the Apache, when it starts, creates more worker processes inside the container. Those worker processes are actually handling incoming requests. To view these processes I run <code>pstree</code> inside the docker container:</p>

<pre><code># pstree -p 1
apache2(1)-+-apache2(8)
           |-apache2(9)
           |-apache2(10)
           |-apache2(11)
           |-apache2(12)
           `-apache2(20)
</code></pre>

<p>The parent Apache process runs on PID 1 inside of the container process namespace. However from the host's perspective it can be also accessed, 
but its PID on the host is different and can be determined by running <code>docker compose</code> command:</p>

<pre><code> $ docker inspect --format '{{.State.Pid}}' container
 17985
</code></pre>

<p>From this we can see that the PID 1 from within the container process namespace maps to PID 17985 on the host. So I can run <code>pstree</code> on the host, to list the children of the Apache process:</p>

<pre><code>$ pstree -p 17985
apache2(17985)─┬─apache2(18010)
               ├─apache2(18011)
               ├─apache2(18012)
               ├─apache2(18013)
               ├─apache2(18014)
               └─apache2(18164)
</code></pre>

<p>From this I assume that the same way how PID 1 in the container maps to PID 17985 on the host, it also maps: </p>

<ul>
<li>PID 8 in container to PID 18010 on host, and</li>
<li>PID 9 to PID 18011;</li>
<li>PID 10 to PID 18012 
and so on...</li>
</ul>

<p>(This allows me to debug the processes from docker container, using tools that are only available only on the host, and not the in the container, like strace)</p>

<p>The problem is that I don't know how safe is to assume that pstree lists the processes in the same order both in the container and in the host.</p>

<p>Would be great if someone could suggest a more reliable way to detect what is a PID on the host of a specific process running inside the Docker container.</p>