Installing k6 in a docker image

Do we have any example of running k6 in a docker image maybe fedora/linux/centos as base image?

Hi @rlal

Welcome to the community forum :wave:

The k6 dockerfile is a good start. It uses golang:1.20-alpine to build k6, and alpine:3.17 to run.

Any particular reason why you need to create your own docker image? Is the already built docker image not working for you?

If you can share what you are attempting and the errors you get, we’ll help you figure it out.

Cheers!

Thanks @eyeveebe for prompt reply.
I have a script file which i can run using command: “k6 run src/test-perf.js”

I have a repository for doing performance test, and want to run this perf test on every build.

I thought the best approach would be to create a docker image which will include my test performance script and have to install k6 in that image to run that script.

What do you suggest would be the best approach? How can i use the “docker pull grafana/k6” ?

Hi @rlal

Happy to help!

You mention you hare a repository with the tests and you run them with every build. Are you using any CI/CD tool for this (Gitlab, Azure, etc.)?

Usually I would recommend just using the official docker image and mount the volume where the scripts are. Though if you are running a concrete CI for the build we can look into an integration.

You could probably have a Dockerfile that extends the official image, FROM grafana/k6:latest, and copy the scripts locally into that image. However, it’s probably not worth it as you would always need to build the image when changing the scripts.

If you can provide a bit more context we can further advice.

Cheers!

Hi @eyeveebe ,
We are using jenkins for CICD tool.
I will try it out your suggestions and look the links you shared.

Meanwhile, i tried “docker run --rm -i grafana/k6 run - <src/test-perf.js” and it worked. Later i tried to save the detailed output in a different json file. I modified this command to:
‘docker run --env accessToken=devaccess_token --rm -i grafana/k6 run - <src/test-perf.js --out json=<test.json’

But this is not working for me. Can you help in correcting the above command to output it in a output json file?

Hi @rlal

When using the k6 docker image, you can’t just give the script name since the script file will not be available to the container as it runs. Instead you must tell k6 to read stdin by passing the file name as -. Then you pipe the actual file into the container with < or equivalent. This will cause the file to be redirected into the container and be read by k6. And this works for the input script, if you don’t mount the volume for the container.

For the output this is not the same. For the output file to exist outside the container once k6 finishes, you need to mount a volume. As documented in: JSON (see docker command)

$ docker run -it --rm \
    -v <scriptdir>:/scripts \
    -v <outputdir>:/jsonoutput \
    grafana/k6 run --out json=/jsonoutput/my_test_result.json /scripts/script.js

In this case, you would see the file under <outputdir> in the host running docker, once the test is finished. In the example you can see that also the scripts directory <scriptdir> is mounted so the container can read the scripts from the host file system.

I hope this helps.

Cheers!

Hi @eyeveebe,
It worked for me!!
Thanks for your prompt reply everytime. We are loving using K6!!!

You guys rockzz!!!

Thanks once again,
Ratnesh Lal

1 Like