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:
- The fact that you are mixing
--port
and --network host
- The directory you are mounting to is NFS or otherwise non standard (are you using Vagrant?).
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