简体   繁体   中英

Using k6 on docker to test a localhosted site

I've got a app running on my computer in localhost:1235, and I'm trying to load test it.

I installed k6 container for docker to test it, but of course from the nature of docker, my container has a different localhost. I'm trying to understand what do.

I run the following command: docker run -it --rm --net=host -vc:/users/k6:/k6 loadimpact/k6 run /k6/script

I read somewhere that --net=host doesn't work on windows, is that right? How would I find the host IP?

I've tried running by this tutorial: http://blog.michaelhamrah.com/2014/06/accessing-the-docker-host-server-within-a-container/

The IP I find 172.17.0.1 doesn't work in my test.

I also tried adding -p 1235:1235 but it failed, I guess docker tries to bind this port and just forward to it.

Thanks in advance, Chaim

Inside your k6 script use the url host.docker.internal to access something running on the host machine.

For example to access a service running on the host at http://localhost:8080

// script.js
import http from "k6/http";
import { sleep } from "k6";

export default function () {
  http.get("http://host.docker.internal:8080");
  sleep(1);
}

Then on windows or mac this can be run with:

$ docker run -i loadimpact/k6 run - <script.js

for linux you need an extra flag

$ docker run --add-host=host.docker.internal:host-gateway -i loadimpact/k6 run - <script.js

References:

k6 inside the docker instance should be able to connect to the "public" IP on your host machine - the IP that is configured on your ethernet or Wifi interface. You can do a ipconfig /all to see all your interfaces and their IPs.

On my Mac I can do this: $ python httpserv.py & [1] 7824 serving at port 8000 $ ifconfig en1 en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 ether b8:09:8a:bb:f7:ed inet6 fe80::148f:5671:5297:fc24%en1 prefixlen 64 secured scopeid 0x5 inet 192.168.0.107 netmask 0xffffff00 broadcast 192.168.0.255 nd6 options=201<PERFORMNUD,DAD> media: autoselect status: active $ echo 'import http from "k6/http"; export default function() { let res = http.get("http://192.168.0.107:8000"); console.log(res.status); };' |docker run -i loadimpact/k6 run - $ python httpserv.py & [1] 7824 serving at port 8000 $ ifconfig en1 en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 ether b8:09:8a:bb:f7:ed inet6 fe80::148f:5671:5297:fc24%en1 prefixlen 64 secured scopeid 0x5 inet 192.168.0.107 netmask 0xffffff00 broadcast 192.168.0.255 nd6 options=201<PERFORMNUD,DAD> media: autoselect status: active $ echo 'import http from "k6/http"; export default function() { let res = http.get("http://192.168.0.107:8000"); console.log(res.status); };' |docker run -i loadimpact/k6 run -

Ie I start a simple HTTP server on port 8000 of the host machine, then executes the k6 docker image and tells it to access a URL based on the IP address of the physical, outward-facing en1 interface on the host machine. In your case, on Windows, you can use ipconfig to find out your external-facing IP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM