4

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 -v c:/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

Robert
  • 25,401
  • 6
  • 72
  • 85
Chiptus
  • 849
  • 7
  • 22

2 Answers2

2

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:

Stuart
  • 168
  • 6
0

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 -

I.e. 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.

Ragnar
  • 1,011
  • 1
  • 8
  • 14