如何在 Docker 中使用 Selenium 运行 dotnet 核心应用程序

Ill*_*ych 8 c# selenium selenium-chromedriver docker .net-core

我在 Docker 容器中运行了 dotnet core 2.2(aspnet core)应用程序。我正在使用您可以在任何基本教程中找到的最简单的 Dockerfile:

  • 使用microsoft/dotnet:2.2-sdk作为基本图像
  • 复制 *.csproj
  • 恢复包
  • 建造
  • 发布到 /app 文件夹
  • 用于microsoft/dotnet:2.2.1-aspnetcore-runtime从 /app 文件夹运行应用程序

现在我想从另一个网站获取一些数据。它是一个 SPA,所以我需要先使用浏览器来呈现页面 - 我决定将 Selenium 与 ChromeDriver 一起使用,因为我已经对它们有点熟悉了。

我已经添加Selenium.WebDriver v3.141Selenium.WebDriver.ChromeDriver v73.0在我的项目中设置了 Selenium。在本地 Windows 上它工作正常。但是当我通过 Docker 运行它时,我得到:

The file /app/chromedriver does not exist. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html
Run Code Online (Sandbox Code Playgroud)

所以现在我想知道如何在 Docker 中使用 dotnet core 2.2 运行 Selenium + 单实例 Chrome(不需要为我的目的设置 Selenium Grid)。

我想我需要创建自定义 Dockerfile ,它:

  • 安装 selenium、chrome 及其所有依赖项
  • 安装 dotnet
  • 与我当前的 Dockerfile 一样构建和运行我的应用程序

但我不确定如何做到这一点。特别是如何“嵌套”Dockerfiles。我应该在一个 Dockerfile 中完成这个组合吗?我应该为 Selenium + ChromeDriver 创建 Dockerfile 并将其用作下一步的基本映像吗?

mas*_*ary 6

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
Run Code Online (Sandbox Code Playgroud)

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"]
Run Code Online (Sandbox Code Playgroud)

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


Cri*_*n T 6

由于自包含应用程序出现在 dotnet core 中,我认为更好的方法是使用官方 selenium docker: https: //hub.docker.com/r/selenium/standalone-chrome 并构建自包含应用程序。这是我的 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"]
Run Code Online (Sandbox Code Playgroud)