Connection timeout in dockerized metabase

44 Views Asked by At

I have two different services on one server up and running with docker.

One service contains metabase with following docker-compose file.

  metabase:
    container_name: metabase
    image: metabase/metabase:latest
    ports:
      - "3102:3000"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - metabase-data:/metabase

And another service contains mongo db:

  mongodb:
    image: mongo:4.4
    restart: always
    ports:
      - ${MONGODB_PORT:-27017}:27017
    volumes:
      - mongodb-data:/data/db
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD}

I tried host.docker.internal , my server public address, localhost etc but nothing works. and always receive connection timeout.

Netcat test is sucessfull from my local machine

nc -v 212.xx.xxx.175 27017
Connection to 212.xx.xxx.175 27017 port [tcp/*] succeeded!

But tried these in metabase container and all of them ends up with 'operation timed out'

nc -v host.docker.internal 27017
nc: host.docker.internal (172.17.0.1:0): Operation timed out

I also can access my database from mongo compass app, (so im pretty sure it's not about firewall) but no chance when using metabase.

Update: Creating bridge network in one docker-compose and use it as external network in another, works. My main question is why it doesn't work with my public address or host.docker.internal?

1

There are 1 best solutions below

1
datawookie On

If both of the service exist in the same docker-compose.yml file then they can communicate via the Docker network using the service name as their DNS name.

version: "3"

services:
  metabase:
    container_name: metabase
    image: metabase/metabase:latest
    ports:
      - "3000:3000"
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - metabase-data:/metabase
  mongodb:
    container_name: mongodb
    image: mongo:4.4
    restart: always
    ports:
      - ${MONGODB_PORT:-27017}:27017
    volumes:
      - mongodb-data:/data/db
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USERNAME}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD}
volumes:
  metabase-data:
  mongodb-data:

Now from within the running Metabase container you can do:

nc -v mongodb 27017

and it will open a connection to the MongoDB container on port 27017.

Screenshot below shows:

  • LHS: nc connection to MongoDB container and
  • RHS: Metabase configuration for connecting to MongoDB (open http://127.0.0.1:3000 in browser for Metabase).

You will probably need to sort out SSL for the connection to the database, but that is another matter!

enter image description here