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

[Question]: Correct way to install browsers in dockerfile with playwright.ps1 #2058

Closed
braidenstiller opened this issue Mar 10, 2022 · 10 comments

Comments

@braidenstiller
Copy link

braidenstiller commented Mar 10, 2022

Your question

Hi, I have recently upgraded playwright to v1.19.1 and noticed the playwright cli tool has been replaced with the playwright.ps1 script. I setup, build and run my runtime through a dockerfile based on the auto-generated .net dockerfile template and I have added:

RUN pwsh /app/build/playwright.ps1 install chrome.

Full dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
RUN apt-get update -yq \
    && apt-get install wget -yq \
    && wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb \
    && dpkg -i packages-microsoft-prod.deb \
    && apt-get update -yq \
    && apt-get install powershell -yq

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
RUN apt-get update -yq \
    && apt-get install curl gnupg -yq \
    && curl -sL https://deb.nodesource.com/setup_16.x | bash \
    && apt-get install nodejs -yq \
    && npm install -g npm
WORKDIR /src
COPY ["src/Web/Server/Web.Server.csproj", "src/Web/Server/"]
RUN dotnet restore "src/Web/Server/Web.Server.csproj"
COPY . .
WORKDIR "/src/src/Web/Server"
RUN dotnet build "Web.Server.csproj" --no-restore -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Web.Server.csproj" --no-restore --verbosity normal -c Release -o /app/publish
COPY --from=build /app/build/playwright.ps1 /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
RUN pwsh playwright.ps1 install chrome
RUN rm -rf playwright.ps1
ENTRYPOINT ["dotnet", "Web.Server.dll"]

This successfully executes but when I run my container I still recieving the following error from playwright:

Executable doesn't exist at /root/.cache/ms-playwright/chromium-965416/chrome-linux/chrome
Looks like Playwright Test or Playwright was just installed or updated. Please run the following command to download new browsers:             pwsh bin\\Debug\\netX\\playwright.ps1 install

What is the correct way to install/run the install script in a dockerfile? I understand playwright has a base image but I would prefer to install the dependencies into my docker image instead.

@braidenstiller
Copy link
Author

Turns out I was installing the wrong thing.... it should be chromium, not chrome.
Final dockerfile incase anyone finds this useful, uncessarry bits removed:

FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
#Install powershell to playwright scripts
RUN apt-get update -yq \
    && apt-get install wget -yq \
    && wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb \
    && dpkg -i packages-microsoft-prod.deb \
    && apt-get update -yq \
    && apt-get install powershell -yq

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
# Application
COPY ["src/Web/Server/Web.Server.csproj", "src/Web/Server/"]
RUN dotnet restore "src/Web/Server/Web.Server.csproj"
COPY . .
RUN dotnet build "src/Web/Server/Web.Server.csproj" --no-restore -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "src/Web/Server/Web.Server.csproj" --no-restore --verbosity normal -c Release -o /app/publish
COPY --from=build /app/build/playwright.ps1 /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
# Install playwright dependencies and cleanup
RUN pwsh playwright.ps1 install chromium
RUN pwsh playwright.ps1 install-deps chromium
RUN rm -rf playwright.ps1
RUN apt remove wget powershell -yq

ENTRYPOINT ["dotnet", "Web.Server.dll"]

@ianrathbone
Copy link

@braidenstiller thank you for this, I've been struggling to get a working docker image and this has really helped me! 🥇

@aaronamm
Copy link

aaronamm commented Jun 3, 2022

@braidenstiller thanks for sharing such a useful script.

@braidenstiller
Copy link
Author

@ianrathbone @aaronamm Hey, glad I could help!
To add to the above, I ended up replacing the powershell portion with NodeJS.

I haven't tested this exact dockerfile below but I've copied the NodeJS parts in ad-hoc. In my testing, I found it shaved a few minutes off my build pipeline, with the added benefit of a slightly nicer looking dockerfile.

Enjoy!

FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# Install NodeJS to run playwright installer
RUN apt-get update -yq \
    && apt-get install curl gnupg -yq \
    && curl -sL https://deb.nodesource.com/setup_16.x | bash \
    && apt-get install nodejs -yq \
    && npm install -g npm
# Install browsers and depdencies for playwright
RUN npx playwright@1.20.0 install chromium \
    && npx playwright@1.20.0 install-deps chromium
#Uninstall NodeJS
RUN apt remove nodejs curl gnupg -yq \
    rm -fv /etc/apt/sources.list.d/nodesource*

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
# Application
COPY ["src/Web/Server/Web.Server.csproj", "src/Web/Server/"]
RUN dotnet restore "src/Web/Server/Web.Server.csproj"
COPY . .
RUN dotnet build "src/Web/Server/Web.Server.csproj" --no-restore -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "src/Web/Server/Web.Server.csproj" --no-restore --verbosity normal -c Release -o /app/publish
COPY --from=build /app/build/playwright.ps1 /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Web.Server.dll"]

@EgoPingvina
Copy link

@braidenstiller could you help me, please? I'm a newbie in docker.

  1. What is -focal and what's the difference with -alpine? I tried to use it mcr.microsoft.com/dotnet/aspnet:7-focal but see not found.
  2. With your example it returns me Package ttf-ubuntu-font-family is not available, but is referred to by another package.. Can you say how can I fix it? I found this, add RUN echo "deb http://ftp.us.debian.org/debian buster main non-free" >> /etc/apt/sources.list.d/fonts.list, but it does not work too.

my dockerfile

# Adjust DOTNET_OS_VERSION as desired
ARG DOTNET_OS_VERSION="-alpine"
ARG DOTNET_SDK_VERSION=7.0

FROM mcr.microsoft.com/dotnet/aspnet:${DOTNET_SDK_VERSION} as base
WORKDIR /publish
ENV ASPNETCORE_URLS http://+:5100
ENV ASPNETCORE_ENVIRONMENT Production
EXPOSE 5100

# Install NodeJS to run playwright installer
RUN apt-get update -yq \
    && apt-get install curl gnupg -yq \
    && curl -sL https://deb.nodesource.com/setup_16.x | bash \
    && apt-get install nodejs -yq \
    && npm install -g npm
RUN echo "deb http://ftp.us.debian.org/debian buster main non-free" >> /etc/apt/sources.list.d/fonts.list
# Install browsers and depdencies for playwright
RUN npx playwright@1.20.0 install chromium \
    && npx playwright@1.20.0 install-deps chromium
#Uninstall NodeJS
RUN apt remove nodejs curl gnupg -yq \
    rm -fv /etc/apt/sources.list.d/nodesource*

FROM mcr.microsoft.com/dotnet/sdk:${DOTNET_SDK_VERSION}${DOTNET_OS_VERSION} AS build
WORKDIR /src
# application
COPY ["src/Layers/Models/Models.csproj", "Layers/Models/"]
COPY ["src/Layers/Domain/Domain.csproj", "Layers/Domain/"]
COPY ["src/TestProject/TestProject.csproj", "TestProject/"]
# restore all projects as a separate step to speed up container reassembly
RUN dotnet restore "TestProject/TestProject.csproj"
# copy everything and build
COPY /src .
WORKDIR ./TestProject
RUN dotnet build "TestProject.csproj" -c Release --no-restore

# publish
FROM build AS publish
RUN dotnet publish "TestProject.csproj" -c Release -o /publish --no-restore --no-build /p:UseAppHost=false

# final stage/image
FROM base as final
ENV ASPNETCORE_URLS http://+:5100
ENV ASPNETCORE_ENVIRONMENT Production
EXPOSE 5100
WORKDIR /publish
COPY --from=publish /publish .
ENTRYPOINT [ "dotnet", "TestProject.dll" ]

@mxschmitt
Copy link
Member

mxschmitt commented Aug 18, 2023

Focal is the codename for Ubuntu 20.04. Jammy is 22.04. Alpine is not supported see here. See also here our official Docker image and Dockerfile.

