Namespace PID
In this tutorial, we are going to discuss namespace process ids (PID) in action.
As we discussed in the previous tutorial, we have a Linux system which is a docker engine and that has its own root process with process id of 1.
And that kicked of a set of processes with other process ids and on the same Linux host we have a docker container which has its own set of processes and its own set of processes and its root process with a process id of 1.
As we saw in the above picture, the process in the container which has a root process id of 1 is in fact just another process running on the underlying Linux system and that has its own separate process id which in this case is 5.
So this is what we are going to look at in this tutorial. We going to run the container on a docker host and we’re going to compare the process ids of the same process inside the container as well as outside on the Linux host.
So we’re going to pick one docker container. Now we’ll go to tomcat and we’ll look at the instructions for running the image using following command.
$ docker run -it --rm -p 8888:8080 tomcat:9.0 Unable to find image 'tomcat:8.0' locally 8.0: Pulling from library/tomcat f189db1b88b3: Pull complete 3d06cf2f1b5e: Pull complete edd0da9e3091: Pull complete eb7768aae14e: Pull complete e2780f585e0f: Pull complete e5ed720afeba: Pull complete d9e134700cfc: Pull complete e4804b33d02a: Pull complete b9df0c24315e: Pull complete 49fdae8eaa20: Pull complete 1aea3d9a32e6: Pull complete Digest: sha256:8ecb10948deb32c34aeadf7bf95d12a93fbd3527911fa629c1a3e7823b89ce6f Status: Downloaded newer image for tomcat:8.0 Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /docker-java-home/jre Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar 07-Dec-2020 15:25:01.006 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.53 07-Dec-2020 15:25:01.007 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 29 2018 14:42:45 UTC 07-Dec-2020 15:25:01.007 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.53.0 07-Dec-2020 15:25:01.007 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux 07-Dec-2020 15:25:01.007 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 5.4.0-56-generic 07-Dec-2020 15:25:01.007 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 07-Dec-2020 15:25:01.007 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-7-openjdk-amd64/jre 07-Dec-2020 15:25:01.008 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.7.0_181-b01 07-Dec-2020 15:25:01.008 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation 07-Dec-2020 15:25:01.008 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat 07-Dec-2020 15:25:01.008 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat 07-Dec-2020 15:25:01.008 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties 07-Dec-2020 15:25:01.008 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 07-Dec-2020 15:25:01.008 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 07-Dec-2020 15:25:01.009 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 07-Dec-2020 15:25:01.009 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs= 07-Dec-2020 15:25:01.009 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat 07-Dec-2020 15:25:01.009 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat 07-Dec-2020 15:25:01.009 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp 07-Dec-2020 15:25:01.009 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.17 using APR version 1.5.1. 07-Dec-2020 15:25:01.009 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 07-Dec-2020 15:25:01.012 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.1.0f 25 May 2017) 07-Dec-2020 15:25:01.111 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"] 07-Dec-2020 15:25:01.123 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"] 07-Dec-2020 15:25:01.125 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 547 ms 07-Dec-2020 15:25:01.150 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina 07-Dec-2020 15:25:01.150 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.53 07-Dec-2020 15:25:01.159 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/examples 07-Dec-2020 15:25:01.854 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/examples has finished in 694 ms 07-Dec-2020 15:25:01.854 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/docs 07-Dec-2020 15:25:01.878 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/docs has finished in 24 ms 07-Dec-2020 15:25:01.878 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/host-manager 07-Dec-2020 15:25:01.911 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/host-manager has finished in 33 ms 07-Dec-2020 15:25:01.911 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/ROOT 07-Dec-2020 15:25:01.936 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/ROOT has finished in 25 ms 07-Dec-2020 15:25:01.936 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /usr/local/tomcat/webapps/manager 07-Dec-2020 15:25:01.966 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory /usr/local/tomcat/webapps/manager has finished in 30 ms 07-Dec-2020 15:25:01.970 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-apr-8080"] 07-Dec-2020 15:25:01.982 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-apr-8009"] 07-Dec-2020 15:25:01.992 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 866 ms
One we run the above command tomcat starts up. As we can see the logs of the tomcat service starting up. Once it starts up then open a browser and go to the IP of our host/localhost and port and we can see the Apache tomcat web page.
So that confirms that docker container is running. So now i am going to stop this container because I didn’t actually running it in the background. I wanted to run it in the background.
$ docker run -d -it --rm -p 8888:8080 tomcat:8.0 42fd445fea95f833c4a7a13c97274ec82d629b7234bf5cdf0ab6a0e2e5d339c3 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 42fd445fea95 tomcat:8.0 "catalina.sh run" 40 seconds ago Up 18 seconds 0.0.0.0:8888->8080/tcp crazy_cerf
Now I am going to run the exec command to access the docker container and run commands inside the container.
$ docker exec -it 42fd445fea95 /bin/bash root@42fd445fea95:/usr/local/tomcat# ps -eaf UID PID PPID C STIME TTY TIME CMD root 1 0 4 13:55 pts/0 00:00:04 /docker-java-home/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.pro root 58 0 0 13:57 pts/1 00:00:00 /bin/bash root 63 58 0 13:57 pts/1 00:00:00 ps -eaf root@42fd445fea95:/usr/local/tomcat# $ docker exec -it 42fd445fea95 ps -eaf UID PID PPID C STIME TTY TIME CMD root 1 0 2 13:55 pts/0 00:00:04 /docker-java-home/jre/bin/java - root 64 0 0 13:59 pts/1 00:00:00 ps -eaf
So as you can see we are able to see there’s just one process running on that container. Since it is a tomcat container, its running a tomcat process and the process id as we can is 1.
So that’s the only process running on the tomcat container. And if we run ps -eaf command in docker host and grep for the same process.
$ ps -eaf | grep "docker-java-home" root 21140 21122 1 19:25 pts/0 00:00:05 /docker-java-home/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start ashok 21765 12197 0 19:32 pts/2 00:00:00 grep --color=auto docker-java-home
Here we should be able to see the same process running on my docker host. And but this time you can see that it has a different process id.
So that is telling us that the same process is running inside the docker container with a process id of 1. But on the docker host with a different process id.
So with the name space you’re able to give multiple process id to the same process.