Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run dotnet core app with Selenium in Docker

I have dotnet core 2.2 (aspnet core) app running in Docker container. I'm using the simplest possible Dockerfile you can find in any basic tutorial:

  • use microsoft/dotnet:2.2-sdk as base image
  • copy *.csproj
  • restore packages
  • build
  • publish to /app folder
  • use microsoft/dotnet:2.2.1-aspnetcore-runtime to run the app from /app folder

Now I'd like to grab some data from another website. It is a SPA, so I need to use a browser to render the page first - I decided to use Selenium with ChromeDriver because I'm already a little bit familiar with them.

I've added Selenium.WebDriver v3.141 and Selenium.WebDriver.ChromeDriver v73.0 to my project, set Selenium there. Locally on Windows it works fine. But when I run this via Docker I'm getting:

The file /app/chromedriver does not exist. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html

So now I'm wondering how can I run Selenium + single instance Chrome (there is no need to set up Selenium Grid for my purpose) with dotnet core 2.2 in Docker.

I suppose I need to create custom Dockerfile which:

  • installs selenium, chrome and all their dependencies
  • installs dotnet
  • does the same as my current Dockerfile to build and run my app

But I'm not really sure how to do this. Especially how to "nest" Dockerfiles. Should I do this composition in a single Dockerfile? Should I create Dockerfile for Selenium + ChromeDriver and use it as base image for next step?

like image 526
Illia Ratkevych Avatar asked Mar 17 '19 10:03

Illia Ratkevych


People also ask

Can you run selenium in Docker?

To run containerized selenium tests, installation of Docker is required. Follow the below steps to install and setup Docker on Windows. Download the Installer: Docker provides an installer for Windows which can be downloaded from the official docker website.


2 Answers

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

like image 176
masteroleary Avatar answered Sep 29 '22 04:09

masteroleary


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"]
like image 41
Cristian T Avatar answered Sep 29 '22 04:09

Cristian T