How do I maintain connection between Docker containers while having one of them connected to host machine?
I am currently deploying an application with Docker (version 27.2.1), using docker compose
on debian 11.
My docker-compose.yml
file consists in defining 4 containers (app
, api
, db
and ngninx
).
app
and api
are Django applications. db
is a postgresql service.
The whole is deployed on a machine that hosts an SMTP service on port 25.
My Django app app
needs to use this service to send emails to the users, but it also has to be able to connect with the other containers. For example, db
is the host of the postgresql server that contains all the data managed by the Django models defined in app
.
I cannot find how my containers can be on the same network while having the app
service in relation to the host machine. This prevents me from using the SMTP I need to use. From what I have read on this site and in the docs, it seems that it is not possible to have a service running on both network_mode=host
and on a default bridge network.
Here is the docker-compose.yml
file.
services:
app:
build: ./app
depends_on:
db:
condition: service_healthy
ports:
- "8080:8080"
volumes:
...
extra_hosts:
- "host.docker.internal:host-gateway"
api:
build: ./api
depends_on:
db:
condition: service_healthy
ports:
- "8081:8081"
volumes:
...
db:
build: ./db
healthcheck:
test: ["CMD-SHELL", "pg_isready -U user-h localhost -d database"]
interval: 5s
timeout: 5s
retries: 5
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
...
depends_on:
- app
- api
volumes:
...
Once I get how to connect to the host machine successfully, I will modify my settings.py
for the correct EMAIL_HOST
.
I have tried several options.
First, I added this mention in the app part of the docker-compose
file:
extra_hosts:
- "host.docker.internal:host-gateway"
But, when I get into the app container and try, for example, telnet host.docker.internal 25
, the connection is refused.
I tried to directly bind the port of the host machine to a port of the app container. When using this configuration and executing telnet <ip_address_of_host> 25
, the connection succeeds but is immediately closed by the foreign host.
The other options I have tried so far are not viable as they were using simultaneously host mode and networks.