Docker Community Forums

Share and learn in the Docker community.

How to access host machine's mongo db inside docker container

I am a bit new to docker. I am developing a laravel application, I have my app container running.
Inside app container I want to connect to my locally hosted mongo db server, which is generally localhost:27017.

This is my docker-compose.yml file

version: '3'
services:

  #PHP Service
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: digitalocean.com/php
    container_name: bns_app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www
    volumes:
      - ./:/var/www
      - ./docker_files/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
      - app-network

  #Nginx Service
  webserver:
    image: nginx:alpine
    container_name: bns_web
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./:/var/www
      - ./docker_files/nginx/conf.d/:/etc/nginx/conf.d/
    networks:
      - app-network

  #MySQL Service
  db:
    image: mysql:5.7.22
    container_name: bns_db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_ROOT_PASSWORD: 12345
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - dbdata:/var/lib/mysql/
      - ./docker_files/mysql/my.cnf:/etc/mysql/my.cnf
    networks:
      - app-network

  #REDIS Service
  redis:
      build:
        context: ./docker_files/redis
        dockerfile: Dockerfile
      container_name: bns_redis
      volumes:
        - ./docker_files/redis:/data
      ports:
        - "6379:6379"
      networks:
        - app-network

  # Laravel Echo Server
  laravel-echo-server:
        build:
          context: ./docker_files/laravel-echo-server
          dockerfile: Dockerfile
        container_name: bns_echo_server

        volumes:
          - ./laravel-echo-server.json:/var/www/laravel-echo-server.json:ro
        ports:
          - "6001:6001"
        links:
          - redis
        networks:
          - app-network

  # PHP-WORKER
  php-worker:
    build:
      context: ./docker_files/php-worker
      dockerfile: Dockerfile
    container_name: bns_worker
    volumes:
      - ./php-worker/supervisord.d:/etc/supervisord.d
    depends_on:
      - app
    networks:
      - app-network


#Docker Networks
networks:
  app-network:
    driver: bridge
#Volumes
volumes:
  dbdata:
    driver: local

So inside my app container when I run artisan commands such as docker-compose exec app php artisan migrate:fresh --seed mysql tables are migrated successfully but mongo db (which is not in any container but it is in host machine only) documents are not migrate/seeded and I get error :

No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on '172.18.0.3:27017']

  at /var/www/vendor/mongodb/mongodb/src/functions.php:431
    427|         // TODO: PHPLIB-476: Read transaction read preference once PHPC-1439 is implemented
    428|         $readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
    429|     }
    430| 
  > 431|     return $manager->selectServer($readPreference);
    432| }
    433| 

  Exception trace:

  1   MongoDB\Driver\Manager::selectServer(Object(MongoDB\Driver\ReadPreference))
      /var/www/vendor/mongodb/mongodb/src/functions.php:431

  2   MongoDB\select_server(Object(MongoDB\Driver\Manager), [])
      /var/www/vendor/mongodb/mongodb/src/Database.php:419

  Please use the argument -v to see more details.

This is my ENV file:

MONGO_DATABASE=exchange
MONGO_HOST=172.18.0.3 # << This is the IP address of my app container  <<
MONGO_PORT=27017
MONGO_USERNAME=
MONGO_PASSWORD=

Please do help me. Where am I going wrong ?

I don’t know how but using static ip as 172.17.0.1 worked for me.

Now my ENV file is:

MONGO_DATABASE=exchange
MONGO_HOST=172.17.0.1
MONGO_PORT=27017
MONGO_USERNAME=
MONGO_PASSWORD=

Got reference from here

It must be well documented.