Reputation: 179
How I can automatically pass backend link\ip for frontend ?
I have two services:
services:
core:
restart: always
volumes:
- pws.vlm.logs:/var/www/pws/logs
networks:
- pws-net
environment:
DB_URL: postgres://pws_pg_user:${DB_PASSWORD:-default_password}@postgres/pws
NODE_ENV: ${NODE_ENV:-production}
JWT_SECRET: ${JWT_SECRET}
depends_on:
- postgres
ports:
- 3100:3080
web:
restart: always
volumes:
- pws.vlm.logs:/var/www/pws/logs
networks:
- pws-net
environment:
NODE_ENV: ${NODE_ENV:-production}
API_URL: ${API_URL:-http://127.0.0.1:3100}
depends_on:
- core
ports:
- 3000:3080
If I want to make api calls from frontend to backend I need to take ip address from host or domain name and pass it to web container as API_URL
, Is there some opportunity to use docker networks to pass link to backend by container name, so I can set API_URL=core and It automatically substitute on ip or domain ?
Upvotes: 0
Views: 140
Reputation: 4370
Your service names (core
and web
) act as the DNS entries. You can use http://core:3100/api
from frontend to access the services of backend.
Upvotes: 1
Reputation: 5770
Docker compose orchestrates the containers so that they contact each other directly. Just use the used name of the backend as hostname, in this case core
. You still have to append the port, but the backend is reachable under http://core:3100
so the entry for web would look like this:
web:
restart: always
volumes:
- pws.vlm.logs:/var/www/pws/logs
networks:
- pws-net
environment:
NODE_ENV: ${NODE_ENV:-production}
API_URL: http://core:3100
depends_on:
- core
ports:
- 3000:3080
Upvotes: 2