Reputation: 407
I was trying to build a mongo container from mongo image the mongo server didnot start
The commands i used were
docker pull mongo
docker run -d -p 27017:27017 -v $PWD/mongodata:/data/db --network host --name mongodb mongo
Then i tested it by entering the container by using
docker exec -it mongodb bash
then when i type "mongo" in the terminal of the container it showed that it was not able to connect to server
Any ideas on what might have went wrong
Upvotes: 1
Views: 1231
Reputation: 14776
In order to debug something like this, run it without -d
to see the errors clearly in the standard output.
Your problem is most likely one of two:
--port
and --network host
Try this first (without a volume), see if it works:
$ docker run --rm -p 27017:27017 --name mongodb mongo
If it does, add the the volume, but this time, use a different directory, for example:
$ docker run --rm -p 27017:27017 -v /tmp/mongodata:/data/db --name mongodb mongo
Also note that I have removed the --network host
- you should not mix port exposure and host network. Choose one or the other.
After this, before even trying to connect, confirm it is running:
$ docker ps
And finally, connect to it either from the host or from within the container:
$ docker exec -it mongodb mongo
Finally, as another way of experimenting with any docker stack, consider using docker compose. Here is one for your mongo experiments:
version: '3'
services:
server:
image: mongo
ports: ["27017:27017"]
client:
image: mongo
depends_on: [server]
entrypoint: mongo --host server
And now you can just run:
$ docker-compose run --rm client
Upvotes: 1