0

I am completely new to docker. I followed this instruction to install mongodb chart and docker.

When I connect to 172.17.0.1, it said

Unable to connect to MongoDB using the specified URI.

The following error was returned while attempting to connect: 
MongoNetworkError: failed to connect to server [172.17.0.1:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 172.17.0.1:27017]

The result from pinging the specified server "172.17.0.1" from within the container is:
PING 172.17.0.1 (172.17.0.1) 56(84) bytes of data.
64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.050 ms

--- 172.17.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.050/0.050/0.050/0.000 ms

The mongodb is running on local machine. I think it is not running in the container (not sure) because I installed mongodb in my machine before I install docker.

I have also checked the setting by using docker network inspect bridge

"Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]

This is the yml file:

version: "3.3"

services:
  charts:
    image: quay.io/mongodb/charts:v0.10.0
    hostname: charts
    ports:
      # host:container port mapping. If you want MongoDB Charts to be
      # reachable on a different port on the docker host, change this
      # to <port>:80, e.g. 8888:80.
      - 80:80
      - 443:443
    volumes:
      - keys:/mongodb-charts/volumes/keys
      - logs:/mongodb-charts/volumes/logs
      - db-certs:/mongodb-charts/volumes/db-certs
      - web-certs:/mongodb-charts/volumes/web-certs
    environment:
      # The presence of following 2 environment variables will enable HTTPS on Charts server.
      # All HTTP requests will be redirected to HTTPS as well.
      # To enable HTTPS, upload your certificate and key file to the web-certs volume,
      # uncomment the following lines and replace with the names of your certificate and key file.
      # CHARTS_HTTPS_CERTIFICATE_FILE: charts-https.crt
      # CHARTS_HTTPS_CERTIFICATE_KEY_FILE: charts-https.key

      # This environment variable controls the built-in support widget and
      # metrics collection in MongoDB Charts. To disable both, set the value
      # to "off". The default is "on".
      CHARTS_SUPPORT_WIDGET_AND_METRICS: "on"
      # Directory where you can upload SSL certificates (.pem format) which
      # should be considered trusted self-signed or root certificates when
      # Charts is accessing MongoDB servers with ?ssl=true
      SSL_CERT_DIR: /mongodb-charts/volumes/db-certs
    networks:
      - backend
    secrets:
      - charts-mongodb-uri

networks:
  backend:

volumes:
  keys:
  logs:
  db-certs:
  web-certs:

secrets:
  charts-mongodb-uri:
    external: true

How can I connect to the mongodb?

JOHN
  • 1,411
  • 3
  • 21
  • 41
  • 1
    Where is MongoDB running? (Directly on the host?) Where are you trying to connect from? (Some other Docker container?) How did you start it; can you share your `docker run` command or `docker-compose.yml` file? – David Maze Nov 10 '18 at 16:48
  • @DavidMaze updated. Thanks! – JOHN Nov 11 '18 at 00:23

2 Answers2

1

By default mongodb is configured to only accept results from localhost 127.0.0.1 and when charts image connects to it via docker it is seen as an external connection coming from docker0 and it is hence rejected by mongod [MongoNetworkError: connect ECONNREFUSED 172.17.0.1:27017]

To fix this, edit the mongo config sudo vim /etc/mongod.conf and add your docker0 ip to the bindIp config the line with bindIp: 127.0.0.1 should be changed to bindIp: 127.0.0.1,172.17.0.1 for default docker installations.

This may be an old question but I think it could be a common issue, I had to struggle with this for a while before actually reading the error message more thoroughly and realising it really a simple issue.

Another issue is that the upon first install you can connect to mongo without a username or password, so those two should be deleted from the uri if you had not configured security, making it mongodb://172.17.0.1:27017.

0

Assuming you know how to use echo "mongodb://<username>:<password>@myhost.com/" | docker secret create charts-mongodb-uri - to create the connection the url.

The problem is actually how to connect from a docker container to a outside service running on the host machine. You can use some help from plenty of questions like From inside of a Docker container, how do I connect to the localhost of the machine?

Basically, if you are using docker for mac or windows, use something like echo "mongodb://host.docker.internal" | docker secret create charts-mongodb-uri -, for linux see https://docs.mongodb.com/charts/master/installation/ section RUNNING METADATA DATABASE ON LOCALHOST for the doc, or just use host mode (remove ports section)

version: "3.3"
services:
  charts:
    image: quay.io/mongodb/charts:v0.10.0
    hostname: charts
    network_mode: "host"
    ...
Siyu
  • 11,187
  • 4
  • 43
  • 55
  • I have tried: docker run --rm quay.io/mongodb/charts:v0.10.0 charts-cli test-connection mongodb://172.17.0.1 in linux but not working. – JOHN Nov 11 '18 at 02:26
  • 3
    I need to set bindIP = 0.0.0.0 – JOHN Nov 11 '18 at 09:32
  • 1
    https://askubuntu.com/questions/941816/permission-denied-when-running-docker-after-installing-it-as-a-snap I also needed to do sudo chmod a+rwx /var/run/docker.sock sudo chmod a+rwx /var/run/docker.pid to get permission – JOHN Nov 11 '18 at 09:39