'Connect mongodb docker container to localhost

I am trying to bind port of docker container that is mongodb in docker to localhost. I tried doing: sudo docker run -p 127.0.0.1:27018:27017 mongo:3.2

I could able to connect to it but the databases added in docker containers are not exposed to local.

I even tried of binding 0.0.0.0. and 192.168.x.x that is my ip address also instead of 127.0.0.1. In all cases i am not getting the databases in mongo docker container



Solution 1:[1]

If you are running the container and want to bind it to the localhost (assuming your hosts files on your host machine binds localhost to 127.0.0.1) then you can just type:

sudo docker run -p 27018:27017 --name mongo mongo:3.2

or if you want to use the 27017 port

sudo docker run -p 27017:27017 --name mongo mongo:3.2

If you want to login and don't have the mongo binaries in your host, you can type:

sudo docker container exec -it mongo mongo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jensen