Skip to content
Advertisement

Docker – Nginx, PHP, MongoDB – Laravel artisan migrate connection timeout

I have Laravel running in Docker with 6 containers – Nginx, MongoDB, PHP, Composer, Npm and Artisan.

Here is my docker-compose-yaml :

version: '3'

networks:
  laravel:

services:

  site:
    build:
      context: .
      dockerfile: nginx.dockerfile
    container_name: nginx
    ports:
      - 8080:80
    volumes:
      - ./src:/var/www/html:delegated
    depends_on:
      - php
      - mongodb
    networks:
      - laravel
    
  mongodb:
    image : mongo:latest
    container_name: mongodb
    restart: unless-stopped
    volumes: 
      - /data/db
    ports:
      - 27017:27017

  php:
    build:
      context: .
      dockerfile: php.dockerfile
    container_name: php
    volumes:
      - ./src:/var/www/html:delegated
    environment:
      PHP_INI_SCAN_DIR: "/usr/local/etc/php/custom.d:/usr/local/etc/php/conf.d"
    networks:
      - laravel

  composer:
    build:
      context: .
      dockerfile: composer.dockerfile
    container_name: composer
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    depends_on:
      - php
    user: laravel
    entrypoint: ['composer', '--ignore-platform-reqs']
    networks:
      - laravel

  npm:
    image: node:13.7
    container_name: npm
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    entrypoint: ['npm']
    networks:
      - laravel

  artisan:
    build:
      context: .
      dockerfile: php.dockerfile
    container_name: artisan
    volumes:
      - ./src:/var/www/html:delegated
    depends_on:
      - mongodb
    working_dir: /var/www/html
    user: laravel
    entrypoint: ['php', '/var/www/html/artisan']
    networks:
      - laravel

Here is my Laravel database config :

'mongodb' => [
        'driver' => 'mongodb',
        'host' => 'mongodb',
        'port' => 27017,
        'database' => env('MONGO_DB_DATABASE'),
        'username' => env('MONGO_DB_USERNAME'),
        'password' => env('MONGO_DB_PASSWORD'),
        'options' => [
            'database' => env('MONGO_DB_DATABASE') // sets the authentication database required by mongo 3
        ]
    ],

And here is my mongodb part in the .env file :

MONGO_DB_DATABASE=mongolara
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=

I want to run php artisan migrate, so I run this :

sudo docker-compose run --rm artisan migrate

But it give me this error :

MongoDBDriverExceptionConnectionTimeoutException 
No suitable servers found (`serverSelectionTryOnce` set): [Failed to resolve 'mongodb']

I’ve tried changing some configs like ports, host, .. but the error is still here. Does anyone have an idea ?

Advertisement

Answer

Has two solutions to that:

  1. Put the service mongodb on the same network of the others services.
  2. To use the exposed port on host, in to laravel configuration, do you need put 0.0.0.0 the host of mongodb.
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement