9

Firstly I pulled mongoDb image

sudo docker pull mongo

and run docker mongodb container:

sudo docker run --name database -d -p 27017:27017 mongo --noauth --bind_ip=0.0.0.0

I see that my docker container works:

300b779a63a9        mongo               "docker-entrypoint..."   30 minutes ago      Up 30 minutes  

and now Im trying to connect to it with my docker app

sudo docker run -v ~/Desktop/Template/universal_skillskill -it --link 300b779a63a9:300b779a63a9 alexa

and... Im getting a message that the connection is refused :(

Uncaught exception: MongoError: failed to connect to server [0.0.0:27017] on first connect [MongoError: connect ECONNREFUSED 0.0.0.0:27017]
MongoError: failed to connect to server [0.0.0:27017] on first connect [MongoError: connect ECONNREFUSED 0.0.0.0:27017]

I checked and my app works with mongodb installed locally but it doesnt communicate with docker mongoDb container :( What am I doing wrong? Could somebody smart help me please :)

3 Answers 3

17

You need to connect to 300b779a63a9:27017 as this is the linked name that you gave for the mongodb.

Sign up to request clarification or add additional context in comments.

2 Comments

like this in my container webapp mongoose.connect('mongodb://:300b779a63a9:27017');
mongo:27017 worked for me
8

Adding to previous answers if you are starting your container with these additional parameter for authentication

- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password

then use authSource=admin parameter as well only passing credentials wouldn't work.

mongodb://<user>:<password>@<container_name>:27017/$<DB>?authSource=admin

I faced this issue. thought, this might help someone.

Comments

6

try to connect with the name of your container database and not with IP.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.