Build a k6 binary using Docker | Grafana k6 documentation (original) (raw)

Open source

Using thexk6 Docker image can simplify the process of creating a custom k6 binary. It avoids having to setup a local Go environment, and install xk6 manually.

Note

This tutorial is about creating a custom k6 binary (using Docker). If you want to create a Docker image with a custom k6 binary, refer instead toUsing modules with Docker.

Building your first extension

For example, to build a custom k6 binary with the latest versions of k6 and thexk6-kafka andxk6-output-influxdb extensions, run one of the commands below, depending on your operating system:

docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" grafana/xk6 build \
  --with github.com/mostafa/xk6-kafka \
  --with github.com/grafana/xk6-output-influxdb
docker run --rm -e GOOS=darwin -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" \
  grafana/xk6 build \
  --with github.com/mostafa/xk6-kafka \
  --with github.com/grafana/xk6-output-influxdb
docker run --rm -e GOOS=windows -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" `
  grafana/xk6 build --output k6.exe `
  --with github.com/mostafa/xk6-kafka `
  --with github.com/grafana/xk6-output-influxdb
docker run --rm -e GOOS=windows -v "%cd%:/xk6" ^
  grafana/xk6 build --output k6.exe ^
  --with github.com/mostafa/xk6-kafka ^
  --with github.com/grafana/xk6-output-influxdb

This creates a k6 (or k6.exe) binary in the current working directory.

To build the binary with concrete versions, see the example below (k6 v0.45.1, xk6-kafka v0.19.1, and xk6-output-influxdb v0.4.1):

docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" grafana/xk6 build v0.45.1 \
  --with github.com/mostafa/xk6-kafka@v0.19.1 \
  --with github.com/grafana/xk6-output-influxdb@v0.4.1
docker run --rm -e GOOS=darwin -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" \
  grafana/xk6 build v0.45.1 \
  --with github.com/mostafa/xk6-kafka@v0.19.1 \
  --with github.com/grafana/xk6-output-influxdb@v0.4.1
docker run --rm -e GOOS=windows -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" `
  grafana/xk6 build v0.45.1 --output k6.exe `
  --with github.com/mostafa/xk6-kafka@v0.19.1 `
  --with github.com/grafana/xk6-output-influxdb@v0.4.1
docker run --rm -e GOOS=windows -v "%cd%:/xk6" ^
  grafana/xk6 build v0.45.1 --output k6.exe ^
  --with github.com/mostafa/xk6-kafka@v0.19.1 ^
  --with github.com/grafana/xk6-output-influxdb@v0.4.1

Breaking down the command

The example command line may look a bit intimidating at first, but let’s focus on the first part, which pertains strictly to Docker:

docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6"

This tells Docker to run a new container from an image.

For Windows and Mac, we additionally include the target system as an environment variable:

The remainder is straight from thexk6 documentation, with the exception that we use the grafana/xk6 image rather than a local installation of xk6:

grafana/xk6 build [<k6_version>]
    [--output <file>]
    [--with <module[@version][=replacement]>...]
    [--replace <module=replacement>...]

Flags:
  --output   specifies the new binary name [default: 'k6']
  --replace  enables override of dependencies for k6 and extensions [default: none]
  --with     the extension module to be included in the binary [default: none]

Caution

The use of --replace should be considered an advanced feature to be avoided unless required.

Referring back to our executed command, note that:

Run ./k6 version (or k6.exe version) to check that your build is based on the appropriate k6 version and contains the desired extensions. For example:

$ ./k6 version
k6 v0.43.1 ((devel), go1.20.1, darwin/amd64)
Extensions:
  github.com/grafana/xk6-output-influxdb v0.3.0, xk6-influxdb [output]
  github.com/mostafa/xk6-kafka v0.17.0, k6/x/kafka [js]

Running your extended binary

Now that we have our newly built k6 binary, we can run scripts using the functionalities of the bundled extensions.

Be sure to specify the binary just built in the current directory as ./k6, or else Linux/Mac could execute another k6 binary on your system path. Windows shells will first search for the binary in the current directory by default.

Encountering issues?

If you’re having issues, search thek6 Community Forum. Someone may have had the same issue in the past.