简体   繁体   English

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

[英]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. I have Laravel running in Docker with 6 containers - Nginx, MongoDB, PHP, Composer, Npm and Artisan.

Here is my docker-compose-yaml:这是我的 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:这是我的 Laravel 数据库配置:

'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:这是 .env 文件中我的 mongodb 部分:

MONGO_DB_DATABASE=mongolara
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=

I want to run php artisan migrate, so I run this:我想运行 php artisan migrate,所以我运行这个:

sudo docker-compose run --rm artisan migrate

But it give me this error:但它给了我这个错误:

MongoDB\Driver\Exception\ConnectionTimeoutException 
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?有人有想法吗?

Has two solutions to that:对此有两种解决方案:

  1. Put the service mongodb on the same network of the others services.将服务mongodb放在其他服务的同一网络上。
  2. To use the exposed port on host, in to laravel configuration, do you need put 0.0.0.0 the host of mongodb.要使用主机上的暴露端口,在 laravel 配置中,是否需要将 mongodb 的主机设置为0.0.0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM