0

I'm running an instance of a web application in my Docker container and am also running a MongoDB container so when I launch the web app I can easily connect to the DB on the app's connection page.

The issue is that I'm not sure how to reach the Mongo container from my web app and am not sure if my host/port connection info is correct.

My Docker Setup

As you can see the container is up and running with both mongo and web app services running without errors

enter image description here

I build the two through docker-compose.yml

version: "3.3"

services:
  web:
    image: grafana-asw-v3
    container_name: grafana-asw-v3
    restart: always
    build: .
    ports:
      - "13000:3000"
    volumes:
      - grafana-storage:/var/lib/grafana
    stdin_open: true
    tty: true
  db:
    container_name: mongo
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - grafana-mongo-db:/var/lib/mongo
    ports:
      - "27018:27017"

volumes:
  grafana-mongo-db: {}
  grafana-storage: {}

Issue

With everything up and running I'm attempting to connect through the web app, but I seem to be using the wrong connection info...

I assumed to use "hostMachine:port" (roxane:27018), but it's not connecting. Is there something I overlooked here?

enter image description here

cramos24
  • 29
  • 8
  • 1
    Is the `web` container your application? You should be able to use the database Compose service name `db` and the standard MongoDB port 27017 (`ports:` are completely ignored). See also [Networking in Compose](https://docs.docker.com/compose/networking/) in the Docker documentation. – David Maze Oct 20 '21 at 17:56

2 Answers2

0

There were two changes I had to make to fix this issue:

  1. Modify the bind_ip in mongod.conf via making this change to my docker-compose file
db:
    container_name: mongo
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - grafana-mongo-db:/var/lib/mongo
    ports:
      - "27018:27017"
    command: mongod --bind_ip 0.0.0.0
  1. I needed to refer to the IP address instead of the hostname in the cli in my we application. (Thanks to this answer for help with this one)
cramos24
  • 29
  • 8
0

Short answer

  • db service is in the same network than web service not in host network.
  • As you named your services via container_name you shoud be able to use the connection string mongodb://mongo:27017

Explanation

  • By default, docker containers run under a bridge network allowing them to communicate without viewing your host network.
  • When using ports in a compose file, you define that you want to map an internal port of the container to the host port
  • "27018:27017" => I want to expose the container port number 27017 to the host port number 27018.

As a result, you could expose your web frontend without exposing your mongo service :

version: "3.3"

services:
  web:
    image: grafana-asw-v3
    container_name: grafana-asw-v3
    restart: always
    build: .
    ports:
      - "13000:3000"
    volumes:
      - grafana-storage:/var/lib/grafana
    stdin_open: true
    tty: true
  db:
    container_name: mongo
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - grafana-mongo-db:/var/lib/mongo

volumes:
  grafana-mongo-db: {}
  grafana-storage: {}
Etienne Dijon
  • 1,093
  • 5
  • 10