I\'m running a NodeJS
App with docker-compose. Everything works fine and I can see all my data by connecting to Mongo inside container. But when I connect to RoboMo
You should do a Robomongo SSH tunnel connection to MongoDB inside docker container. First of all you should install a ssh server inside your docker container.
https://docs.docker.com/engine/examples/running_ssh_service/
After that you should configure your connection in Robomongo. Inside "Connection Settings" there are configuration tabs of your Robomongo Connection.
Go to "SSH" Tab and configure your SSH connection to the docker container. After that go to "Connection" Tab and configure your connection to MongoDB as if it was in localhost scope.
First log in with ssh Login details
ssh -i yourpemfile.pem username@ipaddress
Check running container id for MongoDB
docker ps -a
then check the mongo container id
docker inspect container_id
create new connection and add container id use ssh login details to connect to mongodb
I was facing a different problem. I had installed MongoDB locally. So, when the MongoDB on docker was running, it was clashing with the one running on my host. I had installed it using brew
.
So, I ran
brew services stop mongodb-community
and then I restarted Robo3t. I saw the databases created in the MongoDB running on the docker.
Voila!
In your docker-compose file, you can expose a port to the host.
For example, the following code will expose port 27017 inside the machine to the port 27018 in the host.
app:
image: node
volumes:
- /app
ports:
- "27018:27017"
Then, if you have docker-machine installed and your machine is default, you can do in a terminal :
docker-machine ip default
It will give you the ip of your host, for example 192.168.2.3. The address of your database (host) will be 192.168.2.3 and the port 27018.
If your docker machine is not virtual and is your OS, the address of your database will be localhost and the port 27018.
The easiest way is to enable forwarding the Mongo Container itself, here's how my docker-compose looks like.
mongo:
image: mongo
restart: always
ports:
- 27017:27017
There is another way. You can
docker ps
should show you your MongoDB container.docker inspect <mongo container id>
will print out complete information about that container. Look for IPAddress
in the end, that will give you the local IP of the container.Another sidenote: Make sure that you don't expose your mongodb service ports in any way (neither Dockerfile nor docker-compose.yml), cause that will make your database openly accessible from everywhere. Assuming that you don't have set up a username / password for that service you will be scanned and hacked soon.