我在localhost:1235上有一个在我的计算机上运行的应用程序,我正在尝试加载测试它。
我为docker安装了k6容器来测试它,但当然从docker的性质来看,我的容器有一个不同的localhost。我想知道做什么。
我运行以下命令:
docker run -it --rm --net=host -v c:/users/k6:/k6 loadimpact/k6 run /k6/script
我在某处读到--net = host在Windows上不起作用,是吗?我如何找到主机IP?
我尝试过本教程: http://blog.michaelhamrah.com/2014/06/accessing-the-docker-host-server-within-a-container/
我发现172.17.0.1的IP在我的测试中不起作用。
我也尝试添加-p 1235:1235
但它失败了,我想docker尝试绑定此端口并转发它。
提前致谢, 哈伊姆
答案 0 :(得分:0)
k6应该能够连接到主机上的“公共”IP - 在以太网或Wifi接口上配置的IP。您可以ipconfig /all
查看所有接口及其IP。
在我的Mac上,我可以这样做:
$ 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 -
即。我在主机的端口8000上启动一个简单的HTTP服务器,然后执行k6 docker镜像并告诉它根据主机上物理的,向外的en1接口的IP地址访问URL。在您的情况下,在Windows上,您可以使用ipconfig
找到面向外部的IP。
答案 1 :(得分:0)
在您的k6脚本中,使用URL host.docker.internal
访问主机上运行的内容。
例如访问位于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);
}
然后在Windows或Mac上可以通过以下方式运行:
$ docker run -i loadimpact/k6 run - <script.js
对于Linux,您需要一个额外的标志
$ docker run --add-host=host.docker.internal:host-gateway -i loadimpact/k6 run - <script.js
参考: