Connect to mongodb running inside Docker

2019-07-30 00:11发布

问题:

I am using Windows 10

I have installed Virtual Box

In virtual Box I have installed Ubuntu with network setting as Bridge Network

I am using Docker Toolbox for Windows to Start Docker

In Docker I have Mongodb container running at default port

I am doing all my development stuff inside Ubuntu installed in Virtual Box

Here is my Terminal Log

$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v17.06.0-ce


$ docker-machine env
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="C:\Users\test\.docker\machine\machines\default"
export DOCKER_MACHINE_NAME="default"
export COMPOSE_CONVERT_WINDOWS_PATHS="true"
# Run this command to configure your shell:
# eval $("C:\Program Files\Docker Toolbox\docker-machine.exe" env)


$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
77427d4fc49b        mongo               "/bin/sh -c '/usr/..."   About an hour ago   Up 45 minutes                           mongo

Issue is I dont know how to Connect to this container running MongoDB globally say from Ubuntu running on Virtual Box

Edit 1:

Terminal Log from Ubuntu

test@test-VirtualBox:~$ ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:e1:c8:be brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.27/24 brd 192.168.1.255 scope global dynamic enp0s3
       valid_lft 78034sec preferred_lft 78034sec
    inet6 fe80::2ae9:9e0c:d9a3:f044/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:3d:79:a8:7f brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever

回答1:

You need to run your mongo container with port mappings

docker run -p 27017:27017 mongodb

And then you can access using 192.168.99.100:27017.

Edit-1

If you are connect it from another VM then make sure to add an adapter with common Host-only network lan. In most cases this would vboxnet1

And configure the network as below for your Ubuntu VM (make sure both docker VM and Ubuntu VM use the same)