LXC + Docker

In October I gave a presentation at Bravi about LXC + Docker. Since I was working hard in project at that time I almost forgot to post my presentation here.

I would like had included some descriptions about the presentation to make the slides more self explanatory. But is almost Christmas and I’m a bit lazy write down anything right now. Sorry ;(

But I recorded this presentation. I will try to included the video here later.

References:

  • The images of the first part were took from Ben Golub - Docker Presentation.
  • The images about Docker images and containers were took from Docker documentation.

Libxmljs - Finding XML elements with namespaces using xpath

Today I spent a stupid time trying to parse a XML input to JS. Even using a great module libxmljs I was having problems to read any element that was not inside of client element (xpath: //client).

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <Response xmlns="http://tempuri.org/">
      <Result>
        <client xmlns="">
          <msg>SEARCH OK.</msg>
          <code>0</code>
        </client>
      </Result>
    </Response>
  </soap:Body>
</soap:Envelope>

Why I could not get any of these elements?

  • soap:Envelope - xpath: //soap:Envelope
  • soap:Body - xpath: //soap:Body
  • Response - xpath: //Response
  • Result - xpath: //Result

This question forced me study a bit more about xml structure. In a first moment I didn’t want to study it because XML is too boring. But I was spending too much time just guessing and searching a solved solution for my problem.

Reading XML Namespaces and How They Affect XPath and XSLT I could finally understand why I could get only elements inside of client.

Summarizing that article there are three types of namespaces:

Prefix Namespaces

A namespace is composed of a name and a URI specification. The scope of the prefix-namespace mapping is that of the element that the namespace declaration occurs on as well as all its children.

<bk:bookstore xmlns:bk="urn:xmlns:25hoursaday-com:bookstore">
 <bk:book>
    <bk:title>Lord of the Rings</bk:title>
    <bk:author>J.R.R. Tolkien</bk:author>
    <inv:inventory status="in-stock" isbn="0345340426"
        xmlns:inv="urn:xmlns:25hoursaday-com:inventory-tracking" />
 </bk:book>
</bk:bookstore>

Default Namespaces

Since use prefix namespace for all element is too verbose. Any element without a prefix namespaces is associated to the default namespace.

<bookstore xmlns="urn:xmlns:25hoursaday-com:bookstore">
 <book>
    <title>Lord of the Rings</title>
    <author>J.R.R. Tolkien</author>
    <inv:inventory status="in-stock" isbn="0345340426"
        xmlns:inv="urn:xmlns:25hoursaday-com:inventory-tracking" />
 </book>
</bookstore>

Undeclared Namespaces

If an element has blank namespace (xmlns="") then it is a undeclared namespace and any children element without a prefix namespace is associated to that undeclared namespace.

<bookstore xmlns="urn:xmlns:25hoursaday-com:bookstore">
 <book xmlns="">
    <title>Lord of the Rings</title>
    <author>J.R.R. Tolkien</author>
    <inv:inventory status="in-stock" isbn="0345340426"
        xmlns:inv="urn:xmlns:25hoursaday-com:inventory-tracking" />
 </book>
</bookstore>

Understanding these namespaces helped me finally solve my problem. The reason I could only get the client element and its children was because it was using an undeclared namespace. Using libxmljs is necessary specify the namespace on reading an element with namespace. Which means I was not passing the namespace for those elements with namespace and their children.

This is simple example to clarify the final solution:

Finding out to which docker container a process belongs to

Linux containers run all over the same Linux Kernel. It means all process running inside each container will be visible in the host machine. Due that, how do we know if a process is running in the host or in a container? And how do we discover in which container that process is running? Lets solve that based in a real example.

This week I was running some tests in one of the Bravi’s server and I realized there was a weird grunt process using 30% of CPU. That was weird because should not exists a grunt process running in a staging/production environment and neither should be using so much from the CPU.

top

Running this command ps -axfo pid,uname,cmd I got the full list of process running in that machine. In this list I could find that process with id equals to 2973 and its top parent process.

With the top process id I could finally find the docker container running that grunt process.

Javascript Double Not-Operator(!!)

In javascript Falsy and Truthy values are used all over the place.

For example the following values are always falsy:

  • "" (empty string)
  • undefined
  • false
  • 0
  • null
  • NaN

And any other value is truthy.

Since undefined and empty string are falsy values then we could replace this code below:

if (typeof x !=== 'undefined' && x !=== '') {
  // not emtpy
}

with a falsy/truthy verification:

if (x) {
  // not emtpy
}

To set the result of falsy/thruthy verification to a variable is possible to use a double not-operator(!!) which will force a type casting to Boolean.

var xIsEmpty = !!x;

Actually !! is not a operator it is only the ! operator twice. The first ! will cast to boolean inverting the result. Then the second ! will invert again the value so it is the expected boolean value.

var x = 0;        // falsy
console.log(!x);  // true   (it is not what we want)
console.log(!!x); // false

Node Inspect Snippet for Sublime

Is pretty common use console logs in nodejs to check the value of variables you are working on.

But only the console.log is not good enough some times. Mainly if the object you are checking has deep properties.

For these cases I usually use inspect because it allows improve the log defining some options (showHidden, depth, colors and customInspect).

Due that I wrote a simple sublime snippet to help me create the logs:

Typing inspect will generate this snippet:

console.log('\n---->', require('util').inspect(obj, { depth: null, colors: true }));

Which will print something like this:

Node Inspect