运行Java等价的" docker exec -ti {container} curl"

时间:2017-09-04 09:54:39

标签: java docker nginx cucumber

我有一个docker容器,配置为运行Nginx并重定向流量。我在docker容器外运行验收测试,需要重定向我的URL请求。

目前正在运行" docker exec -ti {container} curl"从命令行返回我的预期响应,但是我的Unirest HTTP客户端没有通过容器发送请求(因为我没有告诉它,不知道我是否可以如何)。

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

1 个答案:

答案 0 :(得分: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>