我有一个用于实验的码头容器。所以当我尝试新的应用程序时,我不知道我会使用哪些端口。有没有可能不在docker-run命令中暴露出来,就从主机上使用ip/port访问docker容器应用程序?
发布于 2018-01-16 17:34:37
Docker for Mac无法将流量路由到容器,并从容器返回主机。
https://docs.docker.com/docker-for-mac/networking/#i-cannot-ping-my-containers
发布于 2017-11-13 18:43:09
更新: OP没有在原始问题中提供关于docker-for-mac的信息,因此如果您使用此版本的docker,则不能应用其余部分。
原始答案:
是的,你可以这样做。
假设您有一个名为boring_pare的容器和一个在端口8080上运行的web应用程序。您可以通过请求http://[ip_of_boring_pare]:8080从主机通过访问它。如果您使用的是默认的docker网络,则此ip可能在172.17.0.xxx范围内。
要找到这个IP,您可以使用以下命令inspect您的容器:
docker container inspect boring_pare此外,您还提到:
有没有可能通过ip/端口从主机访问docker容器应用程序,而不使用在docker-命令中公开它?
这里的正确术语应该是发布。进一步阅读:
关于EXPOSE / Dockerfile的
...The EXPOSE指令实际上并不发布端口。它作为构建镜像的人和运行容器的人之间的一种类型的文档,关于要发布的端口。要在运行容器时实际发布端口,请使用docker run上的-p标志发布并映射一个或多个端口,或使用-P标志发布所有公开的端口并将其映射到高阶端口。
更新以回应@robie2011的评论:
下面,我运行3个命令:
nginx容器而不将port 80发布到host端口host检查其curl以访问<代码>D49主页我的控制台输出:
$ docker run --rm --name some-nginx -d nginx
0e53d3b5ef6d65a4731c4066f3523c5ecd3c118abedae44b33e89fdf8e401632
$ docker container inspect --format '{{ .NetworkSettings.IPAddress }}' some-nginx
172.17.0.3
$ curl 172.17.0.3
<!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>
$https://stackoverflow.com/questions/47261308
复制相似问题