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

我是Docker的新手.

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

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

在Docker里面:

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是

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

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

我该如何从外面连接到它?

尝试:

> mongo localhost:27017
>
docker检查some-mongo | grep IPAddress
“IPAddress”:“172.17.0.12”,
mongo 172.17.0.12:27017

> mongo 0.0.0.0:27017
> mongo 10.0.0.12:27017
> mongo 192.168.3.124:27017

最佳答案 原生Docker:(LInux)

通过-p 27017:27017将端口暴露给主机,您可以通过Docker主机本身的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
点赞