如何通过默认 IP 在 localhost 上运行 docker 容器? [英] How can I run a docker container on localhost over the default IP?

查看:36
本文介绍了如何通过默认 IP 在 localhost 上运行 docker 容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注

同时,当我出于某种原因运行它时,它在 IP 10.0.75.2 上运行:

Meanwhile, when I run it it for some reason it runs on the IP 10.0.75.2:

发生这种情况有什么特别的原因吗?有没有办法让它像例子中一样在 localhost 上运行?

Is there any particular reason why this is happening? And is there any way to get it to run on localhost like in the example?

编辑:我尝试使用 --net=host 但没有结果:

Edit: I tried using --net=host but had no results:

推荐答案

默认网络是桥接的.由于 -P 选项,0.0.0.0:49166->443 显示了容器中公开端口到主机上高级端口的端口映射.您可以通过将该标志更改为类似 -p 8080:80 -p 443:443 的方式手动映射特定端口,以便将主机上的端口 8080 和 443 映射到容器中.

The default network is bridged. The 0.0.0.0:49166->443 shows a port mapping of exposed ports in the container to high level ports on your host because of the -P option. You can manually map specific ports by changing that flag to something like -p 8080:80 -p 443:443 to have port 8080 and 443 on your host map into the container.

您还可以按照您的要求将默认网络更改为您的主机网络.这消除了容器提供的一些隔离和保护,并限制了您配置容器之间集成的能力,这就是它不是默认选项的原因.该语法将是:

You can also change the default network to be your host network as you've requested. This removes some of the isolation and protections provided by the container, and limits your ability to configure integrations between containers, which is why it is not the default option. That syntax would be:

docker run --name nginx1 --net=host -d nginx

<小时>

从您的评论和重读中,我看到您还在询问 10.0.75.2 ip 地址的来源.这取决于您启动 docker 守护程序的方式.当您将 --ip 标志传递给守护程序 这里的文档.如果您在带有 docker-machine 的 vm 中运行 docker,我希望这是您的 vm 的 IP.


from your comments and a reread I see you're also asking about where the 10.0.75.2 ip address comes from. This is based on how you launch the docker daemon. That IP binding is assigned when you pass the --ip flag to the daemon documentation here. If you're running docker in a vm with docker-machine, I'd expect this to be the IP of your vm.

这篇关于如何通过默认 IP 在 localhost 上运行 docker 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