Create k6 docker image with xk6-cognito extension

Hey community,

I want to create a k6 docker image with xk6-cognito extension and i am pretty new to docker. I am using k6 grafana base image with added xk6-cognito:

# Use golang as the builder image
FROM golang:1.16-alpine as builder

# Set the working directory
WORKDIR $GOPATH/src/go.k6.io/k6

# Copy the k6 source code into the container
ADD . .

# Install Git
RUN apk --no-cache add git

# Build k6 with the xk6-cognito extension
RUN CGO_ENABLED=0 go install -a -trimpath -ldflags "-s -w -X go.k6.io/k6/lib/consts.VersionDetails=$(date -u +'%FT%T%z')/$(git describe --always --long --dirty)" && \
    go install -trimpath github.com/k6io/xk6/cmd/xk6@latest && \
    xk6 build --with github.com/tmieulet/xk6-cognito@latest && \
    cp k6 $GOPATH/bin/k6

# Use Alpine as the final image
FROM alpine:3.13

# Install CA certificates and create a non-root user
RUN apk add --no-cache ca-certificates && \
    adduser -D -u 12345 -g 12345 k6

# Copy the k6 binary from the builder stage
COPY --from=builder /go/bin/k6 /usr/bin/k6

# Set the user to run the container
USER 12345

# Set the entrypoint to k6
ENTRYPOINT ["k6"]

I keep getting go.mod errors
0.116 fatal: not a git repository (or any of the parent directories): .git
0.135 go: go.mod file not found in current directory or any parent directory; see ‘go help modules’

Can you help?

Hi @joaomatos3, welcome to the community forum!

I modified your Dockerfile a little. This works for me:

FROM grafana/xk6 as builder

# Build k6 with the xk6-cognito extension
RUN xk6 build --with github.com/tmieulet/xk6-cognito@latest && \
    cp k6 $GOPATH/bin/k6

# Use Alpine as the final image
FROM alpine:3.13

# Install CA certificates and create a non-root user
RUN apk add --no-cache ca-certificates && \
    adduser -D -u 12345 -g 12345 k6

# Copy the k6 binary from the builder stage
COPY --from=builder /go/bin/k6 /usr/bin/k6

# Set the user to run the container
USER 12345

# Set the entrypoint to k6
ENTRYPOINT ["k6"]