XK6 Browser in Docker

Hi team,

I used the XK6 browser locally to run some additional size comparison on some images, it works great. However in our pipeline it runs with a Dockerfile. I have a question on how that would translate to a Dockerfile(it works for Prometheus XK6). I added the xk6-browser line to it, but I am running into an error.

I used the following Dockerfile

FROM golang:1.17-alpine as builder
WORKDIR "$GOPATH"/src/go.k6.io/k6
COPY . .

RUN apk --no-cache add git &&\
    go install -trimpath go.k6.io/xk6/cmd/xk6@latest &&\
    xk6 build --with github.com/grafana/xk6-output-prometheus-remote@latest &&\
    xk6 build --with github.com/grafana/xk6-browser &&\
    cp k6 "$GOPATH"/bin/k6

FROM alpine:3.13
RUN apk add --no-cache ca-certificates && \
    adduser -D -u 12345 -g 12345 k6
COPY --from=builder /go/bin/k6 /usr/bin/k6

USER 12345
CMD [""]

However, when I try to run the command

docker run --rm -i -v "$PWD:/app" xk6_browser_image ./xk6-browser run /app/tests/sometest/sometest.js

It gives the following error, I also tried with quotes and without but no luck.

What am I missing here?


Hi Rody,

This question seems to be unrelated to xk6-browser and more to do with how to work with docker, so probably better suited for a different forum.

I took a look at the dockerfile that you’ve created and found that it should work if you issue the following command:

docker run --rm -i -v "$PWD:/app" xk6_browser_image k6 run /app/tests/sometest/sometest.js


Thanks for the reply @ankur .
I tried your solution but this will just run k6 and not xk6-browser.

time="2022-09-06T11:49:17Z" level=error msg="Exception raised in test \"TEST NAME\". Failing the test and continuing. \nlaunching browser: exec: no command" source=console

This is locally, you see browser specific items below the green.

This is docker, you see the exception being raised that it can’t launch browser. Also, There is no browser content below.

From the screenshot

So there is definitely a difference that I am missing.

Hi Rody,

This command you are running in the dockerfile:

Is creating a single binary (called k6) that is combining k6 with xk6-output-prometheus-remote and xk6-browser. So the command I suggested earlier should work with browser tests as long as the browser is installed in the docker image.

Looking at the error message you’re now getting, the important part in that error message is:

This is telling you (and noted that the logging message isn’t perfectly clear) that it can’t launch the browser, and looking back at your dockerfile I can see that there isn’t one being installed in the image. You should be able to install chromium with apk add chromium and then run the tests. It’s worth noting here that some of the options (such as headless: false when launching a new browser) will not work in a GUI-less distro such as Alpine.


1 Like


You saved my day! Thanks so much, this works perfectly :slight_smile:

Is creating a single binary (called k6 ) that is combining k6 with xk6-output-prometheus-remote and xk6-browser .

Just to clarify, the command:

xk6 build --with github.com/grafana/xk6-output-prometheus-remote@latest &&\
    xk6 build --with github.com/grafana/xk6-browser

Would build a k6 binary containing the Prometheus extension, and then overwrite it with a binary containing the xk6-browser extension. So the resulting binary would be able to run xk6-browser scripts, but not use the Prometheus output.

Instead, if you want to combine both extensions in a single binary, you can pass --with more than once, so the command should be:

xk6 build --with github.com/grafana/xk6-output-prometheus-remote@latest \
  --with github.com/grafana/xk6-browser

Thank you for taking the time to highlight this.
I will make the changes as I want to use both in the Dockerfile.