如何从外部主机(mac)连接到docker

时间:2015-05-12 10:03:46

标签: networking docker

我是码头工人的新手。

我运行了一个使用mongo运行它的docker容器(使用return iter()

boot2docker

在码头工具内:

docker run --name some-mongo -p 27017:27017 -d mongo

root@8e9b742c127a:/# mongo MongoDB shell version: 3.0.2 connecting to: test Server has startup warnings: 2015-05-12T08:49:56.130+0000 I CONTROL [initandlisten] 2015-05-12T08:49:56.130+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2015-05-12T08:49:56.130+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-05-12T08:49:56.130+0000 I CONTROL [initandlisten] 2015-05-12T08:49:56.130+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2015-05-12T08:49:56.130+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-05-12T08:49:56.130+0000 I CONTROL [initandlisten] > show dbs local 0.078GB >

docker ps

现在事情变得有点棘手,因为他们需要网络知识。

如何从外面连接到它?

尝试:

  1. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8e9b742c127a mongo:latest "/entrypoint.sh mong About an hour ago Up About an hour 0.0.0.0:27017->27017/tcp some-mongo

  2. mongo localhost:27017

  3. docker inspect some-mongo | grep IPAddress "IPAddress": "172.17.0.12", mongo 172.17.0.12:27017

  4. mongo 0.0.0.0:27017
  5. mongo 10.0.0.12:27017

1 个答案:

答案 0 :(得分:2)

原生Docker: LInux

通过-p 27017:27017 向您的主机公开端口,您可以通过 Docker Host 本身的IP地址访问MongoDB服务。

假设您的Docker主机是192.168.0.1,您可以使用该IP地址。

请参阅:binding container ports to the hostConnect using Network port mapping

非原生Docker :( 例如:OS X / Windows

更新:以上回复适用于Linux上的Docker;如果您通过boot2docker之类的操作系统在OS X上,则需要连接到boot2docker虚拟机的IP地址,该地址可以从boot2docker CLI获得:

$ boot2docker ip
192.168.59.103