Connecting to Mongo Docker container from Mongo Compass on local machine? - mongodb

I have the following docker compose file that I use to spin up a mongo docker container which works as intended.
However, I cannot seem to connect to the container from compass on my local machine.
I executed docker inspect on my mongo container and got its IP and tried to connect using compass, but it didn't work - connection timed out.
Is it the IP of my docker network or the IP of the mongo container I need?
docker compose file:
version: "3"
services:
pokerStats:
image: pokerStats
container_name: pokerStats
ports:
- 8080:8080
depends_on:
- db
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27017:27017"

I was able to get this working using the top answer on this question.
I had to change my docker-compose file to expose port 27018 on my local:
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27018:27017"
And my connection details on Compass to be:

You are mapping port 27017 from your host machine to port 27017 on the container. So the following connection string should work mongodb://localhost:27017

Related

My mongo db is not able to connect to my node js application

I am a beginner with docker and I am trying to set up a docker compose file to bring up an application and that application uses mongo db. This is my docker compose file.
version: "3.7"
services:
iap:
network_mode: host
image: myrepo/myapp:v1
volumes:
- "appdata:/opt/myapp"
ports:
- "xxx:xxx" # Web UI (TCP)
depends_on:
- mongo
mongo:
network_mode: host
image: myregistry/mongo:4.2
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
ports:
- "27017:27017"
volumes:
- mongodata:/data/db
- "$PWD/seed-data:/docker-entrypoint-initdb.d/"
command: mongod
when I do docker compose up, this is the error that I see
(node:34) UnhandledPromiseRejectionWarning: MongoServerSelectionError: getaddrinfo ENOTFOUND mongo
I also have an entry point script
`db.auth('admin', 'admin')
db = db.getSiblingDB('pronghorn')
db.createUser({
user: 'test',
pwd: 'test',
roles: [
{
role: 'dbOwner',
db: 'test'
}
]
});
`
I do not have a lot of experience with docker and I want to know what am I doing wrong here?
Please can someone help?
Error you are getting is connectivity error, so I guess it's wrong port or host, double check that and look at this for more info about docker mongo setup.
Try to set ports without ""
ports:
27017:27017
if you use docker, at config nodejs MONGO_HOST is 'mongo' (name service)

Unable to connect to the mongodb instance running in a docker container from outside

The following code snippet is a part of my "docker compose" file and as you see, the internal port of 27017 was mapped to 37017 to prevent colliding with the MongoDB instance running on the host development machine:
version: '3.4'
services:
mongo:
image: mongo
container_name: mongodb
restart: always
ports:
- 27017:37017
I use Compass when trying to connect to this mongo instance in docker and the following connection string, but Compass fails to connect to the database:
mongodb://host.docker.internal:37017
What is missing in this configuration keeping me from connecting to the mongodb in docker?
The credit to this answer goes to David Maze. Switching the port numbers addressed the problem. The correct yaml looks like below:
version: '3.4'
services:
mongo:
image: mongo
container_name: mongodb
restart: always
ports:
- 37017:27017

Exception opening socket exception when trying to connect docker container to mongodb

I have a spring boot application which connects to a Mongo database. I have created a docker-compose file. The spring boot application has two instances. First instance runs on 8080 and 27017, which works perfectly fine. Now the second instance runs on 8083 and 27018. I can easily connect to 27017 and 27018 through Mongo GUI. However, when I run docker-compose up for the second instance, the spring boot gives the exception.
Following are my docker-compose files:
First Instance(docker-compose.yml):
version: '3'
services:
app:
container_name: HR-BACKEND
restart: always
build: .
ports:
- "8081:8080" #VF Webservice
links:
- mongo
mongo:
container_name: MONGOHR
image: mongo:4.0.2
ports:
- "27017:27017"
volumes:
- /data/hrdb:/data/db
First Instance (application.properties)
spring.data.mongodb.uri=mongodb://mongo:27017/tsp
Second Instance(docker-compose.yml):
version: '3'
services:
app:
container_name: VF-BACKEND
restart: always
build: .
ports:
- "8083:8080" #VF Webservice
links:
- mongovf
mongovf:
container_name: MONGOVF
image: mongo:4.0.2
ports:
- "27018:27017"
volumes:
- /data/hrdb:/data/db
Second Instance (application.properties)
spring.data.mongodb.uri=mongodb://mongovf:27018/tsp
Docker version:
Docker version 18.09.1, build 4c52b90
. I could not really find a solution in SO. Please let me know if more details are needed
In your second instance you are trying to connect to mongo on the wrong port. application.property should be:
spring.data.mongodb.uri=mongodb://mongovf:27017/tsp
In a docker compose context, you connect directly to containers without going through the docker host. This is why you have to connect directly to the container port instead of trying to connect through the port published on the docker host.

Go mgo.v2 package error: no reachable servers

Trying to use the mgo.v2 package to connect to the mongodb server. I have started the server using:
mongod --auth
I am able to connect to the server using the terminal using:
$ mongo -u "username" -p "password" --authenticationDatabase "db"
But when I use:
mgo.Dial("mongodb://usernamer:password#127.0.0.1:27017/dbname")
It gives me an error saying {"error":"no reachable servers"}.
My docker-compose.yml file is as below
version: "2"
services:
todo:
build:
context: .
dockerfile: todo/Dockerfile
restart: always
volumes:
- .:/go/src/prac
container_name: todo
ports:
- 8800:8081
mongodb:
command: mongod --auth
container_name: mongodb
image: mongo:latest
ports:
- 27017:27017
The problem appears to be that you are trying to connect to 127.0.0.1. MongoDB is not in the same container, so this won't work.
mgo.Dial("mongodb://usernamer:password#127.0.0.1:27017/dbname")
You should instead be connecting to the MongoDB container you defined by using the name you chose.
mgo.Dial("mongodb://usernamer:password#mongodb:27017/dbname")
Docker Compose creates a network for your containers in which they can access each other using the names you have defined as hostnames. Note that you don't need to define ports for containers to reach each other; these are only needed to reach containers from outside Docker.

docker run mongo image on a different port

The short question is can I run mongo from mongo:latest image on a different port than 27017 (for example on 27018?)
If yes, how can I do this inside a docker-compose.yml file in order ro be able to type the following command:
docker-compose run
The longer story:
I have an app running in AWS EC2 instance. The app consists of a mongodb and a web application. Now I decided to separate part of this app into its own microservice running in the same AWS inside docker container (two containers one for another mongo and one for a web app). I think the problem is I can not have mongodb running on port 27017 and at the same time another mongodb running inside a docker container on port 27017. Right? I have this assumption because when I stop the first mongo (my app mongo), my docker mongo works.
So I am trying to make the second mongo (the one that is inside the docker container), to run in a different port and my second web app (the one inside another docker conianter), to listen to mongo on a different port. Here is my attempt to change the docker-compose file:
version: '2'
services:
webapp:
image: myimage
ports:
- 3000:3000
mongo:
image: mongo:latest
ports:
- 27018:27018
And inside my new app, I changed the mongo url to:
monog_url = 'mongodb://mongo:27018'
client = MongoClient(monog_url, 27018)
Well, the same if I say:
monog_url = 'mongodb://mongo:27018'
client = MongoClient(monog_url)
But when I run docker-compose run, it still does not work, and I get the following errors:
ERROR: for mongo driver failed programming external
connectivity on endpoint: Error starting userland proxy:
listen tcp 0.0.0.0:27017: bind: address already in use
Or
pymongo.errors.ServerSelectionTimeoutError:
mongo:27018: [Errno -2] Name or service not known
You can tell MongoDB to listen on a different port in the configuration file, or by using a command-line parameter:
services:
mongo:
image: 'mongo:latest'
command: mongod --port 27018
ports:
- '27018:27018'
You can run processes inside a container and outside on the same port. You can even run multiple containers using the same port internally. What you can't do is map the one port from the host to a container. Or in your case, map a port that is already in use to a container.
For example, this would work on your host:
services:
webapp:
image: myimage
ports:
- '3000:3000'
mongo:
image: 'mongo:latest'
ports:
- '27018:27017'
mongo2:
image: mongo:latest
ports:
- '27019:27017'
The host mongo listens on 27017. The host also maps ports 27018 and 27019 to the container mongo instances, both listening on 27017 inside the container.
Each containers has its own network namespace and has no concept of what is running in another container or on the host.
Networks
The webapp needs to be able to connect to the mongo containers internal port. You can do this over a container network which allows connections between the container and also name resolution for each service
services:
webapp:
image: myimage
ports:
- '3000:3000'
networks:
- myapp
depends_on:
- mongo
mongo:
image: 'mongo:latest'
ports:
- '27018:27017'
networks:
- myapp
networks:
myapp:
driver: bridge
From your app the url mongo://mongo:27017 will then work.
From your host need to use the mapped port and an address on the host, which is normally localhost: mongo://localhost:27018
Default communication between different containers running on the same host
I solved the problem, not by running the container in a different port though, but by learning one new feature in docker-compose version 2 and that is we do not need to specify links or networks. The newly created containers by default will be part of docker0 network and hence they can communicate with each other.
As Matt mentioned, we can run processes inside containers on the same port. They should be isolated. So the problem can not be that the docker container and the host are using the same port. The problem is perhaps there is an attempt to forward a used port in host to another port in container.
Below is a working docker-compose file:
version: '2'
services:
webapp:
image: myimage
ports:
- 3000:3000
mongo:
image: mongo:latest
I looked at the mongo:latest docker file in github, and realized they exposed 27017. So we do not need to change the port or forward host ports to the running mongo container. And the mongo url can stay on the same port:
monog_url = 'mongodb://mongo:27017'
client = MongoClient(monog_url, 27017)
Docker run an image on a different port
So the above solution solved the problem, but as for the question title 'docker run mongo image on a different port', the simplest way is just to change the docker-compose to:
version: '2'
services:
web:
image: myimage
mongo:
image: mongo:latest
command: mongod --port 27018
Mongo is now running on 27018 and the following url is still accessible inside the web:
monog_url = 'mongodb://mongo:27018'
client = MongoClient(monog_url, 27018)
I had the same problem
I changed ports in my docker-compose.yml file and this work for me
In doing so, i don't change port in my connection string
mongo:
image: mongo
ports:
- "27018:27018"