<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: stefanjacobs</title>
    <description>The latest articles on DEV Community by stefanjacobs (@stefanjacobs).</description>
    <link>https://dev.to/stefanjacobs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F315410%2F7878f5c9-df83-47fd-b495-b1a56033eb40.jpg</url>
      <title>DEV Community: stefanjacobs</title>
      <link>https://dev.to/stefanjacobs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stefanjacobs"/>
    <language>en</language>
    <item>
      <title>nsenter: Entering a running process or container</title>
      <dc:creator>stefanjacobs</dc:creator>
      <pubDate>Tue, 04 Feb 2020 06:59:48 +0000</pubDate>
      <link>https://dev.to/stefanjacobs/nsenter-entering-a-running-process-or-container-46a3</link>
      <guid>https://dev.to/stefanjacobs/nsenter-entering-a-running-process-or-container-46a3</guid>
      <description>&lt;p&gt;Containers are great! They encapsulate a complete system; when configured correctly they are secure and if they run once, they run everywhere.&lt;/p&gt;

&lt;p&gt;When it comes to debugging things, containers start to get complicated. If e.g. a container is build running as a non root user and you &lt;code&gt;docker exec&lt;/code&gt; into it, but the container does not have &lt;code&gt;sudo&lt;/code&gt; or &lt;code&gt;netstat&lt;/code&gt; installed and you want to see its current network properties like open ports, how do you do that? Or if you want to see the environment variables that were injected to the container and the container was started with?&lt;/p&gt;

&lt;p&gt;One way to do that would be to copy a static binary of e.g. &lt;code&gt;netstat&lt;/code&gt; into the container and fiddle with root privileges. I asked myself (and Google): Is there not a better way to do something like that?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;nsenter&lt;/code&gt; to the rescue! With this command (you have to be root for that) you enter the so called namespace of a process on the host. All that you have to know is the PID of the process with e.g. &lt;code&gt;ps aux | less&lt;/code&gt;. See the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;## Enter Process namespace, and attach to network namespace of given PID&lt;/span&gt;
nsenter &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="nv"&gt;$PID&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After executing the script you just see a (new) plain shell. And now you can execute &lt;code&gt;netstat&lt;/code&gt; to see, if all ports that you wished for are open and listening. To leave the namespace just type &lt;code&gt;exit&lt;/code&gt; or &lt;code&gt;CTRL-D&lt;/code&gt; and you are back in the original shell.&lt;/p&gt;

&lt;p&gt;But as written earlier, it is also possible to enter the specific environment of a container. See the following example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Enter process namespace with the environment variables set&lt;/span&gt;
&lt;span class="nb"&gt;sudo&lt;/span&gt; /usr/bin/nsenter &lt;span class="nt"&gt;--target&lt;/span&gt; &lt;span class="nv"&gt;$PID&lt;/span&gt; &lt;span class="nt"&gt;--mount&lt;/span&gt; &lt;span class="nt"&gt;--uts&lt;/span&gt; &lt;span class="nt"&gt;--ipc&lt;/span&gt; &lt;span class="nt"&gt;--net&lt;/span&gt; &lt;span class="nt"&gt;--pid&lt;/span&gt; &lt;span class="nb"&gt;env&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; - &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;sudo cat&lt;/span&gt; /proc/&lt;span class="nv"&gt;$PID&lt;/span&gt;/environ | xargs &lt;span class="nt"&gt;-0&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt; bash
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Given a process PID and executing that script, you are in a new shell showing even the environment variables that were given, when starting the process - awesome 🥳!&lt;/p&gt;

&lt;p&gt;Last but not least a little helper script that is also available as a &lt;a href="https://gist.github.com/stefanjacobs/99fa0f8c65e24532307406fe5255580d"&gt;Gist here&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Copy&amp;amp;paste the following snippet to a machine to create a script that enters a namespace of a given PID with environment set: ./dockerEnter 1234&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"sudo /usr/bin/nsenter --target &lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;1 --mount --uts --ipc --net --pid env -i - &lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;(sudo cat /proc/&lt;/span&gt;&lt;span class="se"&gt;\$&lt;/span&gt;&lt;span class="s2"&gt;1/environ | xargs -0) bash"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; enterDocker.sh&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;chmod&lt;/span&gt; +x enterDocker.sh&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;For detailed information see &lt;code&gt;man nsenter&lt;/code&gt;!&lt;/p&gt;

</description>
      <category>bash</category>
      <category>docker</category>
      <category>kubernetes</category>
    </item>
  </channel>
</rss>
