How to run dotnet core app with Selenium in Docker

Since the appearance in dotnet core of self-contained applications I think a better approach is to use the official selenium docker: https://hub.docker.com/r/selenium/standalone-chrome and build the application self contained. Here is my dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build-env
WORKDIR /app

COPY . ./
RUN dotnet publish MyApp.csproj -c Release -o out --self-contained -r linux-x64 /p:PublishTrimmed=true

FROM selenium/standalone-chrome
WORKDIR /app

COPY --from=build-env /app/out .

ENTRYPOINT ["./MyApp"]

So I recently had the same problem.

TL;DR; You have to install chrome into the docker image by putting the commands in the Docker file.

 FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch

 # Install Chrome
 RUN apt-get update && apt-get install -y \
 apt-transport-https \
 ca-certificates \
 curl \
 gnupg \
 hicolor-icon-theme \
 libcanberra-gtk* \
 libgl1-mesa-dri \
 libgl1-mesa-glx \
 libpango1.0-0 \
 libpulse0 \
 libv4l-0 \
 fonts-symbola \
 --no-install-recommends \
 && curl -sSL https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
 && echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list \
 && apt-get update && apt-get install -y \
 google-chrome-stable \
 --no-install-recommends \
 && apt-get purge --auto-remove -y curl \
 && rm -rf /var/lib/apt/lists/*

 # Add your dotnet core project build stuff here

Easier solution - I pushed this as a docker image in my docker hub repo so you can use it as your base image. See this example of my dotnet core 2.2

 FROM masteroleary/selenium-dotnetcore2.2-linux:v2 AS base

 WORKDIR /app

 EXPOSE 80

 EXPOSE 443

 FROM masteroleary/selenium-dotnetcore2.2-linux:v2 AS build WORKDIR /src

 COPY ["MyProject.csproj", ""]

 RUN dotnet restore "MyProject.csproj"

 COPY . .

 WORKDIR "/src/"

 RUN dotnet build "MyProject.csproj" -c Prod -o /app

 FROM build AS publish

 RUN dotnet publish "MyProject.csproj" -c Prod -o /app

 FROM base AS final

 WORKDIR /app

 COPY --from=publish /app .

 ENTRYPOINT ["dotnet", "MyProject.dll"]

How did this happen?

Basically created a new project in visual studio for dotnet core 2.2 mvc with docker support.

Intentions are to run my dotnet core app in a linux container

Assumed that by installing nuget packages Selenium.Support, Selenium.WebDriver, Selenium.WebDriver.ChromeDriver anything I needed would be included in the docker container automatically since Selenium.WebDriver supports .NetStandard 2.0 (BTW the others don't, just realized that)

Turns out you have to install chrome into the docker image by putting the commands in the Docker file.

I've explained the whole learning process here including how I found this working code: https://hub.docker.com/r/masteroleary/selenium-dotnetcore2.2-linux