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