2
FROM alpine:latest
# Copy source to container
RUN mkdir -p /usr/app
# Copy source code
COPY package.json /usr/app
COPY package-lock.json /usr/app
COPY . /usr/app
# Set working directory
WORKDIR /usr/app
# Environment variables
ENV BASE_URL="Local https url"
ENV PARALLEL_RUN=false
ENV TAG=int
ENV PLAYWRIGHT_BROWSERS_PATH=/usr/lib
# npm install
RUN apk add --update npm
RUN apk add chromium
# Run tests
RUN npm run codeceptjs

Above is the Dockerfile. When tried to Build the image from docker file then I am getting below error:

13 8.596 Error: browserType.launch: Failed to launch chromium because executable doesn't exist at /usr/lib/chromium-888113/chrome-linux/chrome
#13 8.596 Try re-installing playwright with "npm install playwright"**

Although, I can see chromium is getting installed at the mentioned path but still it saying "executable not present".

valiano
  • 16,433
  • 7
  • 64
  • 79
Chals
  • 21
  • 4
  • Do you have `playwright` as dependency in your package.json? It looks like you don't run `npm install` anywhere in your Dockerfile – st.huber Sep 28 '21 at 07:08
  • I am using RUN apk add --update npm which will install npm. Yes, I have playwright dependency in package.json – Chals Sep 30 '21 at 10:06
  • That will install `npm`, correct but it won't install your node_modules. Please add `RUN npm install` after `RUN apk add chromium` and try again – st.huber Sep 30 '21 at 10:07
  • I tried the above mentioned steps and added RUN npm install but I am still getting below error: #15 10.76 Error: browserType.launch: Failed to launch chromium because executable doesn't exist at /usr/lib/chromium-888113/chrome-linux/chrome #15 10.76 Try re-installing playwright with "npm install playwright" – Chals Sep 30 '21 at 10:22
  • Which NodeJS version are you using? Could it be related to https://github.com/microsoft/playwright/issues/4033? – st.huber Sep 30 '21 at 10:49
  • I checked the above solution but it didn't worked. I am using NodeJS version as 15.12.5 – Chals Sep 30 '21 at 11:17

1 Answers1

0

I believe your problem lies with using alpine.

According to the playwright developers, there are no plans to support playwright on alpine. This makes your whole undertaking more complex. It's correct that you need to provide your own chromium and cannot use the browsers that come with playwright. Therefore, you should set PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD to prevent any (incompatible) browsers from being loaded.

The chromium executable should be in your Docker image under /usr/bin/chromium-browser. You need to use playwright's browserType.launch to set the path to the executable:

const { chromium } = require("playwright-chromium");
// ...
const browser = await chromium.launch({
    executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH,
});

If you want a simpler solution, I would suggest using the Docker image of Zanika, containing chromium and playwright already. Here the link to the tag on DockerHub. At the very least you can see it as a reference implementation if you still want to use your own image.

st.huber
  • 1,481
  • 2
  • 24
  • 45
  • I tried Zanika docker image but I am getting error as browserType.launch: Failed to launch: Error: spawn /usr/lib/chromium-888113/chrome-linux/chrome ENOENT – Chals Oct 01 '21 at 05:33
  • can you please share what you try to run with `npm run codeceptjs`. Also the `RUN` command is used during Docker image builds. If you want to run a node script in your Docker container then you have do define an [`ENTRYPOINT`](https://docs.docker.com/engine/reference/builder/#entrypoint) or [`CMD`](https://docs.docker.com/engine/reference/builder/#cmd) command – st.huber Oct 01 '21 at 06:12
  • What do you mean by you "tried Zanika docker image"? How did you run it? How did you add your code to be run to it? – st.huber Oct 01 '21 at 06:12
  • Here is the code for npm run codeceptjs in package.json file: "scripts": { "codeceptjs:local": "npx codeceptjs run", "codeceptjs": "if test $PARALLEL_RUN = true; then npm run codeceptjs:workers:tag; else npm run codeceptjs:tag; fi", "codeceptjs:tag": "npx codeceptjs run --grep $TAG", "codeceptjs:workers:tag": "npx codeceptjs run-workers $PARALLEL_WORKERS --grep $TAG" }, – Chals Oct 01 '21 at 06:19
  • FROM zenika/alpine-chrome:with-node USER root WORKDIR /usr/app COPY package.json /usr/app COPY package-lock.json /usr/app COPY . /usr/app # Environment variables ENV BASE_URL="Local https url" ENV PARALLEL_RUN=false ENV TAG=int ENV PLAYWRIGHT_BROWSERS_PATH=/usr/lib ENV PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium-browser RUN rm -rf node_modules RUN npm cache clean --force RUN npm config set unsafe-perm true RUN npm install -g ts-node RUN npm install --force – Chals Oct 01 '21 at 06:21
  • # Run tests ENTRYPOINT ["chromium-browser" "--headless" "--use-gl=swiftshader" "--disable-software-rasterizer" "--disable-dev-shm-usage"] RUN npm run codeceptjs Here is the remaining part of docker file as it was not possible to add entire docker file in single comment – Chals Oct 01 '21 at 06:21
  • Thank you. Is there any reason why you want to use alpine? Can't you just use the official [codecept image](https://codecept.io/docker.html)? – st.huber Oct 01 '21 at 06:31
  • Codeceptjs Image size is huge around 4 GB whereas alpine image is in mb's. Sometimes downloading the codeceptjs image from Jenkins Linux agent do takes time and my plan is to have customized image having less size – Chals Oct 01 '21 at 06:33
  • Could you please tell how to pass below code in docker file having Alpine base image: const { chromium } = require("playwright-chromium"); // ... const browser = await chromium.launch({ executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH, }); – Chals Oct 01 '21 at 12:32
  • 1
    You write in in a JS file ([example](https://github.com/Zenika/alpine-chrome/blob/master/with-playwright/src/useragent.js)) and add `CMD ["node", "src/useragent"]` to your Dockerfile. That's how they did it at Zenika – st.huber Oct 01 '21 at 13:36
  • Thanks. Now only problem I am getting is chromium is getting downloaded with version as chromium (93.0.4577.82-r0) but its looking for another version browserType.launch: Failed to launch chromium because executable doesn't exist at /usr/lib/chromium/chromium-888113/chrome-linux/chrome I am not sure why there is mismatch in downloading and executing the chromium version. – Chals Oct 01 '21 at 13:50
  • @Chals please consider upvoting my answer and/or marking it as accepted answer if it helped you – st.huber Apr 01 '22 at 07:53