2017-09-04 34 views
0

我有一个Docker容器配置为运行Nginx并重定向流量。我正在Docker容器之外运行验收测试,并且需要我的URL请求被重定向。运行相当于“docker exec -ti {container} curl”的Java代码

目前从命令行运行“docker exec -ti {container} curl”会返回我想要的响应,但是我的Unirest HTTP客户端没有通过容器发送请求(因为我没有告诉它,不确定如何/如果我能)。

我认为我的解决方案是以编程方式在我的测试中创建“docker exec curl”请求以实现此结果,但似乎无法找到如何。如果有人有一个想法如何实现这一点,将不胜感激。

+0

你为什么不使用'-p'或'-P'选项和测试使用直接主机URL输出端口? –

+0

我最初尝试做这个输出端口80,但Nginx已经在容器上使用该端口。除非有两种方式可以使用,否则我在集装箱化方面有点新鲜。 – WillBroadbent

回答

0

您需要使用动态端口映射这个

$ ID=$(docker run -P -d nginx) 

$ PORT=$(docker port $ID | grep "80/tcp" | cut -d":" -f2) 

$ curl http://localhost:${PORT}/ 
<!DOCTYPE html> 
<html> 
<head> 
<title>Welcome to nginx!</title> 
<style> 
    body { 
     width: 35em; 
     margin: 0 auto; 
     font-family: Tahoma, Verdana, Arial, sans-serif; 
    } 
</style> 
</head> 
<body> 
<h1>Welcome to nginx!</h1> 
<p>If you see this page, the nginx web server is successfully installed and 
working. Further configuration is required.</p> 

<p>For online documentation and support please refer to 
<a href="http://nginx.org/">nginx.org</a>.<br/> 
Commercial support is available at 
<a href="http://nginx.com/">nginx.com</a>.</p> 

<p><em>Thank you for using nginx.</em></p> 
</body> 
</html>