I created a .Net application using Selenium. Everything is working fine locally, but when using my application in a Docker container I’m always receiving the following exception
System.InvalidOperationException: session not created: Chrome failed to start: exited normally.
(chrome not reachable)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) (SessionNotCreated)
at OpenQA.Selenium.WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute)
at OpenQA.Selenium.WebDriver.ExecuteAsync(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.WebDriver.StartSession(ICapabilities capabilities)
at OpenQA.Selenium.WebDriver..ctor(ICommandExecutor executor, ICapabilities capabilities)
at OpenQA.Selenium.Chromium.ChromiumDriver..ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
I’m using the following arguments when creating the Chrome driver in my .Net application
var chromeOptions = new ChromeOptions();
if (OperatingSystem.IsLinux())
{
chromeOptions.BinaryLocation = "/usr/bin/google-chrome"; // Set the correct path to the Chrome binary
}
chromeOptions.AddArgument("--disable-dev-shm-usage");
chromeOptions.AddArgument("--no-sandbox");
chromeOptions.AddArgument("--disable-search-engine-choice-screen");
_webDriver = new ChromeDriver(chromeOptions);
I’m using the Selenium Chrome image, whereas it contains my .Net application which is published as a standalone.
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM selenium/standalone-chrome:latest AS base
USER app
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["MyApp.csproj", "MyApp/"]
RUN dotnet restore "./MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "./MyApp.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./MyApp.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=true --self-contained true -r linux-x64
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["./MyApp"]
EXPOSE 5000
I’m having this issue both on the cloud I’m trying to run my container in, as local.
By adding the following, it got solved
chromeOptions.AddArgument("--remote-debugging-port=9222");