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:

FROM golang:1.18-alpine as builder
ADD . .
RUN apk --no-cache add build-base git &&
go install &&
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’
image: grafana/grafana:9.3.8
- “3000:3000”
context: .
dockerfile: Dockerfile
image: grafana/k6:latest
- “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, with --with instead of --with

And for the docker-compose, something similar to 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).


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

    # Multi-stage build to generate custom k6 with extension
    FROM golang:1.20-alpine as builder
    ADD . .
    RUN apk --no-cache add build-base git
    RUN go install
    RUN CGO_ENABLED=1 xk6 build \
      --with \
      --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

        build: .
          - k6
          - "6565:6565"
          - ./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

    import http from 'k6/http';
    import { sleep } from 'k6';
    import sql from 'k6/x/sql';
    export default function () {
  • 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 as expected:

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

I hope this helps.