Andrey (azangru) wrote,

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 (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 of the host, not of the guest, where Chromium is running.

The solution? When starting Chromium in the virtual machine, bind it to address 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 Was it doing so by itself? Another bit of Rails magic? I guess I am too lazy to go investigate.

  • (no subject)

    This was a good talk. Interesting to see that SvelteKit is taking the same direction as, by using html forms to submit data without the…

  • (no subject)

    A talk about building large-scale (React) apps. These days, the questions that concern me are: - how can a project be scaled up without…

  • (no subject)

    Watched the second episode of The Problem with Jon Stewart. I thought of Jon as an intellectual comedian. Someone who transcends the limitations of…

  • Post a new comment


    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.