[英]How to run dotnet core app with Selenium in Docker
我在 Docker 容器中運行了 dotnet core 2.2 (aspnet core) 應用程序。 我正在使用最簡單的 Dockerfile 您可以在任何基本教程中找到:
microsoft/dotnet:2.2-sdk
作為基礎鏡像microsoft/dotnet:2.2.1-aspnetcore-runtime
從 /app 文件夾運行應用程序現在我想從另一個網站獲取一些數據。 這是一個 SPA,所以我需要先使用瀏覽器來渲染頁面——我決定將 Selenium 與 ChromeDriver 一起使用,因為我已經對它們有點熟悉了。
我已將Selenium.WebDriver v3.141
和Selenium.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
所以現在我想知道如何在 ZC5FD214CDD0D2B3B4272E73B022 中使用 dotnet core 2.2 運行 Selenium + 單實例 Chrome(無需為我的目的設置 Selenium 網格)。
我想我需要創建自定義 Dockerfile :
dotnet
但我不確定如何做到這一點。 尤其是如何“嵌套”Dockerfile。 我應該在一個 Dockerfile 中做這個組合嗎? 我應該為 Selenium + ChromeDriver 創建 Dockerfile 並將其用作下一步的基本映像嗎?
所以我最近遇到了同樣的問題。
TL;博士; 您必須通過將命令放入 Docker 文件中來將 chrome 安裝到 docker 映像中。
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
更簡單的解決方案 - 我將其作為 docker 集線器存儲庫中的 docker 圖像推送,因此您可以將其用作基本圖像。 請參閱我的 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"]
這怎么發生的?
基本上在 Visual Studio 中為 dotnet core 2.2 mvc 創建了一個新項目,支持 docker。
意圖是在 linux 容器中運行我的 dotnet 核心應用程序
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)
事實證明,您必須通過將命令放入 Docker 文件中來將 chrome 安裝到 docker 映像中。
我在這里解釋了整個學習過程,包括我如何找到這個工作代碼: https://hub.docker.com/r/masteroleary/selenium-dotnetcore2.2-linux
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. 這是我的 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"]
這也是dotnet 6.0的更新版本 這是一個多階段 Docker 文件,有助於更快地構建
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base WORKDIR /app RUN apt update && apt 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/* FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build /* The .net App Docker Configuration */ FROM build AS publish RUN dotnet publish -c Release -o /app FROM base AS final WORKDIR /app COPY --from=publish /app. RUN echo ls -a ENTRYPOINT ["dotnet", "{Entry Point File Name}.dll"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.