14

I am using the official mongodb docker container. I want to connect to the mongodb container from my host machine on port 27017. I ran the container with these ports exposed

-p 27017:27017

I am not able to connect (connection refused) and I believe its because the mongo conf file is not configured to allow remote connections. How can I configure it to allow? The official container does not have vi/nano installed to modify the image.

I am able to connect to mongodb from another container by creating a link - however this is not my wish

CraigH
  • 2,041
  • 3
  • 30
  • 48
  • You have to execute a **docker ps** and see if the 27017 port is forwarded to the Docker host for the container you're running the mongo image on. The entry has to look like this : **PORTS 0.0.0.0:27017->27017/tcp** . I ran this exact command and didn't have to modify anything in order for my local mongo shell to connect to the dockerized db : **docker run -p 27017:27017 -v /opt/mongodb/db:/data/db --name my-mongo-dev -d mongo** . Could you post the output of the **docker logs your_container_name** command ? – JJP Jun 06 '16 at 22:38
  • any update on this? – Hosni Jun 28 '16 at 00:47

1 Answers1

16

Better solutions for furthering:
https://blog.madisonhub.org/setting-up-a-mongodb-server-with-auth-on-docker/ https://docs.mongodb.com/v2.6/tutorial/add-user-administrator/

My answer to another question. How to enable authentication on MongoDB through Docker?

Here's what I did for the same problem, and it worked.

  1. Run the mongo docker instance on your server

    docker run -d -p 27017:27017 -v ~/dataMongo:/data/db mongo
    
  2. Open bash on the running docker instance.

    docker ps
    

    CONTAINER IDIMAGE COMMAND CREATED STATUS PORTS NAMES

    b07599e429fb mongo "docker-entrypoint..." 35 minutes ago Up 35 minutes 0.0.0.0:27017->27017/tcp musing_stallman

    docker exec -it b07599e429fb bash
    root@b07599e429fb:/#
    

    Reference- https://github.com/arunoda/meteor-up-legacy/wiki/Accessing-the-running-Mongodb-docker-container-from-command-line-on-EC2

  3. Enter the mongo shell by typing mongo.

    root@b07599e429fb:/# mongo
    
  4. For this example, I will set up a user named ian and give that user read & write access to the cool_db database.

    > use cool_db
    
    > db.createUser({
        user: 'ian',
        pwd: 'secretPassword',
        roles: [{ role: 'readWrite', db:'cool_db'}]
    })
    

    Reference: https://ianlondon.github.io/blog/mongodb-auth/ (First point only)

  5. Exit from mongod shell and bash.

  6. Now run the mongo docker with auth enabled.

    docker run -d -p 27017:27017 -v ~/dataMongo:/data/db mongo mongod --auth
    

    Reference: How to enable authentication on MongoDB through Docker? (Usman Ismail's answer to this question)

  7. I was able to connect to the instance running on a Google Cloud server from my local windows laptop using the below command.

    mongo <ip>:27017/cool_db -u ian -p secretPassword
    

    Reference: how can I connect to a remote mongo server from Mac OS terminal

  • 2
    Very good stuff and of course does work. Nevertheless imho it's REALLY worth to mention at the beginning that it does not work with the newest Mongo release (3.6 is the latest one currently, worked for 3.4). This is remarked [here](https://stackoverflow.com/a/43441619/1906088). It will help others with saving their expensive time. – pt12lol Dec 20 '17 at 09:46