gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:19:50 26 4
gpt4 key购买 nike

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

  • 使用 microsoft/dotnet:2.2-sdk 作为基础镜像
  • 复制*.csproj
  • 恢复包
  • build
  • 发布到/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

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

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

  • 安装 selenium、chrome 及其所有依赖项
  • 安装dotnet
  • 使用与我当前的 Dockerfile 相同的方式来构建和运行我的应用

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

最佳答案

所以我最近遇到了同样的问题。

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?

基本上在 visual studio 中为支持 docker 的 dotnet core 2.2 mvc 创建了一个新项目。

意图是在 linux 容器中运行我的 dotnet 核心应用

假设通过安装 nuget 包 Selenium.Support、Selenium.WebDriver、Selenium.WebDriver.ChromeDriver,我需要的任何东西都会自动包含在 docker 容器中,因为 Selenium.WebDriver 支持 .NetStandard 2.0(顺便说一句,其他人不支持,只是意识到)

事实证明,您必须通过将命令放入 Docker 文件来将 chrome 安装到 docker 镜像中。

我在这里解释了整个学习过程,包括我如何找到这个工作代码:https://hub.docker.com/r/masteroleary/selenium-dotnetcore2.2-linux

关于c# - 如何在 Docker 中使用 Selenium 运行 dotnet 核心应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55206172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com