How can I run selenium chrome driver in a docker container? : <h1>tl;dr</h1>

<p>How can I install all the components to run Selenium in a docker container?</p>

<hr>

<h1>Question</h1>

<p>I'm starting with this image:</p>

<pre><code>FROM microsoft/aspnetcore-build:2 AS builder
WORKDIR /source

COPY . .
RUN dotnet restore
RUN dotnet build
ENTRYPOINT ["dotnet", "run"]
</code></pre>

<p>How can I make it so that I can start and use an headless Chrome Driver with this:</p>

<pre><code>ChromeOptions options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--disable-gpu");
var driverPath = Path.GetFullPath(Path.Combine(environment.ContentRootPath, "bin/Debug/netcoreapp2.0"));
return new ChromeDriver(driverPath, options, TimeSpan.FromSeconds(60));
</code></pre>

<p>within a docker container?</p>

<hr>

<h1>What have I tried</h1>

<h3>Installing the Chrome driver</h3>

<p>The <code>chromedriver</code> is distributed via the <code>Selenium.WebDriver.ChromeDriver</code> NuGet package. </p>

<h3>Installing Chrome</h3>

<p>On my Mac OS X with Google Chrome installed the current setup works just fine.</p>

<p>I've tried to add these lines:</p>

<pre><code>RUN apt-get update &amp;&amp; apt-get -y install libglib2.0-dev libxi6 libnss3-dev
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" &gt;&gt; /etc/apt/sources.list.d/google.list'
RUN apt-get update &amp;&amp; apt-get -y install google-chrome-stable
</code></pre>

<p>The above installs this version of Chrome:</p>

<pre><code>google-chrome-stable:
  Installed: 64.0.3282.119-1
  Candidate: 64.0.3282.119-1
  Version table:
 *** 64.0.3282.119-1 500
        500 http://dl.google.com/linux/chrome/deb stable/main amd64 Packages
        100 /var/lib/dpkg/status
</code></pre>

<p>which is compatible with the version of the Chrome Driver.</p>

<p>which come from trying to solve each error that came out by trying to run Selenium with the docker container.</p>

<p>If I run this setup I get:</p>

<blockquote>
  <p>Starting ChromeDriver 2.35.528139
  (47ead77cb35ad2a9a83248b292151462a66cd881) on port 57889 Only local
  connections are allowed. An error occurred while sending the request.
  Couldn't connect to</p>
</blockquote>

<p>when running the container.</p>

<h3>Debugging in the container</h3>

<p>If I enter the container manually and try to run the chrome driver manually I get:</p>

<blockquote>
  <p>Starting ChromeDriver 2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881) on port 9515
  Only local connections are allowed.</p>
</blockquote>

<p>and running <code>curl -i http://localhost:9515/status</code> I get:</p>

<pre><code>HTTP/1.1 200 OK
Content-Length:136
Content-Type:application/json; charset=utf-8
Connection:close

{"sessionId":"","status":0,"value":{"build":{"version":"alpha"},"os":{"arch":"x86_64","name":"Linux","version":"4.9.60-linuxkit-aufs"}}}
</code></pre>

<p>so it seems that the driver works just fine.</p>

<p>If I run chrome headless instead via <code>google-chrome-stable --headless --disable-cpu --no-sandbox</code> I get:</p>

<pre><code>[0125/210641.877388:WARNING:discardable_shared_memory_manager.cc(178)] Less than 64MB of free space in temporary directory for shared memory files: 63
[0125/210641.902689:ERROR:instance.cc(49)] Unable to locate service manifest for metrics
[0125/210641.902756:ERROR:service_manager.cc(890)] Failed to resolve service name: metrics
[0125/210642.031088:ERROR:instance.cc(49)] Unable to locate service manifest for metrics
[0125/210642.031119:ERROR:service_manager.cc(890)] Failed to resolve service name: metrics
[0125/210642.032934:ERROR:gpu_process_transport_factory.cc(1009)] Lost UI shared context.
</code></pre>

<p>The first warning can be solved via setting a docker volume in <code>/dev/shm:/dev/shm</code> or by setting <code>-shm-size</code> to something large (higher than 64MB).</p>

<p>The rest of the errors, if google, lead to me many bug reports from Google Chrome repositories.</p>
