Docker compose can't resolve hostname in local network
To Reproduce
Here is a docker compose app which only try to connect to a mongo database:
services:
mongosh:
image: alpine/mongosh:latest
command: sh -c "mongosh 'mongodb://mongo' && tail -f /dev/null"
depends_on:
- mongo
mongo:
image: mongo:latest
ports:
- '27018:27017'
command:
- --storageEngine=wiredTiger
volumes:
- data:/data/db
logging:
driver: none
volumes:
data:
node_modules:
Current vs. Expected behavior
Current behavour The mongosh app can't connect to the mongo database.
Connecting to: mongodb://mongo/?directConnection=true&appName=mongosh+2.0.2
MongoServerSelectionError: getaddrinfo EAI_AGAIN mongo
I tried pinging the mongo hostname in the docker container and it couldn't resolve it:
> docker exec -it test-mongonet-qbhbhx-node-1 ping mongo
ping: bad address 'mongo'
Expected behaviour The mongosh app should connect to the mongo database.
Provide environment information
Operating system: Ubuntu 25.04
Dokploy version: 0.25.11
Services deployed: mongo and mongosh apps via docker compsoe
Which area(s) are affected? (Select all that apply)
Docker Compose
Are you deploying the applications where Dokploy is installed or on a remote server?
Same server where Dokploy is installed
Additional context
The following issue may be related: https://github.com/Dokploy/dokploy/issues/2558
The only workaround I could find was to use the external deploy-network network. But it forces to use app specific hostnames for the database to prevent conflict:
services:
mongosh:
image: alpine/mongosh:latest
command: sh -c "mongosh 'mongodb://mongonet-mongo' && tail -f /dev/null"
depends_on:
- mongo
networks:
- dokploy-network
mongo:
image: mongo:latest
hostname: mongonet-mongo
ports:
- '27018:27017'
command:
- --storageEngine=wiredTiger
volumes:
- data:/data/db
logging:
driver: none
networks:
- dokploy-network
volumes:
data:
node_modules:
networks:
dokploy-network:
external: true
Connecting to: mongodb://mongonet-mongo/?directConnection=true&appName=mongosh+2.0.2
Using MongoDB: 8.2.2
Using Mongosh: 2.0.2
Will you send a PR to fix it?
No
I see they are connected and working fine (Using isolated deployment and not using it)
Thanks for your response. I installed dokploy lcoally on my mac and can confirm that my reproduction code does work. Mongosh was able to connect to mongo. I suppose my issue comes then from my VPS config. I am on OVHCloud. After some research I wasn't able to pin-point the exact issue. I'll stick with the fix suggested in my original post. I'll close the topic as it has nothing to do with dokploy.