Andrey (azangru) wrote,
Andrey
azangru

Today I learned...

I was playing with headless Chromium today, installed in a Vagrant box.

The reason for Chromium, in my case, was that I need to dynamically draw images, depending on parameters that come with http requests. I had been using a Node implementation of HTML5 canvas for this purpose, but it gets a little tedious with multiple lines of text: you need to take care of line breaks yourself, and to keep track of line heights, text width, text height, and all that nonsense. Which a browser, given a handful of CSS instuctions, can handle by itself, easily. And now that a proper browser (not just PhantomJS) can be run headlessly, in the command-line environment...

...So that's how Chromium ended up in my Vagrant box.

Then I killed several hours trying to connect to it from the host machine. I diligently read and re-read the manuals. I forwarded the desired ports from the virtual machine to the host, as explained in the docs. I made sure those ports were listened to by the host. But despite all those efforts, Chromium would start inside the virtual machine, and would be accsessible through port 9222 from within the virtual machine, but not from the host. Which made no sense. Thought I.

Then I remembered how a couple of years ago I had a similar trouble with a Node server running inside Vagrant. It, too, was unreachable from the host. And at that time a colleague of mine gave me a solution that worked. So I went to look into that solution.

The idea was beautifully simple, though it had not made sense to me back then. Chromium (and, apparently, that Node server those two years ago) by default binds to the loopback address, which is 127.0.0.1 (commonly known as localhost). Loopback address is, by definition, local to the machine on which the process is running. So when I attempt to access localhost:9222 on the host machine, I am accessing 127.0.0.1 of the host, not 127.0.0.1 of the guest, where Chromium is running.

The solution? When starting Chromium in the virtual machine, bind it to address 0.0.0.0. In ipv4, it is the address that can be reached via any other address on the local server. So when I enter localhost:9222 on the host machine, which corresponds to whatever ip the host has, it will still be heard by Chromium running in the virtual machine.

Pretty neat, I thought.

Now I wonder how a Rails server, with which I also played in Vagrant those couple of years ago, was easily accessible from the host without me explicitly binding it to 0.0.0.0. Was it doing so by itself? Another bit of Rails magic? I guess I am too lazy to go investigate.
Subscribe

  • (no subject)

    A beautiful cartoon on the front page of The Week:

  • (no subject)

    This is just as bad as a brief exchange with a belligerent guest on a regular news show: Six minutes! Six minutes, during which Rand Paul is…

  • (no subject)

    The war on repetitions:

  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments