webdriver container in circleci for testing
Question: **Problem**

I have a [CircleCI](https://circleci.com) continuous integration for my django
application. I would like to use a [standalone
chrome](https://github.com/SeleniumHQ/docker-
selenium/tree/master/StandaloneChrome) selenium node container to run my UI
tests. The following setup works locally:

  * **Launch django server in the background:**
    
        python manage.py migrate && python manage.py runserver 0.0.0.0:8081 &
    

  * **Run the webdriver container:**
    
        docker run --net='host' --name selenium -d -p 4444:4444 selenium/standalone-chrome
    

  * **Run test:**
    
        from fabric.operations import local
    from selenium import webdriver
    @given('I have a web browser')
    def browser():
        return webdriver.Remote(
            command_executor='http://127.0.0.1:4444/wd/hub',
            desired_capabilities=DesiredCapabilities.CHROME)
    
    
    @when('I open the main page')
    def view_main(browser):
        assert "backend.fail" in local("curl http://localhost:8081/", capture=True)
        browser.maximize_window()
        browser.get("http://localhost:8081/")
        return browser
    

`curl http://localhost:8081/` is run in the context of the CircleCI shell, and
succeeds, whereas `browser.get("http://localhost:8081/")` is run from the
docker container running webdriver and fails.

**Question**

How can I make my docker container see my django server on `localhost:8081` on
CircleCI?

**Research**

I read in the docker
[documentation](https://docs.docker.com/articles/networking/#container-
networking) that `--net=host` puts the host and docker client on the same
network stack, and it works locally in a vagrant vm.

I have looked at [this
question](http://stackoverflow.com/questions/25512772/setting-up-multiple-
docker-containers-and-mongodb-to-run-in-circleci) which explores communication
between multiple docker containers and [this
question](http://stackoverflow.com/questions/31213195/running-docker-
container-tests-on-circleci) which handles a general setup of docker tests on
CircleCI, but none address the visibility of the host from a docker container.


Answer: **Answer:**

After some digging and using CircleCI's excellent `ssh` debugging tool, it
turned out that the reason for the failure was not `--net='host'` but rather
the selenium container failing to start.

It silently failed because

    
    
    unable to start xvfb
    unable to start xvfb
    unable to start xvfb
    unable to start xvfb
    

Then I googled _"selenium circleci"_ and it turned out that CircleCI has a
native `xfvb` running on `:99` and `chromedriver`
[preinstalled](https://circleci.com/docs/environment#browsers):

> CircleCI runs graphical programs in a virtual framebuffer, using **xvfb**.
> This means programs like **Selenium** , Capybara, Jasmine, and other testing
> tools which require a browser **will work perfectly** , just like they do
> when you use them locally.

I changed my test to:

    
    
    @given('I have a web browser')
    def browser():
        try:
            return webdriver.Remote(
            command_executor='http://127.0.0.1:4444/wd/hub',
            desired_capabilities=DesiredCapabilities.CHROME)
        except:
            return webdriver.Chrome()
    

And now my tests pass.

