Search code examples
mongodbdockermongodb-compass

Docker MongoDB container not starting properly - connect ECONNREFUSED 127.0.0.1:27017


I'm new to Docker and am attempting to run a container using the mongo image and connect to it using MongoDBCompass.

The command I'm using to start the container, which was modified from the image documentation is:

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

This results in the container exiting straight away. I noticed this when trying to connect from MongoDBCompass gave the error message connect ECONNREFUSED 127.0.0.1:27017.

Thanks in advance if anyone can help me modify the run command to keep the container running and allow me to connect using MongoDBCompass.


Solution

  • On the docker run command, options are split in two groups.

    • options before the image name are docker options
    • options after the image name are sent as a command to the container

    You have -p 27017:27017 after the image name, so it's sent as a command to the container. Mongo doesn't understand that command, so it exits with an error. Place the port mapping options before the image name and it'll work. Like this

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