Reputation: 1315
I setup docker compose to locally test my app and it seems that the app can't access the mongo instance even though I'm able to on Studio 3T.
this is what my docker-compose file looks like:
services:
web:
image: php-app:latest
ports:
- '80:80'
depends_on:
- db
networks:
- code-network
db:
container_name: mongodb_4.4
image: mongo:4.4
environment:
MONGO_INITDB_ROOT_USERNAME: demo
MONGO_INITDB_ROOT_PASSWORD: secret
ports:
- '27017:27017'
networks:
- code-network
volumes:
- mongodb_data_container:/data/db
volumes:
mongodb_data_container: {}
networks:
code-network:
driver: bridge
this is what shows up in the docker console:
PHP Fatal error: Uncaught MongoDB\\Driver\\Exception\\ConnectionTimeoutException: No suitable servers found (`serverSelectionTryOnce` set): [connection refused calling ismaster on '127.0.0.1:27017'] in /var/www/html/vendor/mongodb/mongodb/src/functions.php:431
Stack trace:
#0 /var/www/html/vendor/mongodb/mongodb/src/functions.php(431): MongoDB\\Driver\\Manager->selectServer(Object(MongoDB\\Driver\\ReadPreference))
#1 /var/www/html/vendor/mongodb/mongodb/src/Collection.php(230): MongoDB\\select_server(Object(MongoDB\\Driver\\Manager), Array)
#2 /var/www/html/index.php(12): MongoDB\\Collection->aggregate(Array)
#3 {main}\n thrown in /var/www/html/vendor/mongodb/mongodb/src/functions.php on line 431
I've tried adjusting my php code to connect using localhost and 127.0.0.1 and neither worked.
Upvotes: 1
Views: 567
Reputation: 96
It seems you did not configure your MongoDB client yet, the default DSN is mongodb://127.0.0.1/
(the local host), and it should be mongodb://db/
(the db service you defined) in your case.
Read more about the constructor for MongoDB client:
Upvotes: 2