Building custom k6 container image

Our performance tests project is complex, we have +40 .js files, csv feeder files and use custom extensions, so we need to bundle all of that in a single image.

We want things to be version controlled, deploy performance tests in a cloud-native way and the image to be compatible with official k6 image.

# Build the k6 binary with the extension
FROM golang:1.20 as builder

RUN go install go.k6.io/xk6/cmd/xk6@v0.8.1
# Update README.md when changing this
RUN xk6 build v0.43.0 \
  --with github.com/grafana/xk6-output-influxdb@80ad3684733b9916b40b2e9b774e5472defc0d99 \
  --with github.com/b4dc0d3rs/k6-utils@ef6f3ebdd33fe8bf88755619022e86fdfa2b6cb5

FROM grafana/k6:0.43.0
COPY --from=builder /go/k6 /usr/bin/k6

WORKDIR /opt

COPY resources/ /opt/resources/
COPY tests/ /opt/tests/
COPY src/ /opt/

ENTRYPOINT ["/opt/run.sh"]

Our simplified run.sh looks like this:

#!/bin/sh

k6 run --paused \
    --out influxdb=http://influxdb:8086/k6 \
    -e NAMESPACE=$NAMESPACE \
    --no-color \
    main.js

You can of course bundle this command in the container image under ENTRYPOINT, but I pass over 20 environment flags here, so I want this to be in a separate file.