Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Docker image that strips away .NET sdk and leaves only runtime? #2739

Open
rho-cassiopeiae opened this issue Oct 31, 2023 · 1 comment

Comments

@rho-cassiopeiae
Copy link

rho-cassiopeiae commented Oct 31, 2023

I understand that in order to build the install script we need the sdk, and that the main use case for Playwright is testing, which also requires it, but for my use case (making a webserver that takes screenshots of pages) I only need the runtime. Currently I have these two images:

playwright:0.1.0

FROM mcr.microsoft.com/dotnet/sdk:7.0-jammy AS base
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
RUN mkdir /ms-playwright && \
    mkdir /ms-playwright-agent && \
    cd /ms-playwright-agent && \
    dotnet new console && \
    echo '<?xml version="1.0" encoding="utf-8"?><configuration><packageSources><add key="local" value="/tmp/"/></packageSources></configuration>' > nuget.config && \
    dotnet add package Microsoft.Playwright --version 1.39.0 && \
    dotnet build && \
    ./bin/Debug/net7.0/playwright.ps1 install chromium --with-deps && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /tmp/* && \
    rm -rf /ms-playwright-agent && \
    chmod -R 777 /ms-playwright

COPY ublock/ /ublock/ # these are unpacked browser extensions, not relevant for the question
COPY isdcac/ /isdcac/
FROM playwright:0.1.0 AS base

FROM mcr.microsoft.com/dotnet/sdk:7.0-jammy AS build
WORKDIR /src
COPY ["./Screenshot.csproj", "Screenshot.csproj"]
RUN dotnet restore
COPY . .
RUN dotnet build --no-restore -c Release -o /app/build

FROM build AS publish
RUN dotnet publish --no-restore -c Release -o /app/publish

FROM base AS final
WORKDIR /app
RUN mkdir /screenshots
EXPOSE 5000
ENV ASPNETCORE_URLS=http://+:5000
ENV ASPNETCORE_ENVIRONMENT=Development
COPY --from=publish /app/publish .
ENTRYPOINT ["xvfb-run", "dotnet", "Screenshot.dll"]

This works fine, but the final image is >2Gb, which is not ideal. I tried changing the base image like this:

FROM mcr.microsoft.com/dotnet/sdk:7.0-jammy AS base
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
RUN mkdir /ms-playwright && \
    mkdir /ms-playwright-agent && \
    cd /ms-playwright-agent && \
    dotnet new console && \
    echo '<?xml version="1.0" encoding="utf-8"?><configuration><packageSources><add key="local" value="/tmp/"/></packageSources></configuration>' > nuget.config && \
    dotnet add package Microsoft.Playwright --version 1.39.0 && \
    dotnet build && \
    ./bin/Debug/net7.0/playwright.ps1 install chromium --with-deps && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /tmp/* && \
    rm -rf /ms-playwright-agent

FROM mcr.microsoft.com/dotnet/aspnet:7.0-jammy AS final
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
COPY --from=base /ms-playwright /ms-playwright
RUN chmod -R 777 /ms-playwright
COPY ublock/ /ublock/
COPY isdcac/ /isdcac/

But then starting the final image errors with [FATAL tini (7)] exec xvfb-run failed: No such file or directory, so I guess simply copying /ms-playwright is not enough. Any help?

EDIT: Also tried copying the install script from the sdk image to the runtime one and running it there, but no luck:

FROM mcr.microsoft.com/dotnet/sdk:7.0-jammy AS base
RUN mkdir /ms-playwright-agent && \
    cd /ms-playwright-agent && \
    dotnet new console && \
    echo '<?xml version="1.0" encoding="utf-8"?><configuration><packageSources><add key="local" value="/tmp/"/></packageSources></configuration>' > nuget.config && \
    dotnet add package Microsoft.Playwright --version 1.39.0 && \
    dotnet build

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS final
# Need to install powershell
RUN apt-get update -y
RUN apt-get install -y wget
RUN wget https://github.com/PowerShell/PowerShell/releases/download/v7.3.9/powershell_7.3.9-1.deb_amd64.deb
RUN dpkg -i powershell_7.3.9-1.deb_amd64.deb
RUN apt-get install -f -y
RUN rm powershell_7.3.9-1.deb_amd64.deb

ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
COPY --from=base /ms-playwright-agent/bin/Debug/net7.0/playwright.ps1 /ms-playwright-agent/playwright.ps1
RUN mkdir /ms-playwright && \
    /ms-playwright-agent/playwright.ps1 install chromium --with-deps && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /tmp/* && \
    rm -rf /ms-playwright-agent && \
    chmod -R 777 /ms-playwright

COPY ublock/ /ublock/
COPY isdcac/ /isdcac/

The image builds with no errors but it seems like the install script doesn't really run, because there's no browser install progress bar like usual, and running the final image gives the same error about xvfb.

@mu88
Copy link

mu88 commented Nov 28, 2023

Your idea sounds very similar to my little project, @rho-cassiopeiae - maybe this Dockerfile can help you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants