How to run k6 browser via docker in v0.43.0?

The instructions to run k6 browser in that moment is only available by CLI:

K6_BROWSER_ENABLED=true k6 run script.js

If I try:

K6_BROWSER_ENABLED=true docker run --rm -i grafana/k6 run - <script.js


          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  (‾)  |
  / __________ \  |__| \__\ \_____/ .io

time="2023-03-01T18:31:39Z" level=error msg="GoError: To run browser tests set env var K6_BROWSER_ENABLED=true\n\tat go.k6.io/k6/js.(*InitContext).Require-fm (native)\n\tat file:///-:2:0(19)\n" hint="script exception"

You’ll want to use the -e switch for the Docker command to pass the environment variable to the container.

docker run --rm -e K6_BROWSER_ENABLED=true -i grafana/k6 run - <script.js

3 Likes

I try to do a docker compose file, but it didn’t work:

version: "2.1"

services:
    k6:
        image: grafana/k6:latest
        command: run /script.js 
        volumes:
            - ./script.js:/script.js
        environment:
            - K6_BROWSER_ENABLED=true

Do you know how to configure that?

The error that I got is:


ERRO[0000] GoError: launching browser: exec: no command
        at github.com/grafana/xk6-browser/browser.mapBrowserType.func1 (native)
        at file:///script.js:16:34(9)  executor=per-vu-iterations scenario=default source=stacktrace

I got the same error that I posted above with this command

Hi @f.gardin

The problem that you are facing is because, even though xk6-browser is now included in k6 as an experimental module, the current k6 Docker image does not include a browser, therefore the launch command is failing to start such process. There is an issue created in order to bundle a browser in the k6 Docker image that you can track here.

By now I think this Dockerfile example can help you build an image for latest k6 release including chromium browser:

FROM grafana/k6:0.43.1
USER root

RUN apk update && apk add --no-cache chromium

ENV K6_BROWSER_ENABLED=true
3 Likes

I got this error:

 => ERROR [2/2] RUN apk update && apk add --no-cache chromium                                                                 0.5s
------
 > [2/2] RUN apk update && apk add --no-cache chromium:
#0 0.375 fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/main/x86_64/APKINDEX.tar.gz
#0 0.422 140465988299592:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed:ssl/statem/statem_clnt.c:1919:
#0 0.424 ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.16/main: Permission denied
#0 0.424 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.16/main: No such file or directory
#0 0.424 fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/community/x86_64/APKINDEX.tar.gz
#0 0.451 140465988299592:error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed:ssl/statem/statem_clnt.c:1919:
#0 0.453 ERROR: https://dl-cdn.alpinelinux.org/alpine/v3.16/community: Permission denied
#0 0.453 WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.16/community: No such file or directory
#0 0.453 2 errors; 15 distinct packages available

I am running this script in wsl

Hi @f.gardin,

I was able to build a docker image on WSL with the Dockerfile in this reply with docker build -t browser-k6 -f Dockerfile ..

Can you walk us through what you are doing to build the image?

Cheers,
Ankur

Hi all!

I built a Docker image with a browser as instructed:

FROM grafana/k6:0.45.0
USER root

RUN apk update && apk add --no-cache chromium

ENV K6_BROWSER_ENABLED=true

USER k6

But when I use it to run the following test (from the docs):

import { chromium } from 'k6/experimental/browser';

export default async function () {
  const browser = chromium.launch({
    headless: true,
    timeout: '60s', // Or whatever time you want to define
  });
  const page = browser.newPage();

  try {
    await page.goto('https://test.k6.io/');
    page.screenshot({ path: 'screenshot.png' });
  } finally {
    page.close();
    browser.close();
  }
}

Then I get the following error:

time="2023-07-05T12:27:35Z" level=error msg="process with PID 16 unexpectedly ended: signal: trace/breakpoint trap (core dumped)" category=browser elapsed="0 ms" goroutine=58
time="2023-07-05T12:27:35Z" level=error msg="Uncaught (in promise) GoError: launching browser: ptrace: Operation not permitted (1)\n\tat github.com/grafana/xk6-browser/browser.mapBrowserType.func2 (native)\n\tat file:///-:4:34(9)\n" executor=per-vu-iterations scenario=default

If I run the same test using the k6 binary, then it works.

Is there something else I need to install and/or configure on the Docker image?

Hi @brunobastosg,

Welcome to the forum!

What OS are you building this image on?

It looks like chrome is crashing in the running container, which from experience generally means that you need to do one of two things:

  1. If working on a m1+ Mac, then you can’t work with the grafana/k6:0.45.0 image since it’s built for the amd64 architecture. Work with the this dockerfile instead.
  2. Try and work with a newer version of Chrome.

Let us know if that helps.

Cheers,
Ankur

Hi @ankur , thanks for the reply.

I’m building the image on Ubuntu 22.04.

I solved the problem by providing the argument no-sandbox to chromium, as below:

const browser = chromium.launch({ args: ['no-sandbox'], headless: true, timeout: '60s' });

I’m glad you were able to resolve the issue. Incase you’re interested, if you set a non-root user at the end of the dockerfile then you shouldn’t need to use no-sandbox.

I did set a non-root user at the end of the dockerfile (unless the k6 user is root).

But even so, I had to use no-sandbox.