how can I connect to a docker from the outside hos

2019-05-06 13:04发布

I am new to dockers.

I have ran a docker container with mongo running it (using boot2docker)

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

inside the 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 is

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

now things get a little tricky since they require network knowledge.

how do I connect to it from out side?

tried:

  1. mongo localhost:27017

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

  3. mongo 0.0.0.0:27017

  4. mongo 10.0.0.12:27017
  5. mongo 192.168.3.124:27017

1条回答
神经病院院长
2楼-- · 2019-05-06 13:43

Native Docker: (LInux)

By exposing the port to your host via -p 27017:27017 you can access the MongoDB service via the IP Address of the Docker Host itself.

Say if your Docker Host is 192.168.0.1 you could use that IP Address.

See: binding container ports to the host and Connect using Network port mapping

non-Native Docker: (e.g: OS X/Windows)

Update: The above response is for Docker on Linux; If you are on OS X via something like boot2docker you will need to connect to the boot2docker Virtual Machine's IP Address which yhou can get from the boot2docker CLI:

$ boot2docker ip
192.168.59.103
查看更多
登录 后发表回答