How can I create a docker-compose in order to run docker build and run in it?

Hello, what I need is to run docker build & run through Dockerfile, but I found that the only possible way to run docker commands is inside docker-compose file. My Dockerfile looks like this:
dockerfile

FROM golang:1.18-alpine as builder
WORKDIR $GOPATH/src/go.k6.io/k6
ADD . .
RUN apk --no-cache add build-base git &&
go install go.k6.io/xk6/cmd/xk6@latest &&
CGO_ENABLED=1 xk6 build
–with GitHub - grafana/xk6-sql: k6 extension to load test RDBMSs (PostgreSQL, MySQL, MS SQL and SQLite3)
–output /tmp/k6
FROM alpine:3.15
RUN apk add --no-cache ca-certificates \
&& adduser -D -u 12345 -g 12345 k6
COPY --from=builder /tmp/k6 /usr/bin/k6
USER 12345
WORKDIR /home/k6

and my docker-compose:

version: ‘3.4’
services:
grafana:
image: grafana/grafana:9.3.8
ports:
- “3000:3000”
k6:
build:
context: .
dockerfile: Dockerfile
image: grafana/k6:latest
ports:
- “6565:6565”
command: sh -c “docker build -t grafana/k6-for-sql:latest . && docker run --rm -i grafana/k6-for-sql run - <tests/test1.js --insecure-skip-tls-verify”

the commands that I need to run are:
docker build -t grafana/k6-for-sql:latest .
docker run --rm -i grafana/k6-for-sql run - <tests/test1.js --insecure-skip-tls-verify

How the docker-compose should look like in order to run properly?

Hi @kkk1

I haven’t tested your concrete example. I usually go by the Dockerfile in the extension repo. In your case I would start with https://github.com/grafana/xk6-sql/blob/master/Dockerfile, with --with github.com/grafana/xk6-sql instead of --with github.com/grafana/xk6-sql=.

And for the docker-compose, something similar to https://github.com/grafana/xk6-output-timescaledb/blob/main/docker-compose.yml usually works well for me. The timescaledb extension has a good working example of Dockerfile and docker-compose that includes grafana, alongside instructions on how to run it.

If you can’t make it work with those examples let me know and I’ll try to build a similar example with GitHub - grafana/xk6-sql: k6 extension to load test RDBMSs (PostgreSQL, MySQL, MS SQL and SQLite3).

Cheers!

If I understand your question correctly, you want to run a command in the Dockerfile?

You should add a comment to your Dockerfile in the end of the file.

CMD [“”] 

This will leave the Dockerfile open for commands.

I have an example here:

[Turning data into understandable insights with K6 load testing | by Rody Bothe | Medium](https://K6 integration)

If this is not what you meant, let me know!

Still not working, what I need to do is to run the k6 with the CMD command in the Dockerfile, can you help me achieve that? thanks

Hi @kkk1

You cannot run a k6 instance with extensions in docker this way.

This topic is in the realm of docker-knowledge more than k6. and you should be able to achieve this using the docker documentation and the links I shared in my first post. You have to build a docker image that has the extension/s you want to use, and if you want execute later with docker-compose, that is doable. But you need to build the image before, if it does not exist in a docker registry. And that you cannot do in the same docker compose.

For further reference, I’ll detail what you can do to run a k6 with the sql extension.

  • Dockerfile based on https://github.com/grafana/xk6-sql/blob/master/Dockerfile

    # Multi-stage build to generate custom k6 with extension
    FROM golang:1.20-alpine as builder
    WORKDIR $GOPATH/src/go.k6.io/k6
    ADD . .
    RUN apk --no-cache add build-base git
    RUN go install go.k6.io/xk6/cmd/xk6@latest
    RUN CGO_ENABLED=1 xk6 build \
      --with github.com/grafana/xk6-sql \
      --output /tmp/k6
    
    # Create image for running your customized k6
    FROM alpine:3.17
    RUN apk add --no-cache ca-certificates \
      && adduser -D -u 12345 -g 12345 k6
    COPY --from=builder /tmp/k6 /usr/bin/k6
    
    USER 12345
    WORKDIR /home/k6
    
    ENTRYPOINT ["k6"]
    
  • docker-compose.yml based on https://github.com/grafana/xk6-output-timescaledb/blob/main/docker-compose.yml

    services:
      k6:
        build: .
        networks:
          - k6
        ports:
          - "6565:6565"
        volumes:
          - ./samples:/scripts
    
  • Create a folder samples in the same directory.

    mkdir samples
    
  • Create a script, e.g. script.js. In my example I simply import the module (import sql from 'k6/x/sql';), and don’t connect to any database. You can find different sql examples in https://github.com/grafana/xk6-sql/tree/master/examples.

    import http from 'k6/http';
    import { sleep } from 'k6';
    import sql from 'k6/x/sql';
    
    export default function () {
        http.get('https://test.k6.io');
        sleep(1);
    }
    
  • Run the test, for example as described in the example I had shared GitHub - grafana/xk6-output-timescaledb: k6 extension to output real-time test metrics to TimescaleDB.

    docker-compose run --rm -T k6 run -<samples/script.js
    
  • After all this, you should see a test run.

  • You can always check that the image has the right extensions by running docker-compose run --rm -T k6 version. In my case the output includes the extension github.com/grafana/xk6-sql as expected:

    k6 v0.43.1 ((devel), go1.20.3, linux/arm64)
    Extensions:
      github.com/grafana/xk6-sql v0.1.0, k6/x/sql [js]
    

I hope this helps.

Cheers!