Reputation: 33
Currently I want to setup a Linux Server with MongoDB and a WebServer which saves stuff inside this DB. I need them both to run inside Docker containers.
My problem is that my WebServer doesnt seem to "see" the MongoDB from inside its container.
What ive tried so far is: run MongoDB with
docker run -p 27017:27017 -v... repoimage
it initializes fine and I can access this DB through the mongoshell without any problems. Also the Webserver can access the DB if it DOESNT run in a container but just on the host as java application.
When I try now to run the server through a container I get a java exception saying "Connection refused" (which is thrown when he cant open the socket)
(Important: the server accesses the mongodb through "localhost")
I thought that container networks are "linked" together. Does anyone have the same problem?
Upvotes: 1
Views: 1405
Reputation: 742
Just try to point the Webserver to the correct container IP. You can get the container IP by using this command
docker inspect <container-uid> | grep IPAddress
.
I believe the default IP for the first container that spins up is 172.17.0.2. This way your Webserver will be pointing to container where MongoDB is running instead of the localhost.
Upvotes: 0
Reputation: 51
Localhost will be resolved by the hosts file in the container, hence it won't point outside of the container to the host machines localhost. Run the container with --net="host" to resolve.
Also, I would suggest starting up the two containers with docker-compose to have their own network and refer to each other in their host files through their own hashes.
https://docs.docker.com/compose/networking/#configuring-the-default-network
Upvotes: 1