11

I've build a docker container running a mongodb-instance, that should be exposed to the host. However, when i want to connect from the host into the mongodb-container, the connection will be denied.

This is my Dockerfile:

FROM mongo:latest

RUN  mkdir -p /var/lib/mongodb && \
     touch /var/lib/mongodb/.keep && \
     chown -R mongodb:mongodb /var/lib/mongodb

ADD mongodb.conf /etc/mongodb.conf

VOLUME [ "/var/lib/mongodb" ]

EXPOSE 27017

USER mongodb
WORKDIR /var/lib/mongodb

ENTRYPOINT ["/usr/bin/mongod", "--config", "/etc/mongodb.conf"]
CMD ["--quiet"]

/etc/mongodb.conf:

And this is the config-file for MongoDB, where i bind the IP 0.0.0.0 explicitly as found here on SO, that 127.0.0.1 could be the root cause of my issue (but it isn't)

systemLog:
  destination: file
  path: /var/log/mongodb/mongo.log
  logAppend: true
storage:
  dbPath: /var/lib/mongodb
net:
  bindIp: 0.0.0.0

The docker container is running, but a connection from the host is not possible:

host$ docker run -p 27017:27017 -d --name mongodb-test mongodb-image
host$ docker ps
$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
6ec958034a6f        mongodb-image       "/usr/bin/mongod --co"   4 seconds ago       Up 3 seconds        0.0.0.0:27017->27017/tcp   mongodb-test

Find the IP-Address:

host$ docker inspect 6ec958034a6f |grep IPA
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAMConfig": null,
                    "IPAddress": "172.17.0.2",

Try to connect:

host$ mongo 172.17.0.2:27017
MongoDB shell version v3.4.0
connecting to: mongodb://172.17.0.2:27017
2016-12-16T15:53:40.318+0100 W NETWORK  [main] Failed to connect to 172.17.0.2:27017 after 5000 milliseconds, giving up.
2016-12-16T15:53:40.318+0100 E QUERY    [main] Error: couldn't connect to server 172.17.0.2:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:234:13
@(connect):1:6
exception: connect failed

When i ssh into the container, i can connect to mongo and list the test database successfully.

Community
  • 1
  • 1
delete
  • 18,144
  • 15
  • 48
  • 79
  • 3
    You specified the mapped port as 28001, why are you connecting to port 27017? – mustaccio Dec 16 '16 at 15:04
  • 3
    Try `localhost:28001` in a browser on your host – user2915097 Dec 16 '16 at 15:11
  • 1
    28001 was a copy-paste-error from another try. i replaced it with 27017 in the question. And the issue remains the same btw. thx for the hint @mustaccio – delete Dec 16 '16 at 15:18
  • 2
    ah, i got it. Because of the EXPOSE i have to connect to localhost instead of the IP of the container. THis way it works fine. Thx user2915097! – delete Dec 16 '16 at 15:20
  • check https://stackoverflow.com/questions/17588876/mongodb-conf-bind-ip-127-0-0-1-does-not-work-but-0-0-0-0-works/34698336#34698336 – OWADVL Dec 17 '17 at 14:54
  • use **localhost** or **IP of node** instead of docker IP. – ROHIT KHURANA Nov 11 '20 at 06:05

2 Answers2

3

Use host.docker.internal with exposed port : host.docker.internal:27017

Alfonso Tienda
  • 3,442
  • 1
  • 19
  • 34
Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53
1

Using localhost instead of the ip, allows the connection.

Combine it with the exposed port: localhost:27017

I tested the solution as it was stated in the comments, and it works.

Cullen Bond
  • 382
  • 1
  • 5