Reputation: 976
I have a docker compose file with 2 services: an apache airflow service and a postgres service for the airflow.
I have a postgres db running locally on port 5555. How can I connect my docker compose file to my local db?
My trimmed compose file looks like this:
version: 3
services:
postgres:
ports:
- "5432:5432"
webserver:
environment:
- db_uri=path.localhost:5555/db_name
ports:
- "8080:8080"
network_mode: "host"
When I run my docker like this I get this error
(psycopg2.OperationalError) could not translate host name "postgres" to address: Name or service not known
.
However if I change the env var from localhost to my inet ip address (db_uri=path.123.456.789.012:5555/db_name
) and remove the network_mode
option it works fine. How can I change it to be dynamic so if someone else runs the docker it will also work for them?
Upvotes: 1
Views: 3456
Reputation: 11
I know this is an old question but I think It should be like this: webserver:
environment:
- db_uri=postgres:5432/db_name
ports:
- "8080:8080"
Upvotes: 1
Reputation: 59946
You can not connect with HOST database using localhost
or other service using service name in host network, in Host network service does not get IP, so every service will be available in host network and you can access other service using HOST IP
or Docker host for window and Mac etc.
If you use the
host network mode
for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated. For instance, if you run a container which binds to port 80 and you use host networking, the container’s application is available on port 80 on the host’s IP address.
If you want to access host database then you can pass HOST IP or you can use host.docker.internal
but this does not work in linux, for mac and window it should work.
so you can try
version: 3
services:
postgres:
ports:
- "5432:5432"
webserver:
environment:
- db_uri=HOST_IP:5555/db_name
ports:
- "8080:8080"
Upvotes: 1