Reputation: 4366
I have built a docker-compose with 3 services: db
, nginx
, selenium
.
App hosted with nginx communicates with database (nginx
<-> db
), and selenium tests app (selenium
-> app
).
version: '3'
services:
nginx:
image: <my_private_repo>/nginx
...
db:
image: <my_private_repo>/db
# nginx cannot communicate with db after I remove exposing port 3306
ports:
- "3306:3306"
...
selenium:
image: joyzoursky/python-chromedriver:3.8-selenium
command: "bash seleniumScript.sh"
depends_on:
- nginx
- db
I run my tests with docker-compose run selenium
Everything works when I expose database port but when I stop exposing port 3306 nginx is unable to communicate with db.
My understanding was that all services within one docker-compose setup are automatically connected and user doesn't need to create any additional networks but I was obviously wrong.
My question is how should I setup exposing ports between services without exposing them to host machine?
Update:
My nginx app has configured database host to service name db
. Selenium script setup fill database using db
host and tests nginx app via https://nginx
url.
Upvotes: 0
Views: 1152
Reputation: 4682
This question here asks something very similar.
The nginx container cannot reach the database container probably, because you're accessing it via docker host localhost
.
Containers that are in the same network can always reach each other without exposing any ports, but using hostnames. See: the docker docs. That means, that you can connect to the database container from inside the nginx container using db:3306
.
Compose by default creates a default network for the project, so unless you want to prevent some containers from being able to communicate with each other, you won't have to create any new networks.
Upvotes: 1