0

I'm running an Apache-PHP container and I also run a MongoDB container. Both containers are run fine. But when I connect MongoDB container from Apache-PHP container, there is an error. enter image description here

Here is my docker-compose file:

version: "3.1"

services:
  admin-server:
    build:
      context: ./php-apache
    dockerfile: Dockerfile  
    container_name: admin-server
    ports: 
      - 8080:80
    volumes: 
      - ./www/app1:/var/www/html
      - ./php-apache/apache.conf:/etc/apache2/sites-available/000-default.conf
    links: 
      - mongo-database
    networks:
      - development_network

  mongo-database:
    image: mongo:latest
    container_name: monog-database
    ports:
      - 27017:27017
    networks:
      - development_network
networks:
  development_network:

I'm running a laravel appliation and here is my mongodb connection:

'mongodb' => [
        'driver' => 'mongodb',
        'host' => '127.0.0.1',
        '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
        ]
    ],

1 Answer 1

1

According to your docker-compose.yaml file you can access you mongodb container on 127.0.0.1:27017 only from host machine.

Containers communicate with each other in development_network network, so you can access your mongodb container on mongo-database:27017 from admin-server container.

Just change laravel application's configuration:

'mongodb' => [
        'driver' => 'mongodb',
        'host' => 'mongo-database',
        '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
        ]
    ],
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much @Nickolay, it's fully working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.