Welcome to this comprehensive guide on PID namespaces in Docker containers. In this tutorial, we will demonstrate how PID namespaces work by comparing the process IDs (PIDs) of a running process inside a Docker container versus on the Docker host. This explanation is intended to help you understand container isolation and how Docker handles process management.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Understanding PID Namespaces
On a Linux system running Docker, the Docker Engine functions as the host and operates with its own root process (PID 1). When you run a Docker container, it creates an isolated process namespace. Inside the container, the container’s root process is identified as PID 1, even though, on the host, this same process may have a different PID (for example, PID 5). This behavior is a key feature of containerization, ensuring that processes within containers run as if they each have their own unique process space.Running the Tomcat Docker Container
We’ll start by pulling the Tomcat web server image from Docker Hub. The following commands illustrate two common ways to run the container:-
To start the container interactively:
-
To run the container with port publishing (mapping container port 8080 to host port 8888):
http://{host-ip}:8888 in your browser. When you load this URL, you should see the Apache Tomcat web page, which confirms that the container is running successfully.
If the container is running in the foreground, you can stop it by pressing Ctrl+C.
Running in Detached Mode
For a production or testing environment, you might prefer to run the container in detached mode. Use the-d option as shown below. The example log output indicates the startup messages for the Tomcat server:
docker ps command. You should see the Apache Tomcat container listed, confirming that it is running. You can then access the web server to ensure it is operational.
Inspecting Processes with PID Namespaces
To illustrate the PID namespace, you can inspect the processes from inside the container using thedocker exec command. Replace the container ID (for instance, one starting with “5a5f912e0f0e”) as needed. The commands below show how to list running processes:
ps -eaf reveals that the Tomcat process is running as PID 1. However, when you run a comparable command on the Docker host and filter the output (for example, with grep docker-java-home), you will observe that the same process has a different PID. This clearly demonstrates that with PID namespaces, a single process can exhibit multiple PIDs—one inside its container namespace and another on the host system.
