Download chromium during runtime in docker (like windows)

This commit is contained in:
whimsical-c4lic0 2025-12-13 02:07:56 -06:00
parent a4d8676f2e
commit 0572844ca8
5 changed files with 34 additions and 17 deletions

View File

@ -32,7 +32,7 @@ RUN apt-get update \
npm \ npm \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
RUN npx playwright install --with-deps chromium RUN npx playwright install-deps
RUN apt-get remove --purge -y npm \ RUN apt-get remove --purge -y npm \
&& apt-get autoremove -y && apt-get autoremove -y
@ -54,10 +54,12 @@ COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY docker/entrypoint.sh /app/entrypoint.sh COPY docker/entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh RUN chmod +x /app/entrypoint.sh
ENV DISPLAY=:0.0 \ ENV DEBIAN_FRONTEND="noninteractive" \
DISPLAY_WIDTH=1024 \ DISPLAY=:0.0 \
DISPLAY_WIDTH=1366 \
DISPLAY_HEIGHT=768 \ DISPLAY_HEIGHT=768 \
OFDL_DOCKER=true OFDL_DOCKER=true \
PLAYWRIGHT_BROWSERS_PATH=/config/chromium
EXPOSE 8080 EXPOSE 8080
WORKDIR /config WORKDIR /config

View File

@ -26,21 +26,32 @@ public class AuthHelper
private const float LoginTimeout = 600000f; // 10 minutes private const float LoginTimeout = 600000f; // 10 minutes
private const float FeedLoadTimeout = 60000f; // 1 minute private const float FeedLoadTimeout = 60000f; // 1 minute
public async Task SetupBrowser(bool runningInDocker) public Task SetupBrowser(bool runningInDocker)
{ {
if (runningInDocker) if (runningInDocker)
{ {
Log.Information("Running in Docker. Disabling sandbox and GPU."); Log.Information("Running in Docker. Disabling sandbox and GPU.");
_options.Args = ["--no-sandbox", "--disable-setuid-sandbox", "--disable-gpu", "--disable-blink-features=AutomationControlled", "--disable-infobars"]; _options.Args = ["--no-sandbox", "--disable-setuid-sandbox", "--disable-gpu", "--disable-blink-features=AutomationControlled", "--disable-infobars"];
}
else // If chromium is already downloaded, skip installation
{ string? playwrightBrowsersPath = Environment.GetEnvironmentVariable("PLAYWRIGHT_BROWSERS_PATH");
var exitCode = Microsoft.Playwright.Program.Main(new[] {"install", "--with-deps", "chromium"}); IEnumerable<string> folders = Directory.GetDirectories(playwrightBrowsersPath ?? "/config/chromium")
if (exitCode != 0) .Where(folder => folder.Contains("chromium-"));
if (folders.Any())
{ {
throw new Exception($"Playwright chromium download failed. Exited with code {exitCode}"); Log.Information("chromium already downloaded. Skipping install step.");
return Task.CompletedTask;
} }
} }
var exitCode = Microsoft.Playwright.Program.Main(["install", "--with-deps", "chromium"]);
if (exitCode != 0)
{
throw new Exception($"Playwright chromium download failed. Exited with code {exitCode}");
}
return Task.CompletedTask;
} }
private async Task<string> GetBcToken(IPage page) private async Task<string> GetBcToken(IPage page)
@ -60,9 +71,12 @@ public class AuthHelper
} }
catch (Exception e) catch (Exception e)
{ {
if (e.Message.Contains("An error occurred trying to start process") && Directory.Exists(_userDataDir)) if ((
e.Message.Contains("An error occurred trying to start process") ||
e.Message.Contains("The profile appears to be in use by another Chromium process")
) && Directory.Exists(_userDataDir))
{ {
Log.Error("Failed to launch browser. Deleting chrome-data directory and trying again."); Log.Error("Failed to launch browser. Deleting chromium-data directory and trying again.");
Directory.Delete(_userDataDir, true); Directory.Delete(_userDataDir, true);
IPlaywright playwright = await Playwright.CreateAsync(); IPlaywright playwright = await Playwright.CreateAsync();
browser = await playwright.Chromium.LaunchPersistentContextAsync(_userDataDir, _options); browser = await playwright.Chromium.LaunchPersistentContextAsync(_userDataDir, _options);

View File

@ -18,7 +18,7 @@
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" /> <PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" />
<PackageReference Include="HtmlAgilityPack" Version="1.12.0" /> <PackageReference Include="HtmlAgilityPack" Version="1.12.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.3" /> <PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.3" />
<PackageReference Include="Microsoft.Playwright" Version="1.54.0" /> <PackageReference Include="Microsoft.Playwright" Version="1.55.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="protobuf-net" Version="3.2.46" /> <PackageReference Include="protobuf-net" Version="3.2.46" />
<PackageReference Include="Serilog" Version="4.2.0" /> <PackageReference Include="Serilog" Version="4.2.0" />

View File

@ -3142,10 +3142,10 @@ public class Program
} }
break; break;
case "[red]Logout and Exit[/]": case "[red]Logout and Exit[/]":
if (Directory.Exists("chrome-data")) if (Directory.Exists("chromium-data"))
{ {
Log.Information("Deleting chrome-data folder"); Log.Information("Deleting chromium-data folder");
Directory.Delete("chrome-data", true); Directory.Delete("chromium-data", true);
} }
if (File.Exists("auth.json")) if (File.Exists("auth.json"))
{ {

View File

@ -2,6 +2,7 @@
mkdir -p /config/cdm/devices/chrome_1610 mkdir -p /config/cdm/devices/chrome_1610
mkdir -p /config/logs/ mkdir -p /config/logs/
mkdir -p /config/chromium
if [ ! -f /config/config.conf ] && [ ! -f /config/config.json ]; then if [ ! -f /config/config.conf ] && [ ! -f /config/config.json ]; then
cp /default-config/config.conf /config/config.conf cp /default-config/config.conf /config/config.conf