Regarding 2. its probably caused by an unsupported OS or outdated Playwright version.

@EgoPingvina
Copy link

Thx for answer!

Focal is the codename for Ubuntu 20.04. Jammy is 22.04. Alpine is not supported see here.

When I tried to use .Net 7 with -focal it says that not found.

See also here our official Docker image

I saw it but can't understand how to use it, sorry for my low-skill in Docker. Is it mean that I should use mcr.microsoft.com/playwright/dotnet:v1.37.0-jammy instead of my FROM? But instead of which one?

Regarding 2. its probably caused by an unsupported OS or outdated Playwright version.

I use the latest version Microsoft.Playwright 1.37.1.

@dutts
Copy link

dutts commented Oct 6, 2023

@braidenstiller I'm afraid this no longer appears to work with the error Unable to locate package powershell.

I have tried running

apt-get update -yq \
&& apt-get install wget -yq \
&& wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb \
&& dpkg -i packages-microsoft-prod.deb \
&& apt-get update -yq \
&& apt-get install powershell -yq

on a clean windows aspnet:6.0-focal base image as I get the same error

docker run -it mcr.microsoft.com/dotnet/aspnet:6.0-focal /bin/bash
root@c889cbbacd1e:/# apt-get update -yq \
> && apt-get install wget -yq \
> && wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb \
> && dpkg -i packages-microsoft-prod.deb \
> && apt-get update -yq \
> && apt-get install powershell -yq
Get:1 http://ports.ubuntu.com/ubuntu-ports focal InRelease [265 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease [114 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease [108 kB]
Get:4 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease [114 kB]
Get:5 http://ports.ubuntu.com/ubuntu-ports focal/universe arm64 Packages [11.1 MB]
Get:6 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 Packages [1234 kB]
Get:7 http://ports.ubuntu.com/ubuntu-ports focal/restricted arm64 Packages [1317 B]
Get:8 http://ports.ubuntu.com/ubuntu-ports focal/multiverse arm64 Packages [139 kB]
Get:9 http://ports.ubuntu.com/ubuntu-ports focal-updates/main arm64 Packages [2705 kB]
Get:10 http://ports.ubuntu.com/ubuntu-ports focal-updates/multiverse arm64 Packages [9393 B]
Get:11 http://ports.ubuntu.com/ubuntu-ports focal-updates/restricted arm64 Packages [48.4 kB]
Get:12 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 Packages [1307 kB]
Get:13 http://ports.ubuntu.com/ubuntu-ports focal-backports/universe arm64 Packages [27.8 kB]
Get:14 http://ports.ubuntu.com/ubuntu-ports focal-backports/main arm64 Packages [54.8 kB]
Get:15 http://ports.ubuntu.com/ubuntu-ports focal-security/multiverse arm64 Packages [3262 B]
Get:16 http://ports.ubuntu.com/ubuntu-ports focal-security/restricted arm64 Packages [48.2 kB]
Get:17 http://ports.ubuntu.com/ubuntu-ports focal-security/universe arm64 Packages [1003 kB]
Get:18 http://ports.ubuntu.com/ubuntu-ports focal-security/main arm64 Packages [2310 kB]
Fetched 20.6 MB in 1s (22.8 MB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libpsl5 publicsuffix
The following NEW packages will be installed:
  libpsl5 publicsuffix wget
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 493 kB of archives.
After this operation, 1393 kB of additional disk space will be used.
Get:1 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 libpsl5 arm64 0.21.0-1ubuntu1 [51.3 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 publicsuffix all 20200303.0012-1 [111 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports focal-updates/main arm64 wget arm64 1.20.3-1ubuntu2 [331 kB]
Fetched 493 kB in 0s (9124 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libpsl5:arm64.
(Reading database ... 6421 files and directories currently installed.)
Preparing to unpack .../libpsl5_0.21.0-1ubuntu1_arm64.deb ...
Unpacking libpsl5:arm64 (0.21.0-1ubuntu1) ...
Selecting previously unselected package publicsuffix.
Preparing to unpack .../publicsuffix_20200303.0012-1_all.deb ...
Unpacking publicsuffix (20200303.0012-1) ...
Selecting previously unselected package wget.
Preparing to unpack .../wget_1.20.3-1ubuntu2_arm64.deb ...
Unpacking wget (1.20.3-1ubuntu2) ...
Setting up libpsl5:arm64 (0.21.0-1ubuntu1) ...
Setting up wget (1.20.3-1ubuntu2) ...
Setting up publicsuffix (20200303.0012-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.12) ...
Selecting previously unselected package packages-microsoft-prod.
(Reading database ... 6447 files and directories currently installed.)
Preparing to unpack packages-microsoft-prod.deb ...
Unpacking packages-microsoft-prod (1.0-ubuntu20.04.1) ...
Setting up packages-microsoft-prod (1.0-ubuntu20.04.1) ...
Hit:1 http://ports.ubuntu.com/ubuntu-ports focal InRelease
Hit:2 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease
Hit:3 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease
Hit:4 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease
Get:5 https://packages.microsoft.com/ubuntu/20.04/prod focal InRelease [3611 B]
Get:6 https://packages.microsoft.com/ubuntu/20.04/prod focal/main arm64 Packages [51.5 kB]
Get:7 https://packages.microsoft.com/ubuntu/20.04/prod focal/main amd64 Packages [238 kB]
Get:8 https://packages.microsoft.com/ubuntu/20.04/prod focal/main armhf Packages [16.6 kB]
Get:9 https://packages.microsoft.com/ubuntu/20.04/prod focal/main all Packages [2586 B]
Fetched 313 kB in 0s (847 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package powershell

@braidenstiller
Copy link
Author

@braidenstiller I'm afraid this no longer appears to work with the error Unable to locate package powershell.

I have tried running

apt-get update -yq \
&& apt-get install wget -yq \
&& wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb \
&& dpkg -i packages-microsoft-prod.deb \
&& apt-get update -yq \
&& apt-get install powershell -yq

on a clean windows aspnet:6.0-focal base image as I get the same error

docker run -it mcr.microsoft.com/dotnet/aspnet:6.0-focal /bin/bash
root@c889cbbacd1e:/# apt-get update -yq \
> && apt-get install wget -yq \
> && wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb \
> && dpkg -i packages-microsoft-prod.deb \
> && apt-get update -yq \
> && apt-get install powershell -yq
Get:1 http://ports.ubuntu.com/ubuntu-ports focal InRelease [265 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease [114 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease [108 kB]
Get:4 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease [114 kB]
Get:5 http://ports.ubuntu.com/ubuntu-ports focal/universe arm64 Packages [11.1 MB]
Get:6 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 Packages [1234 kB]
Get:7 http://ports.ubuntu.com/ubuntu-ports focal/restricted arm64 Packages [1317 B]
Get:8 http://ports.ubuntu.com/ubuntu-ports focal/multiverse arm64 Packages [139 kB]
Get:9 http://ports.ubuntu.com/ubuntu-ports focal-updates/main arm64 Packages [2705 kB]
Get:10 http://ports.ubuntu.com/ubuntu-ports focal-updates/multiverse arm64 Packages [9393 B]
Get:11 http://ports.ubuntu.com/ubuntu-ports focal-updates/restricted arm64 Packages [48.4 kB]
Get:12 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 Packages [1307 kB]
Get:13 http://ports.ubuntu.com/ubuntu-ports focal-backports/universe arm64 Packages [27.8 kB]
Get:14 http://ports.ubuntu.com/ubuntu-ports focal-backports/main arm64 Packages [54.8 kB]
Get:15 http://ports.ubuntu.com/ubuntu-ports focal-security/multiverse arm64 Packages [3262 B]
Get:16 http://ports.ubuntu.com/ubuntu-ports focal-security/restricted arm64 Packages [48.2 kB]
Get:17 http://ports.ubuntu.com/ubuntu-ports focal-security/universe arm64 Packages [1003 kB]
Get:18 http://ports.ubuntu.com/ubuntu-ports focal-security/main arm64 Packages [2310 kB]
Fetched 20.6 MB in 1s (22.8 MB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  libpsl5 publicsuffix
The following NEW packages will be installed:
  libpsl5 publicsuffix wget
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 493 kB of archives.
After this operation, 1393 kB of additional disk space will be used.
Get:1 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 libpsl5 arm64 0.21.0-1ubuntu1 [51.3 kB]
Get:2 http://ports.ubuntu.com/ubuntu-ports focal/main arm64 publicsuffix all 20200303.0012-1 [111 kB]
Get:3 http://ports.ubuntu.com/ubuntu-ports focal-updates/main arm64 wget arm64 1.20.3-1ubuntu2 [331 kB]
Fetched 493 kB in 0s (9124 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libpsl5:arm64.
(Reading database ... 6421 files and directories currently installed.)
Preparing to unpack .../libpsl5_0.21.0-1ubuntu1_arm64.deb ...
Unpacking libpsl5:arm64 (0.21.0-1ubuntu1) ...
Selecting previously unselected package publicsuffix.
Preparing to unpack .../publicsuffix_20200303.0012-1_all.deb ...
Unpacking publicsuffix (20200303.0012-1) ...
Selecting previously unselected package wget.
Preparing to unpack .../wget_1.20.3-1ubuntu2_arm64.deb ...
Unpacking wget (1.20.3-1ubuntu2) ...
Setting up libpsl5:arm64 (0.21.0-1ubuntu1) ...
Setting up wget (1.20.3-1ubuntu2) ...
Setting up publicsuffix (20200303.0012-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.12) ...
Selecting previously unselected package packages-microsoft-prod.
(Reading database ... 6447 files and directories currently installed.)
Preparing to unpack packages-microsoft-prod.deb ...
Unpacking packages-microsoft-prod (1.0-ubuntu20.04.1) ...
Setting up packages-microsoft-prod (1.0-ubuntu20.04.1) ...
Hit:1 http://ports.ubuntu.com/ubuntu-ports focal InRelease
Hit:2 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease
Hit:3 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease
Hit:4 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease
Get:5 https://packages.microsoft.com/ubuntu/20.04/prod focal InRelease [3611 B]
Get:6 https://packages.microsoft.com/ubuntu/20.04/prod focal/main arm64 Packages [51.5 kB]
Get:7 https://packages.microsoft.com/ubuntu/20.04/prod focal/main amd64 Packages [238 kB]
Get:8 https://packages.microsoft.com/ubuntu/20.04/prod focal/main armhf Packages [16.6 kB]
Get:9 https://packages.microsoft.com/ubuntu/20.04/prod focal/main all Packages [2586 B]
Fetched 313 kB in 0s (847 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package powershell

Try the version using NPM instead to install playwright. This ended up being quicker at build time as well.

@ramonaZmole
Copy link

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

ARG ENV
ENV Environment=$ENV

WORKDIR "/tests/src/test/functional/FunctionalTests/FunctionalTests"

COPY ["FunctionalTests.csproj", "FunctionalTests/FunctionalTests/"]
COPY . .
        
#Install powershell to playwright scripts
RUN apt-get update -yq \
    && apt-get install wget -yq \
    && wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb \
    && dpkg -i packages-microsoft-prod.deb \
    && apt-get update -yq \
    && apt-get install powershell -yq


#Build .csproj
RUN dotnet build "FunctionalTests.csproj" \
  -c Release \
  -o /app/build


WORKDIR "/app/build"

#Install playwright dependencies
RUN pwsh ./playwright.ps1 install
RUN pwsh ./playwright.ps1 install chromium
RUN pwsh ./playwright.ps1 install-deps chromium


#Run API tests
WORKDIR "/tests/src/test/functional/FunctionalTests/FunctionalTests"

RUN dotnet test "FunctionalTests.csproj" \
    -c Release \
    -o /app/report

This is how I solved the problem

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

No branches or pull requests

7 participants