Docker 直接访问容器 IP/Port
Docker access container IP/Port directly
我有 docker 个实验容器。所以我不知道以后尝试新应用程序时会使用哪些端口。
不在 docker-运行 命令中公开它,是否可以从主机使用 ip/port 访问 docker 容器应用程序?
update: OP 在原始问题中没有提供关于 docker-for-mac
的内容,因此如果您使用的是此版本的,则无法应用其余内容docker.
原回答:
是的,你可以做到。
假设您有一个名为 boring_pare
的容器和一个位于端口 8080
上的 Web 应用程序 运行ning。您可以通过请求 http://[ip_of_boring_pare]:8080
从您的主机 访问它 。如果您使用默认的 docker 网络,此 ip 可能会在 172.17.0.xxx
范围内。
要找到此 IP,您可以 inspect
您的容器,方法是:
docker container inspect boring_pare
此外,您提到:
Isn't it possible to access docker container application with ip/port from the host without exposing it in the docker-run command?
这里的正确术语是发布。延伸阅读:
Documentation 关于 EXPOSE
/ Dockerfile
...The EXPOSE
instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p
flag on docker run
to publish and map one or more ports, or the -P
flag to publish all exposed ports and map them to to high-order ports.
Difference between “expose” and “publish” in docker
更新 以回答 @robie2011 的评论:
下面,我运行3条命令:
- 运行
nginx
容器而不发布 port 80
到 host
端口
- 检查其
ip address
- 使用
host
中的 curl
访问 nginx
主页
我的控制台输出:
$ 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>
$
Docker for Mac is unable to route traffic to containers, and from containers back to the host.
https://docs.docker.com/docker-for-mac/networking/#i-cannot-ping-my-containers
我有 docker 个实验容器。所以我不知道以后尝试新应用程序时会使用哪些端口。 不在 docker-运行 命令中公开它,是否可以从主机使用 ip/port 访问 docker 容器应用程序?
update: OP 在原始问题中没有提供关于 docker-for-mac
的内容,因此如果您使用的是此版本的,则无法应用其余内容docker.
原回答:
是的,你可以做到。
假设您有一个名为 boring_pare
的容器和一个位于端口 8080
上的 Web 应用程序 运行ning。您可以通过请求 http://[ip_of_boring_pare]:8080
从您的主机 访问它 。如果您使用默认的 docker 网络,此 ip 可能会在 172.17.0.xxx
范围内。
要找到此 IP,您可以 inspect
您的容器,方法是:
docker container inspect boring_pare
此外,您提到:
Isn't it possible to access docker container application with ip/port from the host without exposing it in the docker-run command?
这里的正确术语是发布。延伸阅读:
Documentation 关于
EXPOSE
/ Dockerfile...The
EXPOSE
instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the-p
flag ondocker run
to publish and map one or more ports, or the-P
flag to publish all exposed ports and map them to to high-order ports.Difference between “expose” and “publish” in docker
更新 以回答 @robie2011 的评论:
下面,我运行3条命令:
- 运行
nginx
容器而不发布port 80
到host
端口 - 检查其
ip address
- 使用
host
中的curl
访问nginx
主页
我的控制台输出:
$ 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>
$
Docker for Mac is unable to route traffic to containers, and from containers back to the host.
https://docs.docker.com/docker-for-mac/networking/#i-cannot-ping-my-containers