Using webdriverio with headless Chrome in Docker
Tags: webdriverio, testing
Published: 2019-05-07 18:05:55

---
title: Using webdriverio with headless Chrome in Docker
published: true
description: Seeing random Chrome crashes like "tab crashed", or "Chrome is unreachable" errors when running your headless tests in Docker? Run Chrome with these options
tags: webdriverio, testing
canonical_url: https://www.intricatecloud.io/2019/05/running-webdriverio-tests-using-headless-chrome-inside-a-container/
cover_image: https://thepracticaldev.s3.amazonaws.com/i/62w8sex1vojo9tz8lh5k.jpg
---

Using headless chrome for your UI tests works great out of the box on your laptop, but it won’t work out of the box when you're trying to run your tests in Docker. One recent work project was getting webdriverio tests successfully running in a Docker container as part of a Jenkins pipeline. Locally, our tests worked fine, but when we ran them in docker, they became super flaky with errors showing random Chrome crashes, or “Chrome is unreachable” errors. Hope is not lost though - There is in fact a proper incantation of options to pass to Chrome to get it running successfully (and without random crashes).

**tl;dr** Here's a snippet of `wdio.conf.js` showing our Chrome configuration. To see why some of these need to be included, read on.

```
// wdio.conf.js
...
browserName: 'chrome',
chromeOptions: {
  args: [
    '--disable-infobars',
    '--window-size=1280,800',
  ].concat((function() {
    return process.env.HEADLESS_CHROME === '1' ? [
      '--headless',
      '--no-sandbox',
      '--disable-gpu',
      '--disable-setuid-sandbox',
      '--disable-dev-shm-usage'] : [];
  })()),
...
```


### -\-disable-dev-shm-usage

Add this flag if you're seeing the error [(like this guy did) “Session deleted because of page crash from tab crash”](https://github.com/elgalu/docker-selenium/issues/20) in your logs when your tests fail (or a message along the lines of a page crash), or Chrome is unreachable.

**Why do we need this?**

There’s a tmp folder that Chrome uses related to its internal memory management that by default tries to use the /dev/shm directory. I don't quite understand the internals of Chrome, but here's [some details about this from the puppeteer project](https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#tips). Anyway, Docker containers have a [default size of 64MB for /dev/shm (cmd+f for --shm-size)](https://docs.docker.com/engine/reference/run/). Chrome quickly runs out of space in there and your page will then crash, taking your tests down with it. No bueno. There’s two solutions to this:

The easiest way to deal with this problem is to just not use it. There are some small cons, but a good rule of thumb is that unless you're trying to parallelize multiple concurrent Chrome sessions within one container, you're probably safe to disable it by adding `--disable-dev-shm-usage` to your Chrome options.

If you are though ([like this engineer](https://github.com/GoogleChrome/puppeteer/issues/1834#issuecomment-381106859)), then you'll need to increase the size of your /dev/shm. For Docker containers,
- there’s a flag for increasing the shared memory size `--shm-size=1G` that you can pass to your docker run command.
- you can also mount /dev/shm from your host machine to the docker container via `-v /dev/shm:/dev/shm`

### -\-disable-setuid-sandbox --no-sandbox

If you see errors failing to connect to Chrome [like this](https://github.com/GoogleChrome/puppeteer/issues/370), you might need these flags.  Chrome uses sandboxing in order to protect your machine from random content on the internet ([see details here](https://chromium.googlesource.com/chromium/src/+/HEAD/docs/linux_sandboxing.md)). If you’re running Chrome inside of a *disposable* container, then your container is already acting as a sandbox for Chrome, and so you don’t quite *need* chrome’s sandbox (your mileage may vary here depending on what you’re trying to do). 

I typically see these two flags passed along together in various writeups online. [This answer from the Chromium team](https://groups.google.com/a/chromium.org/forum/m/#!topic/chromium-discuss/kfhzPq_Al94) suggests that `--disable-setuid-sandbox` was only used in older versions of chrome.

### -\-disable-gpu

The official Puppeteer docs suggest that this flag is only needed on Windows. Various snippets online usually use `--disable-gpu` alongside `--headless` because an earlier version of headless chrome had bugs with it.

### -\-disable-infobars

This flag can help a minor annoyance - if you take a screenshot of the browser in your tests, then you’ll see a bar at the top of your browser page saying that Chrome is being controlled by automated process. This also pushes elements farther down the page. Its usually fine, but if it gets in your way, then try using this option. This flag was at one point removed around [Jan 2018](https://github.com/GoogleChrome/puppeteer/issues/1765#issuecomment-356761402), and then [added back](https://bugs.chromium.org/p/chromium/issues/detail?id=820453#c6). 

## Wrapping up

Yes, Chrome can run headlessly and it can run inside of a container, and be stable (for the most part). You do however need the right concoction of parameters to get it working right, but hopefully this saves you a few hours of debugging random page crashes. Try out those Chrome options in your configuration (they will also work if you're using puppeteer or other selenium/chrome combo). 

Running into different errors? See [how to attach the VSCode debugger to step through your UI tests](https://www.intricatecloud.io/2019/03/debugging-webdriverio-tests-with-vscode/) to help you narrow down the issue.