2017-09-04 35 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>