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.
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
]
],
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
]
],
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.