Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't connect to my mongo docker container by its name

I'm pretty new to docker but I'm having some issues getting a node app to connect to a mongo database running on a separate container.

I'm using the official mongo image

I run it using:

docker run --name some-mongo --network-alias some-mongo -d mongo

It's running on port 27017 by default. I can connect to it using the mongo shell:

mongo --host mongodb://172.17.0.2:27017

But I can't connect to it by name

mongo --host mongodb://some-mongo:27017

MongoDB shell version: 3.2.19
connecting to: mongodb://some-mongo:27017/test
2018-05-07T17:23:20.813-0400 I NETWORK  [thread1] getaddrinfo("some-mongo") failed: Name or service not known
2018-05-07T17:23:20.813-0400 E QUERY    [thread1] Error: couldn't initialize connection to host some-mongo, address is invalid :
connect@src/mongo/shell/mongo.js:223:14
@(connect):1:6
exception: connect failed

Instead I get an error message about how I can't connect to the mongo host:

I'm trying some docker-compose tutorials but either they're too simple or they don't seem to work for me. I just want to connect a custom node app, (not the official node) to mongodb and some other dependencies.

like image 939
obesechicken13 Avatar asked Oct 27 '25 03:10

obesechicken13


1 Answers

You have to run your container with passing ports to your host machine:

docker run -p 27017:27017 --name some-mongo --network-alias some-mongo -d mongo

Then you can connect to MongoDB from your host machine:

If you don't want to do this you can connect to mongo through docker container command

docker exec -it some-mongo mongo
like image 145
Alexander Oleynik Avatar answered Oct 28 '25 18:10

Alexander Oleynik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!